|
| 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 | + |
| 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