I can't logout with laravel

Viewed 402

I'm working on my first laravel project in which I made an admin panel to make admin able to controller the website. I tried to login admin into dashboard and everything was fine, when I tried to log him out nothing works and the page was just reloading and redirect back.

Here is my logout button:

<a class="navbar-brand btn btn-primary btn-lg"
 href="{{route('admin.logout'))}}"> log out</a>

My route:

 Route::get('logout', 'LoginController@logout')->name('admin.logout');

And my controller:

use Illuminate\support\Facades\Auth;


  public function logout(Request $request){
     
       Auth::logout();

        return redirect()->route('get.admin.login')
        ->with(['success'=>'logged out successfully']);
    }

I used Auth::logout(); method from laravel official documentation, however it didn't work. I hope I can find help.

3 Answers

as your using Auth::logout()inside that function

public function logout(Request $request) {
    Auth::logout(); 
    return redirect('/login');
}

here Auth:: will be null so apply middleware to get Auth instance and then you can logout

Route::get('logout', 'LoginController@logout')->name('admin.logout')->middleware('auth');

You can use the following in your controller:

return redirect('login')->with(Auth::logout());

You can use below code, for logout the admin.

Auth::logout(); 
return redirect('/login');
Related