I am using angular as a client and asp.net-core as a server. From server I am creating JWT token and refresh token and passing it to angular and storing it in localstorage. I am putting my JWT token validation for 5 minutes and refresh token as 2 days . The issue which I am facing is that my token is expire and I am on page where only authenticate user can visit who have valid jwt token until I refresh my page or go to another URL my auth is not going to come into picture and will not get to know that our token is expire so if there is a event in which we make call to [authorize]Api our call will going to reject because of we having a expired token . So **I want to know is there any way to check that is token expire before making any call to server api**

here is my **AuthGurd**

```

  constructor (private _authService: AuthService, private _router: Router) {}

  async canActivate(): Promise<boolean>{
    if(await this._authService.isUserAuthenticated() === true){
      return true;
    }
    else{
      this._router.navigate([RouteConstant.SIGN_IN]);
      return false;
    }
  }
public async isUserAuthenticated(): Promise<boolean>{
    if(this._storageService.getToken() !== null){
      if(!this._jwtHelperService.isTokenExpired(this._storageService.getToken()?.toString())){
        return true;
      }
      else{
        if(!this._storageService.refreshTokenExists()){
          return false;
        }
        else{
          let authTokenClient: AuthTokenClient = {
            token: this._storageService.getToken() as string,
            refreshToken: this._storageService.getRefreshToken() as string
          };
            return await this.refreshAuthToken(authTokenClient);
            !this._jwtHelperService.isTokenExpired(this._storageService.getToken()?.toString());
        }
      }
    }
    else{
      return false;
    }
  }
  }

  public setAuth(authToken: AuthToken){
    this._storageService.saveToken(authToken.token?.toString());
    this._storageService.saveRefreshToken(authToken.refreshToken);
  }
  public async refreshAuthToken(authTokenClient: AuthTokenClient) {
    const response = await this._http.post<AuthToken>(environment.apiHost + UrlConstant.ACCOUNT_REFRESH, authTokenClient,{observe: 'response'}).toPromise();
    console.log(response);
    const newToken = (<any>response).body.token;
            console.log(newToken);
            const newRefreshToken = (<any>response).body.refreshToken;
            console.log(newRefreshToken);
            localStorage.setItem("token", newToken);
            localStorage.setItem("refreshToken",newRefreshToken);
            if (newToken && newRefreshToken == null){
              return false
            }
            else {
              return true;
            }
  }

}

```

I tried to validate the jwt token before every call to api i have done that by
here my
**app.component.ts**

```
async clickEvent(){
    if (await this._authService.isUserAuthenticated()==true)
    return this.api();
  else{
    this.logout();
  }
}
  public api(){
    this._http.get("http://localhost:15363/WeatherForecast").subscribe(response => {
 console.log(response);
    }, err => {
      console.log(err)
    });
  }

```

**app.component.html**

```
<div>
    <button  (click)="clickEvent()" >
        Click Me</button>
</div>

```

**c#** code Api which angular is calling

```
 [HttpGet]
        [Authorize]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

```

But I think this is not best approach for one call it's will valid but in application we have many calls we cannot every time use this if else statement