You can create a new table or field to save the time when the user acquired the token, and then the client will poll and re-obtain the new token for the client just a few minutes before the expiry token.
How to refresh Sanctum token
I am building an API ONLY install of Laravel. I decided to use Sanctum for issuing the tokens. In the config/sanctum.php I have set expiration for 60mins.
Once the token expires I get the standard 401 response;
{
"message": "Unauthenticated."
}
I have read online that a refresh endpoint should look like so;
Route::get('refresh', [Auth\AuthenticationController::class, 'refresh'])->middleware('auth:sanctum');
and the method as;
public function refresh(Request $request): JsonResponse
{
$request->user()->tokens()->delete();
return response()->json([
'access_token' => $request->user()->createToken('api')->plainTextToken,
]);
}
The problem is, that if i try and hit that endpoint with an expired token, it just gives the 401 response again. If i remove the sanctum middleware from that route, it throws Call to a member function tokens() on null
Does Sanctum have the ability to issue a refresh token? What is the correct way to refresh a token?
Please or to participate in this conversation.