0

I have Angular 10 and am following a pattern like https://jasonwatmore.com/post/2020/04/19/angular-9-jwt-authentication-example-tutorial

My requirement it to make sure that I have a valid API key whenever making get requests. There are 3 cases

  • Key is valid, make the api call
  • Key has expired. Call a different api to get a new key first
  • No key. Call the key api before calling the api

I've got the interceptor. In here I could call the key api if I don't have a current key, but how do I pause the api requests until the key is valid?

@Injectable()
export class JwtInterceptor implements HttpInterceptor {  
if (!isLoggedIn) {
      this.authenticationService.login().subscribe(???);
}

Where login() calls the api key generation service.

I don't want users to login, just protect the web services. What is the secret recipe?

Interlated
  • 5,108
  • 6
  • 48
  • 79
  • 2
    You don't check if you are logged in in the client but handle 401 errors when making your api calls and then retry failed requests after refreshing the token. See https://stackoverflow.com/questions/45202208/angular-4-interceptor-retry-requests-after-token-refresh – Isidrok Jun 25 '20 at 06:24
  • 1
    Dude that's Angular not Angularjs. – Ali Bahrami Jun 25 '20 at 06:58

0 Answers0