Skip to content

Commit a8259f4

Browse files
committed
feat: update auth docs
1 parent 0803bd2 commit a8259f4

File tree

4 files changed

+103
-52
lines changed

4 files changed

+103
-52
lines changed

.vitepress/config/sidebar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const sidebar = [
9393
link: '/docs/auth/protected-routes',
9494
},
9595
// { text: 'Updating logged-in user', link: '/docs/auth/update' },
96-
{ text: 'Build your own auth library', link: '/docs/auth/helpers' },
96+
// { text: 'Build your own auth library', link: '/docs/auth/helpers' },
9797
],
9898
},
9999
{
@@ -127,7 +127,7 @@ const sidebar = [
127127
{ text: 'HTTP Cache', link: '/docs/http/caching' },
128128
{ text: 'Leaf Mail', link: '/docs/utils/mail/' },
129129
{ text: 'File System', link: '/docs/utils/fs' },
130-
{ text: 'Queues/Jobs', link: '/docs/utils/queues' },
130+
// { text: 'Queues/Jobs', link: '/docs/utils/queues' },
131131
],
132132
},
133133
{

src/docs/auth/login.md

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,70 @@ auth()->login([
4242
]);
4343
```
4444

45-
If the user is successfully authenticated, a session or token is created for them and the user is returned. If the user is not found or the password is incorrect, the method returns `null`. You can then use the `errors()` method to get the error message.
45+
The `login()` method returns `true` if the user is successfully authenticated and `false` if the user is not authenticated. You can then use the `errors()` method to get the error message.
4646

4747
```php
48-
$data = auth()->login([
48+
$success = auth()->login([
4949
'email' => '[email protected]',
5050
'password' => 'password'
5151
]);
5252

53-
if ($data) {
53+
if ($success) {
5454
// User is authenticated
55-
$token = $data->token;
56-
$user = $data->user;
5755
} else {
5856
// User is not authenticated
5957
$error = auth()->errors();
6058
}
6159
```
6260

63-
The output of the `login()` method is an object with the user's data and the token or session. You can then use this data to manage the user's session or token.
61+
You can get the data and tokens needed for authentication using the `data()` method:
62+
63+
```php
64+
$data = auth()->data();
65+
// ['user' => [...], 'accessToken' => '...', 'refreshToken' => '...']
66+
```
67+
68+
If you want to use a couple of fields from the user within your application, you can use the user method:
69+
70+
## The user object
71+
72+
The `user()` method gives you access to an object configured to the user's data. This object is a simple way to access the user's data without having to go through the data array. There are many things you can do with the user object:
73+
74+
- Getting user information (including hidden fields)
75+
- Token/Session management
76+
- Fetching related data from your database
77+
78+
```php
79+
// return all user data without hidden fields
80+
$user = auth()->user()->get();
81+
82+
// pick specific fields
83+
$username = auth()->user()->username;
84+
$email = auth()->user()->email;
85+
86+
// get the user's data + tokens
87+
$data = auth()->user()->getAuthInfo();
88+
```
89+
90+
If your user has one to many relationships with other models, you can fetch related data using the user object:
91+
92+
```php
93+
$posts = auth()->user()->posts();
94+
// will return a Leaf DB instance with posts by the current user
95+
// SELECT * FROM posts WHERE user_id = $current_user_id
96+
```
97+
98+
You can further filter the data by using any of the Leaf DB methods:
99+
100+
```php:no-line-numbers
101+
$posts = auth()->user()->posts()->where('title', 'like', '%leaf%')->get();
102+
```
103+
104+
Note that the method name should be the same as the table name in your database.
105+
106+
```php:no-line-numbers
107+
$posts = auth()->user()->transactions()->where('amount', '>', 1000)->get();
108+
```
64109

65110
## Switching to session auth
66111

@@ -70,22 +115,22 @@ Leaf uses token based authentication by default which uses a JWT to authenticate
70115
auth()->config('session', true);
71116
```
72117

73-
With the addition of session auth, `login()` will automatically start a session, but will leave redirects and every other thing to you:
118+
With the addition of session auth, `login()` will automatically start a session, but will behave in the same way, which means redirects and any other functionality you need will be left up to you to handle:
74119

75120
```php
76121
auth()->config('session', true);
77122

78123
...
79124

80125
// session is automatically started
81-
$data = auth()->login([
126+
$success = auth()->login([
82127
'email' => '[email protected]',
83128
'password' => 'password'
84129
]);
85130

86-
if ($data) {
131+
if ($success) {
87132
// User is authenticated
88-
$user = $data->user;
133+
$user = auth()->user();
89134

90135
response()->redirect('/dashboard');
91136
} else {
@@ -117,29 +162,11 @@ auth()->config('password.key', 'pass');
117162
Better still, you can turn off password authentication completely. This is useful in multi-step authentication systems, where you might authenticate a set of parameters before authenticating the password. To turn off password authentication, you can configure Leaf Auth like this:
118163

119164
```php:no-line-numbers
120-
auth()->config('password', false);
165+
auth()->config('password.key', false);
121166
```
122167

123168
Once this is done, Leaf will no longer expect a password field to authenticate users and will also turn off password hashing and verification.
124169

125-
## Password hashing
126-
127-
Leaf allows you to customize how Leaf should encode passwords. By default, Leaf uses the `Leaf\Helpers\Password::hash` method which has support for `bcrypt` and `argon2`. If you however want to use a different method or turn off password encoding, you can do that directly in the config:
128-
129-
```php
130-
auth()->config('password.encode', false); // turn off encoding
131-
132-
auth()->config('password.encode', function ($password) {
133-
return Password::hash($password);
134-
});
135-
```
136-
137-
These are the available options you can pass to `password.encode`:
138-
139-
- `false` - This turns off password encoding
140-
- `null`/`true` - This uses the default encoding method (Leaf\Helpers\Password::hash)
141-
- `function` - This uses a custom method. The method should accept a password and return the encoded password.
142-
143170
## Password verification
144171

145172
Password verification is done to check if a password matches the hashed password in the database. By default, Leaf uses the `Leaf\Helpers\Password::verify` method which has support for `bcrypt` and `argon2`. If you however want to use a different method or turn off password verification, you can do that directly in the config:
@@ -171,15 +198,17 @@ auth()->config('messages.loginPasswordError', 'Password is incorrect!');
171198

172199
The output of Leaf's authentication methods is an object with the user's data and the token or session. By default, the password field is hidden from the user data. This is a security measure to prevent the password from being exposed.
173200

174-
```php
175-
[
176-
'user' => [
177-
'username' => 'mychidarko',
178-
'email' => '[email protected]',
179-
'created_at' => '2019-09-20 13:47:48'
180-
],
181-
'token' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzYxMzUzMjgsImlzcyI6ImxvY2FsaG9zdCIsImV4cCI6MTU3NjEzNjIyOCwidXNlcklkIjoxfQ.7FODXGGJKioGQVX4ic0DJLoMIQTVUlsd4zFAJA4DAkg'
182-
]
201+
```json
202+
{
203+
"user": {
204+
"username": "mychidd22",
205+
"email": "[email protected]",
206+
"created_at": "2024-10-26 19:29:37.000000",
207+
"updated_at": "2024-10-26 19:29:37.000000"
208+
},
209+
"accessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyLmlkIjo5MjQsImlhdCI6MTczMDAyOTAwNywiZXhwIjo4NjQwMCwiaXNzIjoibG9jYWxob3N0OjU1MDAifQ.yLldIhOkUxn54-3RWLD7PJONoWwqpZ5mmP8fEZ4nNfs",
210+
"refreshToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyLmlkIjo5MjQsImlhdCI6MTczMDAyOTAwNywiZXhwIjozNDU2MDAsImlzcyI6ImxvY2FsaG9zdDo1NTAwIn0.tqfp5Co4vLtq3_J0r5Fp-XwuDSB1i6uC4AcogQ3vnc8"
211+
}
183212
```
184213

185214
If you want to customize what items are hidden from the user data, you can configure Leaf Auth to hide them:
@@ -229,14 +258,14 @@ auth()->config('unique', ['email', 'username']);
229258
Now if a user tries to update their profile with an email or username that already exists in the database, Leaf Auth will return an error. You can get the error message using the `errors()` method.
230259

231260
```php
232-
$data = auth()->update([
261+
$success = auth()->update([
233262
'username' => 'example',
234263
'email' => '[email protected]'
235264
]);
236265

237-
if (!$data) {
266+
if (!$success) {
238267
$error = auth()->errors();
239-
// ['email' => 'The email already exists']
268+
// ['email' => 'email already exists']
240269
}
241270
```
242271

src/docs/auth/signup.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,22 @@ auth()->register([
7676
]);
7777
```
7878

79-
If the user is successfully saved in the database, a session or token is created for them and the user is returned. If Leaf Auth fails to save the user, the method returns `null`. You can then use the `errors()` method to get the error message.
79+
If the user is successfully saved in the database, a session or token is created for them and the user is returned. If Leaf Auth fails to save the user, the method returns `false`. You can then use the `errors()` method to get the error message.
8080

8181
```php
82-
$data = auth()->register([
82+
$success = auth()->register([
8383
'username' => 'example',
8484
'email' => '[email protected]',
8585
'password' => 'password'
8686
]);
8787

88-
if ($data) {
88+
if ($success) {
8989
// User is authenticated
90-
$token = $data->token;
91-
$user = $data->user;
90+
$token = auth()->data();
91+
// ['user' => [...], 'accessToken' => '...', 'refreshToken' => '...']
92+
93+
94+
$username = auth()->user()->username;
9295
} else {
9396
// User is not authenticated
9497
$error = auth()->errors();
@@ -134,6 +137,25 @@ auth()->config('timestamps.format', 'YYYY-MM-DD HH:MM:SS');
134137

135138
You can find the date format documentation [here](/docs/utils/date#formatting-dates).
136139

140+
## Password hashing
141+
142+
Leaf allows you to customize how user passwords should be encoded before they are stored in your database. By default, Leaf uses the `Leaf\Helpers\Password::hash` method which has support for `bcrypt` and `argon2`. If you however want to use a different method or turn off password encoding, you can do that directly in the config:
143+
144+
```php
145+
auth()->config('password.encode', false); // turn off encoding
146+
147+
auth()->config('password.encode', function ($password) {
148+
// return the encoded password
149+
return Password::hash($password);
150+
});
151+
```
152+
153+
These are the available options you can pass to `password.encode`:
154+
155+
- `false` - This turns off password encoding
156+
- `null`/`true` - This uses the default encoding method (Leaf\Helpers\Password::hash)
157+
- `function` - This uses a custom method. The method should accept a password and return the encoded password.
158+
137159
## Hiding sensitive information
138160

139161
The output of Leaf's authentication methods is an object with the user's data and the token or session. By default, the password field is hidden from the user data. This is a security measure to prevent the password from being exposed.
@@ -163,21 +185,21 @@ Leaf uses token based authentication by default which uses a JWT to authenticate
163185
auth()->config('session', true);
164186
```
165187

166-
Switching to session auth does not change the default behaviour of the `register()` method. It won't create a session or do anything fancy by default. If you want to create a session immediately after signing a user up, you can pass true to the `session.register` config:
188+
Switching to session auth does not change the default behaviour of the `register()` method. It does everything the same way it would if you were using token based authentication except that it creates a new session when a user is registered.
167189

168190
```php:no-line-numbers
169-
auth()->config('session.register', true);
191+
auth()->config('session', true);
170192
171193
...
172194
173195
// will create a session
174-
$data = auth()->register([
196+
$success = auth()->register([
175197
'username' => 'example',
176198
'email' => '[email protected]',
177199
'password' => 'password'
178200
]);
179201
180-
if ($data) {
202+
if ($success) {
181203
response()->redirect('/dashboard');
182204
} else {
183205
$error = auth()->errors();
File renamed without changes.

0 commit comments

Comments
 (0)