0

I have an auth login page which logs in and redirects to home.blade.php

I need here to check whether user_type in user table is admin or Normal. If user_type=='admin' redirect to home else redirect to home_user page.

Route.php

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/home_user', 'HomeController@index')->name('home_user');

or

protected function redirectTo() {
        if (Auth::user()->user_type == 'admin')
            return '/home';
        else
            return '/home_user';
    }
2
  • You can add a middleware for this purpose Commented Sep 13, 2019 at 5:18
  • middleware in routes i am new to laravel so Commented Sep 13, 2019 at 5:20

4 Answers 4

2

As defined in the comment you can also use middleware but it could be the tricky way to do that. You can override the following function in the loginController.

protected function authenticated(Request $request, $user)
{
if (Auth::user()->user_type == 'admin') {// do your magic here
    return redirect('/home');
}

 return redirect('/home_user');
}

For the reference you can visit here

Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.
0
Below code, I have used for same. Try this.

 use Redirect;

 protected function authenticated($request, $user)
{
    if($user->is_admin == '1') {
        return Redirect::to('home');
    }elseif ($user->is_admin == '2') {
        Auth::logout();
        return Redirect::to('/admin/login');

    }else{
        return Redirect::to('studentDashboard');
    }

}

1 Comment

have you add use Redirect?
0

You have already solved the problem, but you have 1 mistake in your code. In laravel in order to redirect, you need to return redirect()->to() or redirect()->route() functions. all you need to do is replace your code with this:

protected function redirectTo() {
        if (auth()->user()->user_type == 'admin')
            return redirect()->route('home');
        else
            return redirect()->route('home_user');
}

5 Comments

I am getting error Header may not contain more than a single header, new line detected
@chaitra, where did you place this method(in which controller)?
in login contoller
Route::get('/home', 'HomeController@index')->name('home'); Route::get('/home_user', 'HomeController@index')->name('home_user'); i have given like this is it correct
both admin and normal going to same home page
0

Override the existing authenticated method from AuthenticatesUsers trait.

    /**
    * The user has been authenticated.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  mixed  $user
    * @return mixed
    */
    protected function authenticated(Request $request, $user)
    {
        if (Auth::user()->user_type == 'admin')
            return redirect('/home');
        else
            return redirect('/home_user');
    }

Reference: authenticated method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.