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
Copy file name to clipboardExpand all lines: src/docs/auth/protected-routes.md
+23-19Lines changed: 23 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,15 +73,7 @@ app()->get('/protected', function () {
73
73
74
74
## Using Middleware
75
75
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.
85
77
86
78
```php
87
79
app()->get('/protected', ['middleware' => 'auth.required', function () {
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.
100
92
101
-
```php
102
-
app()->get('/protected', ['auth.required', function () {
103
-
$user = auth()->user();
93
+
## Protected Guest Routes
104
94
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
106
100
}]);
107
101
```
108
102
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.
110
104
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.
112
108
113
109
```php
110
+
auth()->middleware('auth.required', function () {
111
+
response()->exit('You need to be logged in to access this route');
112
+
});
113
+
114
114
auth()->middleware('auth.guest', function () {
115
-
response()->redirect('/dashboard');
115
+
response()->exit('You are already logged in');
116
116
});
117
117
```
118
118
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.
120
120
121
121
```php
122
+
app()->get('/protected', ['middleware' => 'auth.required', function () {
123
+
// this route is protected
124
+
}]);
125
+
122
126
app()->get('/login', ['middleware' => 'auth.guest', function () {
123
127
// this route is only accessible to guest users
124
128
}]);
125
129
```
126
130
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.
0 commit comments