Skip to content

Commit 2e8404f

Browse files
committed
feat: update auth docs
1 parent 2e777fa commit 2e8404f

File tree

4 files changed

+146
-99
lines changed

4 files changed

+146
-99
lines changed

.vitepress/config/sidebar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const sidebar = [
8686
// { text: 'Auth Config', link: '/docs/auth/config' },
8787
{ text: 'User Login', link: '/docs/auth/login' },
8888
{ text: 'User Sign Up', link: '/docs/auth/signup' },
89-
// { text: 'Auth Session', link: '/docs/auth/session' },
89+
{ text: 'Auth User', link: '/docs/auth/user' },
9090
{
9191
text: 'Protecting your routes',
9292
link: '/docs/auth/protected-routes',

src/docs/auth/login.md

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -65,47 +65,7 @@ $data = auth()->data();
6565
// ['user' => [...], 'accessToken' => '...', 'refreshToken' => '...']
6666
```
6767

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-
```
68+
If you want to use a couple of fields from the user within your application, you can use the user method. You can find the documentation for the Auth user method [here](/docs/auth/user).
10969

11070
## Switching to session auth
11171

@@ -236,59 +196,3 @@ auth()->config('session.lifetime', 0); // never expire
236196
```
237197

238198
:::
239-
240-
## Updating a logged-in user
241-
242-
Updating user information means allowing users to change their details (like username, email, or password) in your application. It is a very common feature in most applications.
243-
244-
Leaf provides an `update()` method that allows you to update a user's information.
245-
246-
```php
247-
$user = auth()->update($data);
248-
```
249-
250-
### Unique values
251-
252-
Leaf Auth allows you to set unique fields which should not be repeated for different users. For example, you wouldn't want two users to have the same email address. You can configure Leaf Auth to check for unique fields when a user is being updated:
253-
254-
```php:no-line-numbers
255-
auth()->config('unique', ['email', 'username']);
256-
```
257-
258-
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.
259-
260-
```php
261-
$success = auth()->update([
262-
'username' => 'example',
263-
'email' => '[email protected]'
264-
]);
265-
266-
if (!$success) {
267-
$error = auth()->errors();
268-
// ['email' => 'email already exists']
269-
}
270-
```
271-
272-
## Signing a user out
273-
274-
When a user chooses to end their session, or their session expires, you can sign them out using the `logout()` method. This method will end the user's session or delete their token.
275-
276-
```php
277-
auth()->logout();
278-
279-
// or redirect the user after logout
280-
auth()->logout('/login');
281-
282-
// or redirect to a named route
283-
auth()->logout(['homepage']);
284-
```
285-
286-
If you want to perform a custom operation when a user logs out, you can set a handler for the logout operation:
287-
288-
```php
289-
auth()->config('session.logout', function () {
290-
// your logout handler
291-
});
292-
```
293-
294-
This will ignore whatever route is passed into the `logout()` method and rely solely on the function passed into the session.logout config.

src/docs/auth/update.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/docs/auth/user.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# User Operations
2+
3+
After successfully logging in or registering a user, Leaf Auth provides a bunch of functionality that you can use to manage your users. These range from updating user details to other CRUD operations.
4+
5+
## Getting user information
6+
7+
You can usually get the current user's information using the `data()` method, however, the output is an array of the user's data together with the tokens which is not very convenient for use within your application.
8+
9+
```php
10+
$data = auth()->data();
11+
// ['user' => [...], 'accessToken' => '...', 'refreshToken' => '...']
12+
```
13+
14+
The `user()` method allows you to pick out exactly the information you need from the user's data. If you need all the user's data, you can use the `get()` method:
15+
16+
```php
17+
$user = auth()->user()->get();
18+
19+
// or pick specific fields
20+
$email = auth()->user()->email;
21+
$username = auth()->user()->username;
22+
```
23+
24+
Picking specific fields also gives you access to fields you may have hidden using the auth config. This is useful because it allows you to perform operations on the user's data without mistakenly exposing hidden fields.
25+
26+
```php
27+
auth()->config('hidden', ['secret_field']);
28+
29+
$secretField = auth()->user()->secret_field;
30+
```
31+
32+
While this may seem like a lot of work, it's a good way to ensure that your user's data is secure and only accessible where needed.
33+
34+
## User relationships
35+
36+
Relationships are a way to connect different models in your application. For instance, a user may have many posts, or a user may have many transactions. Using relationships, you can fetch related data from your database. If you have a user model with a one-to-many relationship with a posts model, you can fetch the user's posts using the user object:
37+
38+
```php
39+
$posts = auth()->user()->posts();
40+
// will return a Leaf DB instance with posts by the current user
41+
// SELECT * FROM posts WHERE user_id = $current_user_id
42+
```
43+
44+
You can further filter the data by using any of the Leaf DB methods:
45+
46+
```php:no-line-numbers
47+
$posts = auth()->user()->posts()->where('title', 'like', '%leaf%')->get();
48+
```
49+
50+
You can do this by calling whatever table your user is related to as a method on the user object. For instance, if you want to grab all user transactions from the `transactions` table, you can call the `transactions()` method on the user object. If you want to grab all books your user has read from the `read_books` table, you can call the `readBooks()` method on the user object.
51+
52+
It will return a Leaf DB instance with the related data. You can further filter the data by using any of the Leaf DB methods:
53+
54+
```php:no-line-numbers
55+
$posts = auth()
56+
->user()
57+
->transactions()
58+
->where('amount', '>', 1000)
59+
->get();
60+
61+
$books = auth()
62+
->user()
63+
->readBooks()
64+
->where('title', 'Building with Leaf')
65+
->first();
66+
```
67+
68+
## Updating a logged-in user
69+
70+
Updating user information means allowing users to change their details (like username, email, or password) in your application. It is a very common feature in most applications.
71+
72+
Leaf provides an `update()` method that allows you to update a user's information. It returns `true` if the user is successfully updated and `false` if the user is not updated. You can get the error message using the `errors()` method.
73+
74+
```php
75+
$success = auth()->update($data);
76+
77+
if ($success) {
78+
// User is updated
79+
} else {
80+
// User is not updated
81+
$error = auth()->errors();
82+
}
83+
```
84+
85+
### Unique values
86+
87+
Leaf Auth allows you to set unique fields which should not be repeated for different users. For example, you wouldn't want two users to have the same email address. You can configure Leaf Auth to check for unique fields when a user is being updated:
88+
89+
```php:no-line-numbers
90+
auth()->config('unique', ['email', 'username']);
91+
```
92+
93+
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.
94+
95+
```php
96+
$success = auth()->update([
97+
'username' => 'example',
98+
'email' => '[email protected]'
99+
]);
100+
101+
if (!$success) {
102+
$error = auth()->errors();
103+
// ['email' => 'email already exists']
104+
}
105+
```
106+
107+
## Password reset
108+
109+
Leaf Auth doesn't have a full built-in password reset flow which involves sending an email to the user with a link to reset their password. This may be added in the future, but for now, Leaf Auth comes with a simple `updatePassword()` method that allows you to change a user's password. It takes care of verifying the old password and hashing the new password.
110+
111+
```php
112+
$success = auth()->updatePassword($oldPassword, $newPassword);
113+
114+
if ($success) {
115+
// Password is updated
116+
} else {
117+
// Password is not updated
118+
$error = auth()->errors();
119+
}
120+
```
121+
122+
## Signing a user out
123+
124+
When a user chooses to end their session, or their session expires, you can sign them out using the `logout()` method. This method will end the user's session or delete their token.
125+
126+
```php
127+
auth()->logout();
128+
129+
// or redirect the user after logout
130+
auth()->logout('/login');
131+
132+
// or redirect to a named route
133+
auth()->logout(['homepage']);
134+
```
135+
136+
If you want to perform a custom operation when a user logs out, you can set a handler for the logout operation:
137+
138+
```php
139+
auth()->config('session.logout', function () {
140+
// your logout handler
141+
});
142+
```
143+
144+
This will ignore whatever route is passed into the `logout()` method and rely solely on the function passed into the session.logout config.

0 commit comments

Comments
 (0)