【问题标题】:JWT Auth and Satellizer - Increase expiry token time for mobile appJWT Auth 和 Satellizer - 增加移动应用程序的到期令牌时间
【发布时间】:2016-05-08 03:21:32
【问题描述】:

我正在使用 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'));
}

【问题讨论】:

    标签: angularjs laravel-5 token jwt satellizer


    【解决方案1】:

    您可以在 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,
    

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 2019-08-08
      • 2020-02-17
      • 2016-12-28
      • 2017-09-05
      • 2015-08-03
      • 2016-05-03
      • 1970-01-01
      • 2017-05-19
      相关资源
      最近更新 更多