Giter VIP home page Giter VIP logo

Comments (7)

hassanasad avatar hassanasad commented on June 26, 2024

Tagging @frederikprijck :)

from angular2-jwt.

frederikprijck avatar frederikprijck commented on June 26, 2024

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.

hassanasad avatar hassanasad commented on June 26, 2024

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.

hassanasad avatar hassanasad commented on June 26, 2024

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.

frederikprijck avatar frederikprijck commented on June 26, 2024

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.

hassanasad avatar hassanasad commented on June 26, 2024

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.

frederikprijck avatar frederikprijck commented on June 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.