Giter Site home page Giter Site logo

Comments (3)

serhiisol avatar serhiisol commented on June 3, 2024

it should automatically refresh token if the previous request fails with access token failure.
First of all, you have to identify refresh token request, so the library won't intercept it and won't add extra refresh token functionality for particular request:

  public verifyTokenRequest(url: string): boolean {
    return url.endsWith('refresh-token');
  }

then you have to tell library that previous request failed because of the access token failure:

  public refreshShouldHappen(response: HttpErrorResponse): boolean {
    return response.status === 401; // for example at this point we need to refresh token
  }

from ngx-auth.

ujjaldey avatar ujjaldey commented on June 3, 2024

Yes. I have these functions in place. And I do see that the refresh api is getting called whenever the original request returns 401.

But my api does not return any refresh_token. It refreshes any expired token based on access_token only which I need to pass along with my '/refresh' call. But the refresh request does not seem to have the 'Authorization: Bearer' in the header. So my api is returning invalid token response. I have found a quick workaround. I can pass the token as query param with the url. But is there any better way!

public refreshToken(): Observable<any> {
 return this.tokenStorage
  .getAccessToken()
  .pipe(
	switchMap((accessToken: string) =>
	  this.http.post(`http://localhost/uku/api/auth/refresh?token=${accessToken}`, {})
	),
	tap((tokens: any) => {
	  this.saveAccessData(tokens.data.accessToken);
	}),
	catchError((err) => {
	  this.logout();
	  return Observable.throw(err);
	})
  );
}

from ngx-auth.

serhiisol avatar serhiisol commented on June 3, 2024

if refresh token is verified properly via verifyTokenRequest interceptor won't add any tokens to the request, it won't process that request at all see https://github.com/serhiisol/ngx-auth-example/blob/master/src/app/shared/authentication/authentication.service.ts#L51

from ngx-auth.

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.