當我用錯誤的憑據登錄我得到了正確的迴應。 當我用正確的憑證登錄登錄頁面重新加載302請求 但它從不重定向到統計頁面。 當我調試它,我發現代碼進入這個authinticate.php在中間件文件夾, 重定向到來賓登錄的狀態身份驗證重定向失敗
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
看到代碼: -
路線。 PHP
Route::get('login', '[email protected]');
Route::post('signin', '[email protected]');
Route::get('signout', '[email protected]');
Route::group(['prefix' => 'api'], function() {
Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
Route::post('authenticate', '[email protected]');
});
Route::group(['middleware' => ['web']], function() {
Route::auth();
Route::get('/', '[email protected]');
Route::get('/statistics', '[email protected]');
});
登錄控制器
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請注意,有智威湯遜權威性庫我用它的寧靜認證,只有移動應用。
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);
}
}
我所有其他的股票Laravel 5.2應用程序?'app/Http/Middleware/Authenticate.php'沒有變化嗎?那麼'app/Http/Controllers/Auth'中的控制器呢? 你也可以在你的問題中清理你的英語嗎?至少要添加一些標點符號 - 很難跟隨一個場景結束和下一個開始。 – tremby
問題不清楚! –
你能告訴我\ App \ Http \ Middleware \ Authenticate文件嗎? –