Skip to content

Commit cfe365a

Browse files
authored
Introduces a new option to publish specific auth actions. (#551)
* fix: docs for auth wip * Fix styling * fix: reset * Fix styling * fix: wip * Fix styling * fix: try to offer full permissions for windows * fix: retry tests * fix: refactoring tests * fix: wip * fix: wi * fix: wip * fix: pipeline --------- Co-authored-by: binaryk <[email protected]>
1 parent 5b1920b commit cfe365a

32 files changed

+616
-308
lines changed

.github/workflows/tests.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,11 @@ jobs:
3939
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
4040
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
4141
42+
- name: Clear Composer cache
43+
run: composer clear-cache
44+
45+
- name: Wait for a few seconds
46+
run: sleep 5
47+
4248
- name: Execute tests
4349
run: ./vendor/bin/testbench package:test --parallel --no-coverage

docs-v2/content/en/api/rest-methods.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
--- title: REST Methods menuTitle: Controllers category: API position: 12 ---
1+
---
2+
title: REST Methods
3+
menuTitle: Controllers
4+
category: API
5+
position: 12
6+
---
27

38
## Introduction
49

docs-v2/content/en/auth/authentication.md

Lines changed: 275 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ Now you can finally enjoy the auth setup (`register`, `login`, `forgot`, and `re
1313

1414
Migrate the `users`, `password_resets` table (they already exist into a fresh Laravel app).
1515

16+
<alert type="success">
17+
18+
Laravel 10 automatically ships with Sanctum, so you don't have to install it.
19+
20+
</alert>
21+
1622
### Install sanctum
1723

1824
See the docs [here](https://laravel.com/docs/sanctum#installation). You don't need to add `\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,` in your `'api'` middleware group.
@@ -42,7 +48,8 @@ The `User` model should extend the `Illuminate\Foundation\Auth\User` class or im
4248

4349
<alert type="info">
4450

45-
Make sure you didn't skip adding the `\Laravel\Sanctum\HasApiTokens` trait to your `User` model.
51+
Make sure you have the `\Laravel\Sanctum\HasApiTokens` trait to your `User` model.
52+
Laravel 10 will automatically add this trait to your `User` model.
4653

4754
</alert>
4855

@@ -66,32 +73,33 @@ Restify provides you a simple way to add all of your auth routes prepared. Simpl
6673
Route::restifyAuth();
6774
```
6875

69-
And voila, now you have auth routes ready to be used.
76+
And voilà, now you have auth routes ready to be used.
7077

7178
These are the default routes provided by restify:
7279

73-
| Verb | URI | Action |
74-
| :------------- |:-----------------------------------------| :----------------|
75-
| **POST** | `/api/register` | register |
76-
| **POST** | `/api/login` | login |
77-
| **POST** | `/api/restify/forgotPassword` | forgot password |
78-
| **POST** | `/api/restify/resetPassword` | reset password |
79-
| **POST** | `/api/restify/verify/{id}/{emailHash}` | verify user |
80+
| Verb | URI | Action |
81+
| :------------- |:-----------------------------------------|:---------------|
82+
| **POST** | `/api/register` | register |
83+
| **POST** | `/api/login` | login |
84+
| **POST** | `/api/restify/forgotPassword` | forgotPassword |
85+
| **POST** | `/api/restify/resetPassword` | resetPassword |
86+
| **POST** | `/api/restify/verify/{id}/{emailHash}` | verifyEmail |
8087

8188
<alert type="info">
8289

8390
The `register` and `login` routes are outside the base `restify` prefix because they don't have to follow the `auth` middleware defined in the `config/restify.php` config file.
8491

8592
</alert>
8693

87-
## Export auth controllers
8894

89-
All of these routes are handled by default, so you can just use them facilely. However, you can customize each of them by exporting auth controllers:
95+
You can also pass an `actions` argument, which is an array of actions you want to register. For example:
9096

91-
```shell
92-
php artisan restify:auth
97+
```php
98+
Route::restifyAuth(actions: ['login', 'register']);
9399
```
94-
Now you have all the auth controllers and blade email files exported into your project.
100+
101+
By using the `actions` argument, only the specified routes will be registered. If no `actions` argument is passed, Restify will register all the routes by default.
102+
95103

96104
## Sanctum Middleware
97105

@@ -105,3 +113,256 @@ Next, add the `auth:sanctum` middleware after the `api` middleware in your confi
105113
...
106114
],
107115
```
116+
117+
## Login
118+
119+
Let's ensure the authentication is working correctly. Create a user in the DatabaseSeeder class:
120+
121+
```php
122+
// DatabaseSeeder.php
123+
\App\Models\User::factory()->create([
124+
'name' => 'Test User',
125+
'email' => '[email protected]',
126+
'password' => \Illuminate\Support\Facades\Hash::make('password'),
127+
]);
128+
```
129+
130+
Seed it:
131+
132+
```shell
133+
php artisan db:seed
134+
```
135+
136+
Now you can test the login with Curl or Postman:
137+
138+
```shell
139+
curl -X POST "http://restify-app.test/api/login" \
140+
-H "Accept: application/json" \
141+
-H "Content-Type: application/json" \
142+
-d '{
143+
"email": "[email protected]",
144+
"password": "password"
145+
}'
146+
```
147+
148+
So you should see the response like this:
149+
150+
```json
151+
{
152+
"id": "11",
153+
"type": "users",
154+
"attributes": {
155+
"name": "Test User",
156+
"email": "[email protected]"
157+
},
158+
"meta": {
159+
"authorizedToShow": true,
160+
"authorizedToStore": false,
161+
"authorizedToUpdate": false,
162+
"authorizedToDelete": false,
163+
"token": "1|f7D1qkALtM9GKDkjREKpwMRKTZg2ZnFqDZTSe53k"
164+
}
165+
}
166+
```
167+
168+
## Register
169+
170+
Let's see how to register a new user in the application. You can test the registration using Curl or Postman.
171+
172+
Use the following endpoint for registration:
173+
174+
`http://restify-app.test/api/register`
175+
176+
And send this payload:
177+
178+
```json
179+
{
180+
"name": "John Doe",
181+
"email": "[email protected]",
182+
"password": "secret!",
183+
"password_confirmation": "secret!"
184+
}
185+
```
186+
187+
Note: Email and password fields are required.
188+
189+
Now, you can send a POST request with Curl:
190+
191+
```shell
192+
curl -X POST "http://restify-app.test/api/register" \
193+
-H "Accept: application/json" \
194+
-H "Content-Type: application/json" \
195+
-d '{
196+
"name": "John Doe",
197+
"email": "[email protected]",
198+
"password": "secret!",
199+
"password_confirmation": "secret!"
200+
}'
201+
```
202+
203+
You should see the response like this:
204+
205+
```json
206+
{
207+
"id": "12",
208+
"type": "users",
209+
"attributes": {
210+
"name": "John Doe",
211+
"email": "[email protected]"
212+
},
213+
"meta": {
214+
"authorizedToShow": true,
215+
"authorizedToStore": false,
216+
"authorizedToUpdate": false,
217+
"authorizedToDelete": false,
218+
"token": "2|z8D2rkBLtN8GKDkjREKpwMRKTZg2ZnFqDZTSe53k"
219+
}
220+
}
221+
```
222+
223+
## Forgot Password
224+
225+
To initiate the password reset process, use the following endpoint:
226+
227+
`{{host}}/api/forgotPassword`
228+
229+
And send this payload:
230+
231+
```json
232+
{
233+
"email": "[email protected]"
234+
}
235+
```
236+
237+
After making a POST request to this endpoint, an email will be sent to the provided email address containing a link to reset the password. The link looks like this:
238+
239+
`'password_reset_url' => env('FRONTEND_APP_URL').'/password/reset?token={token}&email={email}',`
240+
241+
This configuration can be found in the `config/restify.php` file. The FRONTEND_APP_URL should be set to the URL of your frontend app, where the user lands when they click the action button in the email. The "token" is a variable that will be used to reset the password later on.
242+
243+
To view the email content during development, you can change the following configuration in your .env file:
244+
245+
```dotenv
246+
MAIL_MAILER=log
247+
```
248+
249+
This will log the email content to the `laravel.log` file, allowing you to see the password reset email without actually sending it.
250+
251+
Now, you can send a POST request with Curl:
252+
253+
```shell
254+
curl -X POST "http://restify-app.test/api/forgotPassword" \
255+
-H "Accept: application/json" \
256+
-H "Content-Type: application/json" \
257+
-d '{
258+
"email": "[email protected]"
259+
}'
260+
```
261+
262+
If the email is successfully sent, you'll receive a response similar to the following:
263+
264+
```json
265+
{
266+
"message": "Reset password link sent to your email."
267+
}
268+
```
269+
270+
Now, the user can follow the link in the email to reset their password.
271+
272+
273+
## Reset Password
274+
275+
After the user has received the password reset email from the Forgot Password process, they can reset their password using the following endpoint:
276+
277+
`http://restify-app.test/api/resetPassword`
278+
279+
The payload should include the token and email received from the password reset email:
280+
281+
```json
282+
{
283+
"token": "7e474bb9118e736306de27126343644a7cb0ecdaec558fdef30946d15225bc07",
284+
"email": "[email protected]",
285+
"password": "new_password",
286+
"password_confirmation": "new_password"
287+
}
288+
```
289+
Now, you can send a POST request with Curl:
290+
291+
```shell
292+
curl -X POST "http://restify-app.test/api/resetPassword" \
293+
-H "Accept: application/json" \
294+
-H "Content-Type: application/json" \
295+
-d '{
296+
"token": "0d20b6cfa48f2bbbb83bf913d5e329207149f74d7b22d59a383d321c7af7fd5e",
297+
"email": "[email protected]",
298+
"password": "new_password",
299+
"password_confirmation": "new_password"
300+
}'
301+
```
302+
303+
If the password reset is successful, you should receive a response similar to the following:
304+
305+
```json
306+
{
307+
"message": "Your password has been successfully reset."
308+
}
309+
```
310+
311+
Now the user's password has been successfully reset, and they can log in with their new password.
312+
313+
314+
## Customizing Authentication Controllers
315+
316+
You can publish the authentication controllers from the Restify package to your own application, allowing you to customize their behavior as needed. To publish the controllers, run the following command:
317+
318+
```shell
319+
php artisan restify:auth
320+
```
321+
322+
This command will copy the authentication controllers to the `app/Http/Controllers/Restify` directory in your Laravel project.
323+
324+
The command accepts an optional `--actions` parameter, which allows you to specify which controllers you want to publish. If no action is passed, the command will publish all controllers and the `ForgotPasswordNotification`. For example, to publish only the `login` and `register` controllers, run:
325+
326+
```shell
327+
php artisan restify:auth --actions=login,register
328+
```
329+
330+
Now, you can make any necessary changes to these controllers to fit your specific requirements.
331+
332+
### Customizing the Register Route
333+
334+
In a real-world scenario, you might need to customize only the register route. To do this, you can use the `restify:auth` command with the `--actions` option to publish only the register controller:
335+
336+
```shell
337+
php artisan restify:auth --actions=register
338+
```
339+
340+
After running the command, the register controller will be published to your application, and you can modify it to fit your requirements.
341+
342+
<alert type="warning">
343+
344+
Important Note: If you want to publish other actions in the future, you'll need to manually update the `routes/api.php` file before running the restify:auth command again. Remove any previously published Restify routes, and keep the `Route::restifyAuth();` line so that the new routes can be correctly published.
345+
346+
</alert>
347+
348+
For example, if you previously published the register route, your `routes/api.php` file might look like this:
349+
350+
```php
351+
// ...
352+
353+
Route::restifyAuth(actions: ["login", "resetPassword", "forgotPassword", "verifyEmail"]);
354+
355+
// ...
356+
```
357+
358+
Before running the `restify:auth` command again, revert the file to its original state:
359+
360+
```php
361+
// ...
362+
363+
Route::restifyAuth();
364+
365+
// ...
366+
```
367+
368+
Now you can run the `restify:auth` command with other actions, and the routes will be published correctly.

docs-v2/content/en/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ Laravel Restify is an extraordinary tool inspired by [Laravel Nova](https://nova
99

1010
If you don't have an application written with Nova, you can start with Laravel Restify from scratch and get a powerful API in a few minutes.
1111

12+
<alert type="warning">
13+
14+
This documentation is for the latest version of Laravel Restify. Please ensure you are using the most recent release of the package to guarantee compatibility with the information provided in this documentation. To update Laravel Restify, refer to the upgrade guide for instructions.
15+
16+
</alert>
17+
1218
## Features
1319

1420
<list :items="[
@@ -22,6 +28,8 @@ If you don't have an application written with Nova, you can start with Laravel R
2228
]">
2329
</list>
2430

31+
32+
2533
## Playground
2634

2735
You can find a playground in the [Restify Demo GitHub repository](https://github.com/BinarCode/restify-demo).

0 commit comments

Comments
 (0)