You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs-v2/content/en/auth/authentication.md
+52-1Lines changed: 52 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,21 @@ Laravel Restify has the support for a facile authentication with [Laravel Sanctu
9
9
10
10
Now you can finally enjoy the auth setup (`register`, `login`, `forgot`, and `reset password`).
11
11
12
+
## Quick start
13
+
14
+
tl;dr:
15
+
16
+
If you run on Laravel 10 or higher, you can use this command that will do all the setup for you:
17
+
18
+
```shell script
19
+
php artisan restify:setup-auth
20
+
```
21
+
22
+
This command will:
23
+
24
+
-**ensures** that `Sanctum` is installed and configured as the authentication provider in the `config/restify.php` file
25
+
-**appends** the `Route::restifyAuth();` line to the `routes/api.php` file to add the authentication routes
26
+
12
27
## Prerequisites
13
28
14
29
Migrate the `users`, `password_resets` table (they already exist into a fresh Laravel app).
@@ -116,7 +131,7 @@ Next, add the `auth:sanctum` middleware after the `api` middleware in your confi
116
131
117
132
## Login
118
133
119
-
Let's ensure the authentication is working correctly. Create a user in the DatabaseSeeder class:
134
+
Let's ensure the authentication is working correctly. Create a user in the `DatabaseSeeder` class:
120
135
121
136
```php
122
137
// DatabaseSeeder.php
@@ -165,6 +180,42 @@ So you should see the response like this:
165
180
}
166
181
```
167
182
183
+
### Authorization
184
+
185
+
We will discuss the authorization in more details here [Authorization](/auth/authorization). But for now let's see a simple example.
186
+
187
+
After a successful login, you will receive an authentication token. You should include this token as a `Bearer` token in the Authorization header for your subsequent API requests using [Postman](https://learning.postman.com/docs/sending-requests/authorization/#bearer-token), axios library, or cURL.
188
+
189
+
Here's an axios example for retrieving the user's profile with the generated token:
Copy file name to clipboardExpand all lines: docs-v2/content/en/auth/authorization.md
+8-2Lines changed: 8 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -15,15 +15,21 @@ Before diving into details about authorization, it is important for you to under
15
15
16
16
When you run a request (ie via Postman), it hits the Laravel application. Laravel will load every single Service Provider it has defined into `config/app.php` and [auto discovered ](https://laravel.com/docs/packages#package-discovery) providers as well.
17
17
18
-
Restify injects the `RestifyApplicationServiceProvider` in your `config/app.php` and it also has an auto discovered provider called `LaravelRestify\LaravelRestifyServiceProvider`.
18
+
Restify injects the `RestifyApplicationServiceProvider` in your `config/app.php` and it also has an auto discovered provider called `\Binaryk\LaravelRestify\LaravelRestifyServiceProvider`.
19
19
20
20
- The `LaravelRestifyServiceProvider` is booted first. This will basically push the `RestifyInjector` middleware at the end of the middleware stack.
21
21
22
22
- Then, the `RestifyApplicationServiceProvider` is booted. This will define the gate, will load repositories and make the auth routes macro. You now have full control over this provider.
23
23
24
24
- The `RestifyInjector` will be handled. It will register all the routes.
25
25
26
-
- On each request, if the requested route is a Restify route, Laravel will handle other middlewares defined in the `restify.php` -> `middleware`.
26
+
- On each request, if the requested route is a Restify route, Laravel will handle other middlewares defined in the `restify.php` -> `middleware`. Here is where you should have the `auth:sanctum` middleware to protect your API against unauthenticated users.
27
+
28
+
## Prerequisites
29
+
30
+
Before we dive into the details of authorization, we need to make sure that you have a basic understanding of how Laravel's authorization works. If you are not familiar with it, we highly recommend reading the [documentation](https://laravel.com/docs/authorization) before you move forward.
31
+
32
+
You may also visit the [Authentication/login](/auth/authentication#authorization) section to learn how to login and use the Bearer token.
Copy file name to clipboardExpand all lines: docs-v2/content/en/auth/profile.md
+16-6Lines changed: 16 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -5,29 +5,39 @@ category: Auth
5
5
position: 1
6
6
---
7
7
8
-
## Sanctum middleware
8
+
## Prerequisites
9
9
10
-
To make sure you can get your profile just right, you should add the `auth:sanctum` middleware to the restify middleware config:
10
+
Make sure you followed the [Authentication](/docs/auth/authentication) guide before, because one common mistake is that people do not add this middleware:
Before retrieving the user's profile, you need to log in and obtain an authentication token. You can refer to the [login documentation](/auth/authentication#login) for details on how to authenticate a user. Make sure to include the `Bearer {$token}` in the `Authorization` header for the subsequent API requests, either using Postman or cURL.
24
+
25
25
When retrieving the user's profile, it is serialized by using the `UserRepository`.
26
26
27
27
```http request
28
28
GET: /api/restify/profile
29
29
```
30
30
31
+
Here's an example of a cURL request for retrieving the user's profile with a random token:
32
+
33
+
```bash
34
+
curl -X GET "http://your-domain.com/api/restify/profile" \
Replace `http://your-domain.com` with your actual domain and `eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...` with the authentication token you obtained after logging in.
0 commit comments