如何编辑Laravel护照令牌的expires_at字段 [英] How to edit laravel passport token's expires_at field
问题描述
我正在尝试使用像下面这样的middleware
编辑token
的expires_at
字段
I am trying to edit expires_at
field of my token
with a middleware
like bellow
namespace App\Http\Middleware;
use Carbon\Carbon;
use Closure;
use Auth;
class Refresh
{
/**
* @param $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$token = Auth::user()->token();
$expiresDate = $token->expires_at;
$currentDate = Carbon::now();
$diff = date_diff_in_minutes($currentDate, $expiresDate);
$baseExpire = site_config('token_expires_minutes');
if ($diff > 0 && $diff < $baseExpire) {
$token->update([
'expires_at' => (new Carbon($expiresDate))->addMinutes(intval($baseExpire - $diff))
]);
}
return $next($request);
}
}
在每个api请求中
更新令牌的expires_at
字段都可以正常工作.
Updating token's expires_at
field in each api requests works fine.
但是令牌在第一个expires_at
日期到期.
But the token expires at the first expires_at
date.
我知道有一种refresh_token
方法可以用于此目的,但是刷新令牌存在一些问题,因此我不得不在不撤销令牌的情况下增加令牌的使用寿命.
I know there is a refresh_token
method that i can use for this but i have some issues with refresh tokens and because of that i have to increase token's life time without revoking.
您知道为什么会这样吗?我该如何解决?
Do you know why this happening? How can i fix this?
推荐答案
根据 docs ,应在AuthServiceProvider.php
中通过调用boot()
方法中的Passport::tokensExpireIn()
函数来设置默认令牌生存期.
According to the docs, the default token lifetime should be set in your AuthServiceProvider.php
by calling the Passport::tokensExpireIn()
function in your boot()
method.
示例:
public function boot() {
...
$baseExpire = site_config('token_expires_minutes');
Passport::tokensExpireIn(now()->addMinutes($baseExpire));
}
编辑
对不起,我误解了您的原始问题.不幸的是,由于已将到期令牌编码到实际的令牌字符串中,因此无法更改已生成令牌的到期令牌(Laravel使用 https://github.com/lcobucci/jwt 来生成和验证令牌).但是,您可以考虑覆盖护照验证以检查表上的到期时间,而不是使用默认程序包.
Sorry, I misunderstood your original question. Unfortunately, it's not possible to change the expiration of a generated token, since the expiration is encoded into the actual token string (Laravel uses https://github.com/lcobucci/jwt to generate and validate tokens). However, you may look into overriding the passport validation to check the expiration on the table rather than using the default package.
这篇关于如何编辑Laravel护照令牌的expires_at字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!