Skip to content

Commit 0f50bad

Browse files
committed
feat: add auth model docs
1 parent 9f0dae0 commit 0f50bad

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

src/docs/auth/login.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ auth()->config('session.cookie', [
121121
]);
122122
```
123123

124-
## Signing up from OAuth
124+
## Signing in from OAuth
125125

126126
Some applications only allow users to sign in using OAuth which means there's no need for users to add emails or passwords. Leaf Auth provides the `fromOAuth()` function which allows you to create a session or token for a user without needing a password.
127127

@@ -160,6 +160,15 @@ $user = auth()->user();
160160

161161
Everything after this point is the same as signing up a user normally.
162162

163+
::: info OAuth Token
164+
The `fromOAuth()` method expects an OAuth token to be passed in. This token is usually gotten from the OAuth provider you are using. You can later use this token to make requests to the OAuth provider on behalf of the user. Leaf Auth saves this token so you can retrieve it later using the `auth()->oauthToken()` method.
165+
166+
```php
167+
$token = auth()->oauthToken();
168+
```
169+
170+
:::
171+
163172
## Auth with no password
164173

165174
Leaf Auth usually expects a password field to authenticate users. This is necessary because most applications require a password to authenticate users. The field is usually named `password`, however, you can configure Leaf Auth to expect a different field:

src/docs/auth/user.md

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,34 @@ While this may seem like a lot of work, it's a good way to ensure that your user
3333

3434
## User relationships
3535

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:
36+
Leaf auth comes with a very basic model system that allows you to get/set data related to the current user. For instance, you may want to get all posts by the current user or all transactions by the current user, or maybe add a new purchase to the current user. All these can be done using the user method.
3737

3838
```php
39-
$posts = auth()->user()->posts();
39+
$posts = auth()->user()->posts()->get();
4040
// will return a Leaf DB instance with posts by the current user
4141
// SELECT * FROM posts WHERE user_id = $current_user_id
4242
```
4343

44-
You can further filter the data by using any of the Leaf DB methods:
44+
If you want to relate a user to a different table, 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 posts from the `posts` table, you can call the `posts()` method on the user object. If you want to grab all user transactions from the `transactions` table, you can call the `transactions()` method on the user object. Once you call the method, it will return a Leaf DB instance which has already been filtered by the user's ID.
45+
46+
```php
47+
$purchases = auth()->user()->purchases();
48+
// will return a Leaf DB instance with purchases by the current user
49+
50+
$purchases->get();
51+
$purchases->first();
52+
...
53+
```
54+
55+
### Filtering user relationships
56+
57+
Since a Leaf DB instance is returned, you can further filter the data by using any of the Leaf DB methods:
4558

4659
```php:no-line-numbers
4760
$posts = auth()->user()->posts()->where('title', 'like', '%leaf%')->get();
4861
```
4962

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:
63+
Here are some common examples:
5364

5465
```php:no-line-numbers
5566
$posts = auth()
@@ -65,6 +76,69 @@ $books = auth()
6576
->first();
6677
```
6778

79+
### Creating related data
80+
81+
If you want to add new data to a database table which should be related to the current user, you can call the table name as a method on the user object and then call the `create()` method on the returned Leaf DB instance.
82+
83+
```php
84+
auth()->user()->posts()->create([
85+
'title' => 'My first post',
86+
'content' => 'This is my first post'
87+
]);
88+
```
89+
90+
This will create a new post in the `posts` table with the `user_id` set to the current user's ID.
91+
92+
### Updating related data
93+
94+
If you want to update data related to the current user, you can call the table name as a method on the user object and then call the `update()` method on the returned Leaf DB instance.
95+
96+
```php
97+
auth()
98+
->user()
99+
->purchases()
100+
->update([
101+
'status' => 'completed',
102+
])
103+
->execute();
104+
```
105+
106+
You can also further filter the data before updating it:
107+
108+
```php
109+
auth()
110+
->user()
111+
->purchases()
112+
->update([
113+
'status' => 'completed',
114+
])
115+
->where('status', 'pending')
116+
->execute();
117+
```
118+
119+
### Deleting related data
120+
121+
If you want to delete data related to the current user, you can call the table name as a method on the user object and then call the `delete()` method on the returned Leaf DB instance.
122+
123+
```php
124+
auth()
125+
->user()
126+
->purchases()
127+
->delete()
128+
->where('status', 'cancelled')
129+
->execute();
130+
```
131+
132+
You can also delete all related data:
133+
134+
```php
135+
auth()
136+
->user()
137+
->purchases()
138+
->delete()
139+
->execute();
140+
```
141+
68142
## Updating a logged-in user
69143

70144
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.

0 commit comments

Comments
 (0)