简体   繁体   English

laravel-如何在自定义卫士上记住我后注销会话?

[英]laravel -how to logout session after checking remember me on a custom guard?

I created a custom guard in laravel the problem i am having with the custom guard is that when i check the remember me function on the login page, i can login in but when i want to sign out i can not terminate the session. 我在laravel中创建了一个自定义防护,自定义防护存在的问题是,当我在登录页面上检查“记住我”功能时,我可以登录,但是当我要注销时,我无法终止会话。 I know that the problem is in the logout functionality but not sure how to fix the issue. 我知道问题出在退出功能上,但不确定如何解决此问题。 I tried to copy the logout function in the authenticatesuser.php but still wasn't able to terminate the session and log out. 我试图将注销功能复制到authenticatesuser.php但仍然无法终止会话并注销。

logincontroller 的LoginController

<?php

namespace App\Http\Controllers\CustomerAuth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function guard()
    {
        return Auth()->guard('customer');
    }

    public function logoutcustomer()
    {
    $customer =  Auth::guard($customer)->logout();
        $customer->session()->invalidate();
        return redirect('/');
    }



    public function showLoginForm()
    {
        if (Auth::user() || Auth::guard('customer')->user()) {
            return redirect('/');
        } else {
            return view('customer-auth.login');
        }
    }
}

AuthenticatesUser.php AuthenticatesUser.php

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

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

    return redirect('/');
}

//navbar customer

<a class="dropdown-item" href="{{ route('customer.logout') }}" onclick="event.preventDefault();
                                                 document.getElementById('logout-form').submit();">
                                    {{ __('Logout') }}
                                </a>
                        <form id="logout-form" action="{{ route('customer.logout') }}" method="POST" style="display: none;">
                            @csrf
                        </form>

web route 网络路线

Route::group(['prefix'=> 'customer'], function () {
// Login Routes...
    // Route::get('login', ['as' => 'customer.login', 'uses' => 'CustomerAuth\LoginController@showLoginForm']);

    Route::post('login', [ 'uses' => 'CustomerAuth\LoginController@login'])->name('customer.login');
    // Route::post('logout', ['as' => 'customer.logout', 'uses' => 'CustomerAuth\LoginController@logoutcustomer']);
    Route::post('logout', 'CustomerAuth\LoginController@logoutcustomer')->name('customer.logout');

}

As a newbie, I was facing the same problem too. 作为一个新手,我也面临着同样的问题。 I added that logout function down below into LoginController.php file. 我将下面的注销功能添加到LoginController.php文件中。 Adding a new line for each guard solved my problem. 为每个警卫添加新的行解决了我的问题。 I'm sure there is better way to write those two guards together, but this is what I can do. 我敢肯定有更好的方法可以将这两个守卫一起编写,但这就是我能做的。

public function logout(Request $request)
    {
        Auth::guard('customer')->logout();
        Auth::guard('web')->logout();
        $request->session()->flush();
        $request->session()->regenerate();
        return redirect('/');
    }
<li class="nav-item">
            <a
              class="nav-link logout"
              href="{{ route('logout') }}"
              onclick="event.preventDefault();
                       document.getElementById('logout-form').submit();">
                       <i class="fas fa-power-off"></i>
                       <p>{{ __('Logout') }}</p>
            </a>
            <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                @csrf
            </form>
          </li>
    Try This:

   public function guard() {   
      return Auth::guard('customer');
    }

    public function logoutcustomer(){
       $this->guard()->logout();
       return redirect('/');
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM