Multiple Authentication in laravel 6x

github logo ・1 min read

i have installed laravel default authentication system (using php artisan ui react --auth) which is best for me but i have created different table for user role and make relationship between user and role table and create new table called role_user but i am unable to redirect a different page based on user role in roles table. So, my question is how to redirect pages for different user based on role? I couldn't change the default /home page, in my condition all user role redirected to same /home page.

twitter logo DISCUSS (2)
markdown guide
 

the easiest way to do this to use the authenticated event which is fired after the authentication take place.

in you LoginController add

   /**
     * @param Request $request
     * @param $user
     * @return RedirectResponse
     */
    protected function authenticated(Request $request, $user)
    {
        if ($user->role === "ADMIN") {
            return redirect('/admin-page');
        }

        return redirect('/user-page');
    }
 

hey, I had a similar problem and just put an if condition in the handle function of the Middleware/Admin.php file. So something like this:

public function handle($request, Closure $next)
{

if(auth()->user()->role === "ADMIN") {
return redirect('/Adminpage');
}
else{
return $next($request);
}
}
With that every "ADMIN" will be redirected to the /Adminpage (you still need to correctly implement the route)and everyone else to your default /home page. There for I'm new to Laravel 6 I don't know if that is good or bad practice, but at least it works. Maybe someone more experienced could help us with that but for now, I hope my solution approach helps you :)

happy coding,
lodric

Classic DEV Post from Feb 13

How Do I Start Giving Talks on Coding?

I don't consider myself an expert in just about any aspect of computer science ...

Saroz Kumar profile image
My Name is Saroj Kumar Shrestha, a Web Developer love to develop new ideas.

Signed up yet?

DEV is one of the most important resources in software.

Get Started