当我使用错误的凭据登录时,我得到了正确的回复。 当我使用正确的凭据登录时,登录页面将重新加载302请求 但它永远不会重定向到统计页面。 当我调试它时,我发现代码转到中间件文件夹中的authinticate.php, 它重定向到访客登录状态
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
查看代码: -
Route.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');
});
登录控制器
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'); }
}
统计控制器
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 auth库进行静态身份验证。
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'
];
中间件/ 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);
}
}
答案 0 :(得分:3)
检查你的缓存。我有类似的问题,我丢失了几个小时,所以这些我已经做了一些步骤:
答案 1 :(得分:1)
快速分析:
您的身份验证方法或控制器没有问题。
问题在于您没有“ / statistics ”的路线
至于Laravel至少从第5版开始,你必须明确你的路线“PS:他们弃用了Route :: Controller()”
顺便说说
Route::get('/', 'StatisticsController@index');
参考您的应用程序基础路径
<强>解决方案强> 添加统计信息路径
Route::get('/statistics', 'StatisticsController@statistics');
例如。
答案 2 :(得分:1)
您正在重定向到StatisticsController @ statistics,但您的StatisticsController中没有定义统计函数。