我的登錄控制器
public function authenticate(Request $request)
{
$credentials = $request-> validate([
'username' => ['required']。
'password' => ['required']。
]);
if (Auth:: attempt($credentials)) {
$request->session()-> regenerate()。
return redirect()-> intended('/')。
}
return back()->withErrors([
'error' => 'The provided credentials do not match our records.' ,
]);
}
我檢查認證的中間件
class AuthorizeUser
{
/**。
*處理一個傳入的請求。
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (Auth::check() ) {
return $next($request)。
} else {
return view('application')。
}
}
}
Auth::check() 在這個中間件中,Auth:: attempt回傳false事件。
uj5u.com熱心網友回復:
檢查你的用戶資料庫的主鍵。如果你設定的表的主鍵不是id,你需要在你的用戶模型中設定它。
Laravel檔案中提到:
Eloquent也會假設每個表都有一個名為id的主鍵列。你可以定義一個protected $primaryKey屬性來覆寫這個約定。
例如,如果你在用戶資料庫中設定了一個user_id列作為主鍵,你需要在User模型中放置以下代碼:
protected $primaryKey = 'user_id' ;
uj5u.com熱心網友回復:
你在auth.php中使用了任何防護措施嗎? 這是你默認的防護措施嗎?
'defaults' => [
'guard' => 'web'。
'passwords' => 'user',
],
uj5u.com熱心網友回復:
把你的路由放在Auth或Web的中間件中,那么它將會作業。
你可以在routes/api.php中使用這段代碼
。 Route::post('your-route-here', [ControllerNameHere:: class,'FunctionNameHere'])->name('Route-Name-Here')->middleware('api')。
或者你可以在routes/web.php中使用
。 Route::post('your-route-here', [ControllerNameHere:: class,'FunctionNameHere'])->name('Route-Name-Here')->middleware('web')。
uj5u.com熱心網友回復:
使用RedirectIfAuthenticated中間件代替:
class RedirectIfAuthenticated
{
/**。
*處理一個傳入的請求。
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @param string|null ...$guards
* @return mixed
*/
public function handle(Request $request, Closure $next, 。 ...$guards)
{
$guards = empty($guards) ? [null] : $guards;
foreach ($guards as $guard) {
if (Auth::guard($guard)->check() ) {
return redirect(RouteServiceProvider::HOME)。
}
}
return $next($request)。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/333692.html
標籤:
