M
M
Maxim Tarabrin2017-05-31 16:22:50
Angular
Maxim Tarabrin, 2017-05-31 16:22:50

How and when to refresh a token in Angular 2?

I am using JWT for django. The manual says that there is an option to update the method out of the box.
I pass the token to 127.0.0.1:8000/api-token-refresh/, and I get an error in response:
orig_iat field is required
And when to refresh the token? With each request to the server, make a parallel request to ./api-token-refresh/?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Abdulla Mursalov, 2017-05-31
@padr1no

Together with the token, the lifetime comes, you bring it to an absolute value, you create a timer subscription, you refresh the token using a refresh token

E
Evgeny Zalivadny, 2020-05-15
@Nordihan

Alternatively, you can use @auth0/angular-jwt to get the token lifetime :

import {JwtHelperService} from '@auth0/angular-jwt';

export class AuthService {
    private jwtHelper: JwtHelperService;

    constructor(){
        this.jwtHelper = new JwtHelperService();
    }

    getAccessToken(): string {
        return localStorage.getItem('access');
    }

    getAccessTokenExpirationDate(): Date {
        return this.jwtHelper.getTokenExpirationDate(this.getAccessToken());
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question