2
votes

I am using Laravel's native auth module for login and using entrust for user roles.

When user logs in, laravel's native auth module only checks for username/email and password. Then logs the user in. I have put a check if user is logged in with a certain user_type (user_type is also posted) and user has that role, then user is sent to that role's dashboard otherwise user is logged out by Auth::logout and redirected to login page with wrong user_type error.

All code works except when user_type is wrong, then it shows blank login page. I have tried a lot of combinations for redirection but no luck.

My code for logout and redirect is

protected function logoutRedirect(Request $request)
{
    Auth::logout();

    return redirect($this->loginPath())
        ->withInput($request->only($this->loginUsername(), 'remember'))
        ->withErrors([
            $this->loginRole() => $this->getWrongRoleMessage(),
        ]);

    /*
    return view('auth.login')
        ->withInput($request->only($this->loginUsername(), 'remember'))
        ->withErrors($this->getWrongRoleMessage());
    */

    /*
    return redirect('/auth/login')->withErrors([$this->loginRole() => $this->getWrongRoleMessage()]);
    */
}

I have placed all the redirect methods i could try in the above code in form of comments. If you can find the errors or if you can propose a better solution please let me know. I am new at laravel so I would appreciate if you help me solve this beginners problem.

PS: by the way I am using Laravel 5.1 and on localhost (xampp + MS windows 7)

1
Try this return Redirect::to('login')->withInput(Input::except('password'))->withErrors([$this->loginRole() => $this->getWrongRoleMessage(),]);. Remember to use Redirect Facade.karmendra
@karmendra yes I tried your code but it does not work.Afzaal Ahmad

1 Answers

1
votes

Finally I found the solution accidentally by searching solution for some other thing. Surprisingly this is not mentioned anywhere in any forum.

Here is the code which takes me back with errors.

redirect()->back()->withErrors([$this->loginRole() => $this->getWrongRoleMessage()]);