ihprince's avatar

How to make custom login only for admin in laravel?

I use laravel ui for authentication. Now, I want to custom login where only the admin can log in, otherwise, the user will be redirected to the user's login page. In the user's table one column name "is_admin". If is_admin == 1 then returns to admin panel otherwise return to user's login page. Here is my custom login method

public function adminAuth(Request $request) {

    $request->validate([
        'email'    => 'required',
        'password' => 'required',
    ]);
    $credentials = $request->only('email', 'password');
    if (Auth::attempt($credentials)) {
       // what goes here?
    }
    return redirect()->back()->with('error', 'Oppes! You have entered invalid credentials');
} 

Now, How to do that what I want?

1 like
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@ihprince Just apply a condition insider if.

if (Auth::attempt($credentials)) {
       	if(Auth::user()->is_admin !== 1){
       		// Now redirect to the user's login page
       	}

       	// Now redirect to the admin panel
    }
4 likes

Please or to participate in this conversation.