Lovelock
Lovelock

Reputation: 8075

JWT Auth and Satellizer - Increase expiry token time for mobile app

I am using Ionic and satellizer along with Laravel and JWT auth to create an API.

Everything is good, but the one issue is the token being removed from local storage after an hour or so.

I really want the token to exist until the user logs out, as they will be using a phone app and not wishing to log in everytime.

This is first experience with tokens, so I am not sure on how this normally works. I imagine people done normally store tokens for ever?

This is in my Ionic controller:

    $auth.login(credentials).then(function() {
        $http.get($rootScope.apiURL + 'authenticate/user').success(function(response){
            var user = JSON.stringify(response.user);
            localStorage.setItem('user', user);
        });
    })

This sets a Satellizer token and also the user information in Local storage.

In Laravel for the API call:

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

Upvotes: 0

Views: 944

Answers (1)

Niklesh Raut
Niklesh Raut

Reputation: 34914

You can increase your expiry time in config/jwt.php , default is 60 minutes

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

'ttl' => 60,

Upvotes: 2

Related Questions