๐Ÿคน
TALL Stack Tips
  • What is TALL Stack
  • Tailwind
    • Tailwind Resources
    • Swinging Bell Notification Icon
    • Styled Unordered Lists
  • Alpine
  • Alpine Resources
  • Tabbed Content Using Alpine JS
  • Checkbox component with SVG tick
  • Dropdown animation
  • Create a Sliding Puzzle Captcha
  • Laravel
    • Tabler Icons Component
    • Password-less Login with Laravel 8+
    • Password-less Login with Magic Link in Laravel 8
    • Laravel Resources
    • Laravel Breeze Login Conditional Redirect
    • Jetstream Login Conditional Redirect
    • Simplify Laravel CRUD Controllers
    • CSRF and expired login forms
    • CSRF and expired logout forms
    • Add your own logo to Laravel Mail
    • Specify a different mail theme for Notifications
    • Show custom page when email verification link expired
    • Using a mutator to save currency
    • Using Spatie Valuestore to hold frequently accessed settings
    • Using the old() helper
    • Alternatives to using Eloquent Accessor
    • UpdateOrCreate may not update timestamp
    • Use of lockForUpdate()
    • Using S3
    • Super Simple User Based Feature Flags
    • Installing a Specific Version of Laravel
    • Versioning your Laravel Project
    • CSS Cache Busting with your Git Commit SHA
    • Adding column to Database Notifications table
    • Find nearby locations using the Haversine formula in Eloquent query
    • Using Queues on Shared Hosting with Laravel
    • Create Guaranteed Unique Invoice Number in Laravel
    • Send Notification to all team members
    • Protect Staging site with Basic Auth
    • Working with Enums
    • PHP DateTime formatting cribsheet
  • Livewire
    • Livewire Resources
    • Naming Livewire Components
    • Dynamic Cascading Dropdown with Livewire
    • Hiding a button after click
    • Working with Javascript Components
    • SweetAlert2 with Livewire
    • Select Multiple or Checkboxes
    • Clearing checkboxes in Livewire
    • Livewire File Uploads Using S3
    • Simple Log File Viewer
  • Related Resources
    • Testing resources
    • When Composer runs out of memory
    • Deployment
    • Security
    • Scheduler & Cron tips
    • LastPass tips
    • Using Git
    • VSCode Tips
    • Markdown
    • Cpanel resources
Powered by GitBook
On this page
  • Intended route
  • Fortify + Jetstream

Was this helpful?

  1. Laravel

Laravel Breeze Login Conditional Redirect

When you want to redirect a user after login according to their role, and using Laravel Breeze

โœ… Checked works with Laravel 10

When using Breeze and having users with different roles (eg customer / administrator), you might want to redirect the user once they have authenticated.

With Breeze this is fairly simple since the authentication process is performed in the user's App and can be easily modified.

Locate the file app/Http/Controllers/Auth/AuthenticatedSessionController.php

Replace the last line of the store() method with your redirect logic. For example;

    public function store(LoginRequest $request): RedirectResponse
    {
        $request->authenticate();

        $request->session()->regenerate();

        //return redirect()->intended(RouteServiceProvider::HOME);

        return redirect()->intended(
            auth()->user()->is_admin ? route('admin.dashboard') : route('dashboard')
        );
    }

In the example a ternary is used, testing the is_admin flag on the logged in user. Be sure to retain the `intended()` function since this serves a valuable purpose.

Intended route

The intended route is the name given to the place the user was trying to reach when they had to login. Imagine the situation; the user is logged in and looking at your application's dashboard or similar. They go away for a few hours then return and click on a menu item.

Since they are no longer logged in because their session expired, they can't go straight to the link and are redirected to the login page. The route that they were trying to reach is stored in session as the intended route. After logging in, if the session contains an intended route then they should be sent there.

The intended() function takes one parameter and this is a fallback route for when intended is not set.

When modifying the behaviour after login, its important to honor the intended route.

Fortify + Jetstream

Performing the same redirect on Jetstream is a little more involved. Check the following TallTips article: Jetstream Login Conditional Redirect

PreviousLaravel ResourcesNextJetstream Login Conditional Redirect

Last updated 1 year ago

Was this helpful?