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
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.
127
127
@@ -160,6 +160,15 @@ $user = auth()->user();
160
160
161
161
Everything after this point is the same as signing up a user normally.
162
162
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
+
163
172
## Auth with no password
164
173
165
174
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:
Copy file name to clipboardExpand all lines: src/docs/auth/user.md
+80-6Lines changed: 80 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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
33
33
34
34
## User relationships
35
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:
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.
37
37
38
38
```php
39
-
$posts = auth()->user()->posts();
39
+
$posts = auth()->user()->posts()->get();
40
40
// will return a Leaf DB instance with posts by the current user
41
41
// SELECT * FROM posts WHERE user_id = $current_user_id
42
42
```
43
43
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:
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:
53
64
54
65
```php:no-line-numbers
55
66
$posts = auth()
@@ -65,6 +76,69 @@ $books = auth()
65
76
->first();
66
77
```
67
78
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
+
68
142
## Updating a logged-in user
69
143
70
144
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