1
votes

When I try to logout from frontpage I'm redirected to admin dashboard if I'am ussing an admin user, if i use an normal user I'm redirected to user account page, I can logout only from admin dashboard, here is my logout function:


public function __construct()
    {
        $this->middleware('guest')->except(['logout', 'destroy']);
    }
public function logout(Request $request)
    {   
        $this->guard()->logout();
        $request->session()->invalidate();

        return $this->loggedOut($request) ?: redirect()
            ->route('login')
            ->with('message', 'You have been logged out');
    }

Middleware Authenticate


protected function redirectTo($request)
    {
        if (!$request->expectsJson()) {
            return route('login');
        }
    }

Middleware RedirectIfAuthenticated


public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            if (Auth::user()->hasRole('administrator')) {
               return redirect()->route('admin.dashboard.index');
            } else {
                return redirect()->route('account');
            }
        }
        return $next($request);
    }

Middleware CheckRole


public function handle($request, Closure $next, $role = 'administrator')
    {

        if (Auth::check()) {
            if (Auth::user()->hasRole($role)) {
                return $next($request);
            }
            abort('404');
        } else {
            return redirect()->route('admin.auth.login');
        }

        return $next($request);
    }

routes.php


Route::get('login', 'Auth\LoginController@showLoginForm')
                                        ->name('login')
                                        ->middleware('web');
Route::post('login', 'Auth\LoginController@login')
                                        ->name('login')
                                        ->middleware('web');
Route::get('logout', 'Auth\LoginController@logout')
                                        ->name('logout')
                                        ->middleware('auth');

$this->loggedOut($request) is from Illuminate\Foundation\Auth\AuthenticatesUsers;

public function logout(Request $request)
    {
        $this->guard()->logout();

        $request->session()->invalidate();

        return $this->loggedOut($request) ?: redirect('/');
    }

    /**
     * The user has logged out of the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return mixed
     */
    protected function loggedOut(Request $request)
    {
        //
    }


1
If you are using a single session and auth provider, you can indeed not do this.Jerodev

1 Answers

0
votes

I found the problem, was from my account route the middleware there was web, so that is why i can't logout. I have added to all routes web middleware and now is working.:)