gpt4 book ai didi

laravel - 身份验证重定向失败

转载 作者:行者123 更新时间:2023-12-02 15:57:25 25 4
gpt4 key购买 nike

当我使用错误的凭据登录时,我得到了正确的响应。当我使用正确的凭据登录时,登录页面会重新加载 302 请求但它永远不会重定向到统计页面。当我调试它时,我发现代码进入了中间件文件夹中的authhinticate.php,它重定向到访客登录状态

if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}

查看代码:-

路线.php

Route::get('login', 'LoginController@index');
Route::post('signin', 'LoginController@signin');
Route::get('signout', 'LoginController@signout');


Route::group(['prefix' => 'api'], function() {
Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
Route::post('authenticate', 'AuthenticateController@authenticate');
});

Route::group(['middleware' => ['web']], function () {
Route::auth();
Route::get('/', 'StatisticsController@index');
Route::get('/statistics', 'StatisticsController@statistics');

});

登录 Controller

    public function index() {

return view('login');
}

public function signin(Request $request) {

$errors = [];
$email=$request['email'];
$password= $request['password'];
$credentials = array('email' => $email, 'password' => $password);

if(Auth::attempt($credentials))
{
return redirect('/statistics');


}
return "bad request";

}
public function signout()
{
Auth::logout();
return redirect('/login'); }

}

统计 Controller

class StatisticsController extends Controller {
public function __construct()
{
$this->middleware('auth');

}
public function index() {

return view('statistics')->with($data);
}

public function statistics() {
return view('statistics');

}



}

Kernal.php 请注意,有 JWT 身份验证库,我仅使用它对移动应用程序进行静态身份验证。

protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];

protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],

'api' => [
'throttle:60,1',
],
];

protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'jwt.auth' => 'Tymon\JWTAuth\Middleware\GetUserFromToken',
'jwt.refresh' => 'Tymon\JWTAuth\Middleware\RefreshToken'
];

middleware/authenticate.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class Authenticate
{

public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}

return $next($request);
}
}

最佳答案

检查您的缓存。我遇到了类似的问题,我损失了几个小时,所以这些是我所做的一些步骤:

  • php artisan 路线:清除
  • 清除浏览器缓存
  • 运行 Composer 更新
  • 下载 laravel(新项目)的新副本,然后慢慢地将代码块合并到新项目中

关于laravel - 身份验证重定向失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35965509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com