Skip to content

Commit c21f406

Browse files
committed
feat: update auth middleware docs
1 parent a7bc6c2 commit c21f406

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/docs/auth/protected-routes.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,7 @@ app()->get('/protected', function () {
7373

7474
## Using Middleware
7575

76-
Leaf Auth also provides a middleware that you can use to protect your routes. The `auth` middleware checks if a user is logged in and allows you to set a callback function to run if a user is not logged in.
77-
78-
```php
79-
auth()->middleware('auth.required', function () {
80-
response()->redirect('/login');
81-
});
82-
```
83-
84-
Once you have defined a callback for the middleware, you can use it in your routes like this:
76+
Leaf Auth provides middleware to keep guest users out and logged in users in. This is a more flexible way to protect your routes and allows you to define more complex authentication logic. The `auth:required` middleware checks if a user is logged in and redirects to `/auth/login` if a user is not logged in.
8577

8678
```php
8779
app()->get('/protected', ['middleware' => 'auth.required', function () {
@@ -96,35 +88,47 @@ app()->group('/protected', ['middleware' => 'auth.required', function () {
9688
}]);
9789
```
9890

99-
If you use this method, the middleware will run before the route is executed. If the user is not logged in, the callback function you defined will be executed. This means you can remove the check for a logged in user from your route handler.
91+
The route or group of routes will only be accessible to logged in users, so you don't need to check if a user is logged in inside the route handler.
10092

101-
```php
102-
app()->get('/protected', ['auth.required', function () {
103-
$user = auth()->user();
93+
## Protected Guest Routes
10494

105-
// no need to check if user is logged in
95+
Just like the `auth.required` middleware, Leaf Auth provides a `auth.guest` middleware to protect routes that should only be accessible to guest users. This is useful for routes like the login and register routes.
96+
97+
```php
98+
app()->get('/login', ['middleware' => 'auth.guest', function () {
99+
// this route is only accessible to guest users
106100
}]);
107101
```
108102

109-
## Protected Guest Routes
103+
If a logged in user tries to access a route protected by the `auth.guest` middleware, they will be redirected to the `/dashboard` route by default.
110104

111-
You can also protect routes that should only be accessible to guest users. This is useful for routes like the login and register routes. You can use the `auth.guest` middleware to protect these routes.
105+
## Customizing auth middleware
106+
107+
Your application may need you to return different responses for the `auth.required` and `auth.guest` middleware. You can customize the middleware by defining your own function that should be called when the middleware fails.
112108

113109
```php
110+
auth()->middleware('auth.required', function () {
111+
response()->exit('You need to be logged in to access this route');
112+
});
113+
114114
auth()->middleware('auth.guest', function () {
115-
response()->redirect('/dashboard');
115+
response()->exit('You are already logged in');
116116
});
117117
```
118118

119-
You can then use this middleware on your guest routes like this:
119+
After defining the custom middleware, you can use it in your routes.
120120

121121
```php
122+
app()->get('/protected', ['middleware' => 'auth.required', function () {
123+
// this route is protected
124+
}]);
125+
122126
app()->get('/login', ['middleware' => 'auth.guest', function () {
123127
// this route is only accessible to guest users
124128
}]);
125129
```
126130

127-
This middleware will run before the route is executed. If a user is logged in, the callback function you defined will be executed. This means you can remove the check for a guest user from your route handler.
131+
You only need to define the custom middleware if the default behavior of the `auth.required` and `auth.guest` middleware does not meet your requirements.
128132

129133
## Session Guards <Badge type="danger" text="DEPRECATED" />
130134

0 commit comments

Comments
 (0)