我创建了一个自定义的后卫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');
}
}
}
AuthenticatesUsers.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');
}
作为一个新手,我面临同样的问题了。我补充说,注销功能如下分解成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>
Try This:
public function guard() {
return Auth::guard('customer');
}
public function logoutcustomer(){
$this->guard()->logout();
return redirect('/');
}