Concept Flow - Authentication guards
User sends request
Guard checks if user is logged in
Yes
Allow access
Controller handles request
The guard checks if the user is authenticated before allowing access or redirecting to login.
<?php // In routes/web.php Route::get('/dashboard', function () { return 'Welcome to dashboard'; })->middleware('auth');
| Step | Request URL | Guard Check | User Authenticated? | Action Taken | Response |
|---|---|---|---|---|---|
| 1 | /dashboard | Check auth guard | No | Redirect to /login | Redirect response |
| 2 | /login | No guard needed | N/A | Show login form | Login form HTML |
| 3 | User submits login form | Validate credentials | Yes | Set user session | Redirect to /dashboard |
| 4 | /dashboard | Check auth guard | Yes | Allow access | Show dashboard content |
| Variable | Start | After Step 1 | After Step 3 | After Step 4 |
|---|---|---|---|---|
| user_authenticated | false | false | true | true |
| response | none | redirect to /login | redirect to /dashboard | dashboard content |
Authentication guards check if a user is logged in before allowing access.
If not authenticated, user is redirected to login page.
Use middleware('auth') on routes to apply guard.
After login, guard allows access to protected routes.
Guards help protect pages from unauthorized users.