How to logout and redirect to login page using Laravel 5.4?

前端 未结 11 1885
天命终不由人
天命终不由人 2020-12-23 08:58

I am using Laravel 5.4 and trying to implement authentication system. I used php artisan command make:auth to setup it. I edited the views according to my layout. Now, when

相关标签:
11条回答
  • 2020-12-23 09:43

    here is another way to do it by calling Auth::logout() in route

    Route::get('/logout', function(){
       Auth::logout();
       return Redirect::to('login');
    });
    
    0 讨论(0)
  • 2020-12-23 09:44

    Best way for Laravel 5.8

    100% worked

    Add this function inside your Auth\LoginController.php

    use Illuminate\Http\Request;
    

    And also add this

    public function logout(Request $request)
    {
        $this->guard()->logout();
    
        $request->session()->invalidate();
    
        return $this->loggedOut($request) ?: redirect('/login');
    }
    
    0 讨论(0)
  • 2020-12-23 09:46

    Well even if what suggest by @Tauras just works I don't think it's the correct way to deal with this.

    You said you have run php artisan make:auth which should have also inserted Auth::routes(); in your routes/web.php routing files. Which comes with default logout route already defined and is named logout.

    You can see it here on GitHub, but I will also report the code here for simplicity:

        /**
         * Register the typical authentication routes for an application.
         *
         * @return void
         */
        public function auth()
        {
            // Authentication Routes...
            $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
            $this->post('login', 'Auth\LoginController@login');
            $this->post('logout', 'Auth\LoginController@logout')->name('logout');
            // Registration Routes...
            $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
            $this->post('register', 'Auth\RegisterController@register');
            // Password Reset Routes...
            $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
            $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
            $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
            $this->post('password/reset', 'Auth\ResetPasswordController@reset');
        }
    

    Then again please note that logout requires POST as HTTP request method. There are many valid reason behind this, but just to mention one very important is that in this way you can prevent cross-site request forgery.

    So according to what I have just pointed out a correct way to implement this could be just this:

    <a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('frm-logout').submit();">
        Logout
    </a>    
    <form id="frm-logout" action="{{ route('logout') }}" method="POST" style="display: none;">
        {{ csrf_field() }}
    </form>
    

    Finally note that I have inserted laravel out of the box ready function {{ csrf_field() }}!

    0 讨论(0)
  • 2020-12-23 09:52

    In your web.php (routes):

    add:

    Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
    

    In your LoginController.php

    add:

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

    Also, in the top of LoginController.php, after namespace

    add:

    use Auth;
    

    Now, you are able to logout using yourdomain.com/logout URL or if you have created logout button, add href to /logout

    0 讨论(0)
  • 2020-12-23 09:54

    In Laravel 6.2

    Add the following route to : web.php

    Route::post('logout', 'Auth\LoginController@logout')->name('logout');
    

    Using Achor tag with logout using a POST form. This way you will also need the CSRF token.

     <a class="log-out-btn" href="#" onclick="event.preventDefault();document.getElementById('logout-form').submit();"> Logout </a>
    
     <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
              {{ csrf_field() }}
      </form>
    
    0 讨论(0)
提交回复
热议问题