Hello to all, welcome to therichpost.com. In this post, I will tell you, Laravel 7 – how to redirect authenticated users based on user type?

Post Working:

In this my laravel 7 application, I have used laravel default auth and I have added new column ‘ usertype’ into my users table and I am redirecting to login user based on usertype.

Here is the code snippet and please use in your laravel project app/Http/Controllers/Auth/LoginController.php

use Auth;

class LoginController extends Controller
{
...
protected function authenticated(Request $request)
    {
      $usertype = Auth::user();
      if($usertype->usertype == "admin")
      {
        $redirect = '/admin/dashboard/';
      }
      if($usertype->usertype == "superadmin")
      {
        $redirect = '/superadmin/dashboard/';
      }
      return redirect($redirect);
    }
...
}

 

This is it and if you have any kind of query related to this then please do comment below.

Jassa

Thank you

By therichpost

Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

2 thoughts on “Laravel 7 – how to redirect authenticated users based on user type?”
  1. protected function authenticated(Request $request)
    {
    return redirect(
    [
    ‘admin’ => ‘/admin/dashboard/’,
    ‘superadmin’ => ‘/superadmin/dashboard/’
    ][ auth()->user()->userType ]
    );
    }

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.