Redirect admin and users to different pages after login – Larvel 5.3

I am using Laravel 5.3. and I am using the authentication which comes out of the box with this version.

I needed to differentiate between different user roles when someone logs in to send them to appropriate pages relevant to their role.

In my users table I have a ‘role’ column for the sake of ease of use.

This was my solution for my needs.

Locate this file: App\Http\Controllers\Auth\LoginController.php

Add this method below the __construct

public function authenticated()
    {
        if(isset(Auth::user()->role))
        {
            if (Auth::user()->role == Constants::ROLE_ADMINISTRATOR)
            {
                return redirect('/admin');
            }
           
            return redirect('/guest');
            
        }
    }

Be sure to include the Auth class. e.g.

use Illuminate\Support\Facades\Auth;

Note: the ‘Constants::ROLE_…” are constants I have in my code.  You may just have a number e.g. 1 for admin and 2 for guest. So it could read (Auth::user()->role == 1 )

An admin will now be directed to the ‘/admin’ page while a guest will be directed to the ‘/guest’ page after they login.

Tips or suggestions. Let us know in the comments.

One thought on “Redirect admin and users to different pages after login – Larvel 5.3”

Leave a Reply

Your email address will not be published. Required fields are marked *