我正在使用 Ionic 和 satellizer 以及 Laravel 和 JWT auth 来创建 API。
一切都很好,但一个问题是令牌在一个小时左右后从本地存储中删除。
我真的希望令牌在用户注销之前一直存在,因为他们将使用手机应用程序并且不希望每次都登录。
这是令牌的第一次体验,所以我不确定这通常是如何工作的。我想人们通常会永远存储令牌吗?
这是在我的 Ionic 控制器中:
$auth.login(credentials).then(function() {
$http.get($rootScope.apiURL + 'authenticate/user').success(function(response){
var user = JSON.stringify(response.user);
localStorage.setItem('user', user);
});
})
这会在本地存储中设置 Satellizer 令牌以及用户信息。
在 Laravel 中进行 API 调用:
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
try {
// verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json([
'error' => 'invalid_credentials',
'error_message' => 'Invalid username or password'
], 401);
}
} catch (JWTException $e) {
// something went wrong
return response()->json(['error' => 'could_not_create_token'], 500);
}
// if no errors are encountered we can return a JWT
return response()->json(compact('token'));
}