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: src/docs/auth/login.md
+70-41Lines changed: 70 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,25 +42,70 @@ auth()->login([
42
42
]);
43
43
```
44
44
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.
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:
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:
@@ -70,22 +115,22 @@ Leaf uses token based authentication by default which uses a JWT to authenticate
70
115
auth()->config('session', true);
71
116
```
72
117
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:
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:
118
163
119
164
```php:no-line-numbers
120
-
auth()->config('password', false);
165
+
auth()->config('password.key', false);
121
166
```
122
167
123
168
Once this is done, Leaf will no longer expect a password field to authenticate users and will also turn off password hashing and verification.
124
169
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
-
143
170
## Password verification
144
171
145
172
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!');
171
198
172
199
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.
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.
Copy file name to clipboardExpand all lines: src/docs/auth/signup.md
+31-9Lines changed: 31 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -76,19 +76,22 @@ auth()->register([
76
76
]);
77
77
```
78
78
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.
You can find the date format documentation [here](/docs/utils/date#formatting-dates).
136
139
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
+
137
159
## Hiding sensitive information
138
160
139
161
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
163
185
auth()->config('session', true);
164
186
```
165
187
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.
0 commit comments