所以我有我的身份验证中间件,它注册Http/Kernel.php
为:
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
];
接下来我对 Authenticate 类中的中间件句柄函数进行了更改:
public function handle($request, Closure $next)
{
if ($this->auth->check()) {
$user = $this->auth->user();
$currentDateTime = strtotime('Y-m-d H:i:s');
$tokenExpirationTile = strtotime($user->token_expiration);
if ($currentDateTime <= $tokenExpirationTile) {
return $next($request);
} else {
$this->auth->logout();
redirect('home/login')->with('message', 'Your session has expired. Please login in again');
}
} else {
redirect('home/login')->with('message', 'Please login before attempting to access that');
}
}
最后我创建了路线:
Route::get('home/dashboard', 'HomeController@dashboard', ['middleware' => 'auth']);
我可以访问这条路线,但作为未登录用户,我应该被重定向。
当我通过函数dd()
中的 a 时,handle
什么也没有发生。
我如何让它在这条路线上运行这个方法?
此外,当涉及到需要在每个操作请求之前进行身份验证的其他控制器时,您怎么说:“在每个操作之前,运行此方法。” 在rails我会做before_action :method_name