JWT 身份验证和 Satellizer语言 - 增加移动应用程序的到期令牌时间



我正在使用Ionic和satellizer以及Laravel和JWT auth来创建API。

一切都很好,但一个问题是令牌在一小时左右后从本地存储中删除。

我真的希望令牌在用户注销之前一直存在,因为他们将使用手机应用程序并且不希望每次都登录。

这是令牌的第一次体验,所以我不确定这通常是如何工作的。我想人们通常会永远存储代币吗?

这是我的离子控制器:

    $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'));
}

您可以在 config/jwt 中增加到期时间.php ,默认值为 60 分钟

| JWT time to live
|--------------------------------------------------------------------------
|
| Specify the length of time (in minutes) that the token will be valid for.
| Defaults to 1 hour
|
*/
'ttl' => 60,

最新更新