Ad

Laravel Authenticated User Logout Error

- 1 answer

tried to implement a simple user register/login function on my site using the laravel default controllers (auth/password), but as soon as I login, the class RedirectIfAuthenticated handle function prevents all access to auth url's, thus I cannot logout anymore. Is there a bug and I need to write an exception on the handle function or have I missed something? Here is how the class looks like by default:

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        //dd($next($request));
        if (Auth::guard($guard)->check()) {
            return redirect('/articles');
        }

        return $next($request);
    }
}
Ad

Answer

The AuthController's constructor should look similar to this:

public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}

The guest middleware is handled by the RedirectIfAuthenticated class and, in order to have the logout functionality working, you should choose one:

  • call the logout method from your AuthController.
  • call whichever method you use for logout and exclude it in AuthController's constructor:

    public function __construct()
    {
        $this->middleware('guest', ['except' => '<whichever_method>']);
    }
    
Ad
source: stackoverflow.com
Ad