我在laravel中创建了一个自定义防护,自定义防护存在的问题是,当我在登录页面上选中“记住我”功能时,我可以登录,但是当我要注销时,我无法终止会话。我知道问题出在退出功能上,但不确定如何解决此问题。我尝试将注销功能复制到authenticatesuser.php
中,但仍然无法终止会话并注销。
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
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>
网络路线
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');
}
答案 0 :(得分:1)
作为一个新手,我也面临着同样的问题。我将下面的注销功能添加到LoginController.php文件中。为每个警卫增加一条新的线解决了我的问题。我确信有更好的方法可以将这两个守卫一起编写,但这是我能做的。
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>
答案 1 :(得分:0)
Try This:
public function guard() {
return Auth::guard('customer');
}
public function logoutcustomer(){
$this->guard()->logout();
return redirect('/');
}