Comments (7)
Tagging @frederikprijck :)
from angular2-jwt.
Based on what's been shared, it's hard to tell what can cause this.
Did you set the skipWhenExpired setting to true?
Is the token expired at all?
from angular2-jwt.
Thanks for the quickest reply :)
No i dont have that boolean setting skipWhenExpired
in my code. I am assuming it will be using default value for it.
Yes the token expires every five minutes and then based on refresh token a new access token is issued.
I see the refresh token logic working in the browser network tab when the API requests fail with a status 401
TokenException
HTTP response and new token is issued. Following requests use that new authorization header.
In one of the articles i was reading these sort of issues can happen if an interceptor or HttpClientModule
is loaded multiple times. I dont see that in my code. Its only loaded once in core.module.ts
file which is imported once in app.module.ts
file.
I only see the JwtInterceptor
being in the providers list twice, one for the actual interceptor and once for a requirement in RefreshTokenInterceptor
. Could that cause any issue ?
from angular2-jwt.
This is my refreshToken interceptor in case it helps:
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError, mergeMap, tap } from 'rxjs/operators';
import { JwtInterceptor, JwtHelperService } from '@auth0/angular-jwt';
import { UserService } from '@app/core/services/user.service';
// Logic inspired from https://gist.github.com/Toilal/8849bd63d53bd2df2dd4df92d3b12f26
@Injectable({
providedIn: 'root',
})
export class RefreshTokenInterceptor implements HttpInterceptor {
constructor(
private userService : UserService,
private jwtInterceptor: JwtInterceptor,
private jwtHelper : JwtHelperService,
) { }
public intercept( req: HttpRequest<any>, next: HttpHandler ): Observable<HttpEvent<any>> {
const whiteListedDomain = this.jwtInterceptor.isAllowedDomain( req );
const isBlacklistedRoute = this.jwtInterceptor.isDisallowedRoute( req );
if ( whiteListedDomain && !isBlacklistedRoute && !req.url?.toUpperCase().includes('SKIPINTERCEPTORS=TRUE') ) {
return next.handle( req )
.pipe(
tap( ( res ) => {
if ( res?.type !== 0 ) {
// Check for API requests other than OPTION types
// Check if token is close to expiry - we can try refreshing it before hitting API errors
const token = this.userService.getToken();
const tokenExpiring = this.jwtHelper.isTokenExpired( token, 60 ); // Check if its expiring within 60 seconds
if ( tokenExpiring === true ) {
this.userService.refreshToken();
}
}
}),
catchError( ( err ) => {
const errorResponse = err as HttpErrorResponse;
if ( errorResponse?.status === 401 && errorResponse?.error?.responseStatus?.errorCode?.toUpperCase() === 'TOKENEXCEPTION' ) {
// Token has expired - refresh it
return this.userService.refreshToken()
.pipe(mergeMap(() => {
return this.jwtInterceptor.intercept( req, next );
}));
}
return throwError( () => err );
})
);
}
return next.handle( req );
}
}
from angular2-jwt.
It does look like your Interceptor could be causing it, i don't understand why it's calling our SDK's interceptor like that.
Can you isolate everything and reproduce it outside of your app for us to look into?
from angular2-jwt.
The solution has been inspired by https://gist.github.com/Toilal/8849bd63d53bd2df2dd4df92d3b12f26 so not a 100% sure why its needed to call the jwtInterceptor.intercept
. If i comment that part - it doesn't resend the requests again with updated token.
from angular2-jwt.
I'm going to close this are there currently isnt much for us to action.
Happy to reopen if you can provide an example application for us to look into, but I do not believe this is an SDK issue.
from angular2-jwt.
Related Issues (20)
- tokenGetter using async documentation not complete
- Error: The inspected token doesn't appear to be a JWT. Check to make sure it has three parts and see https://jwt.io for more. HOT 1
- Angular 12 support HttpInterceptor Context HOT 4
- Angular 13 support HOT 28
- upstream dependency conflict while installing angular2-jwt package using angular-cli HOT 1
- ``
- v5.1.0 broke azure devops publishing HOT 5
- Please respect semver HOT 3
- tokenGetter does not support Promise HOT 2
- Getting an issue of no exported member from angular core HOT 1
- Encourage the library authors to publish an Ivy distribution HOT 2
- Release v5.1.1 decodeToken<T = any>(token?: string): T | Promise<T> HOT 4
- 'ɵɵngDeclareFactory' (imported as 'i0') was not found in '@angular/core' HOT 2
- JwtInterceptor not work HOT 2
- No header with standalone components HOT 4
- Angular 16 support HOT 1
- Package not working with es2022 HOT 4
- Angular JWT 5.0.2 not compiling with Angular 13.3.6 HOT 1
- App no longer working after installing auth0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular2-jwt.