aurelia-auth icon indicating copy to clipboard operation
aurelia-auth copied to clipboard

Feature request: redirect / logout on expiration

Open CD-UNCC opened this issue 9 years ago • 7 comments
trafficstars

Love the plugin. I think it would be great to be able to set a redirect or function for when the JWT expires. The Nav bar will update automatically and auth: true routes will vanish. But if you are on an authenticated route when the token expires you can simply remain there.

CD-UNCC avatar May 02 '16 16:05 CD-UNCC

Second this. I was about to ask if you have any tips on how to handle expiration? My understanding is it'll only be triggered when you try to navigate somewhere, but the token will still exist in storage, so surely all we need to do is redirect if token exists but has expired? If that logic makes sense, I'll try and do a PR for this.

apawsey avatar May 20 '16 06:05 apawsey

Cool, thanks for making a PR.

paulvanbladel avatar May 20 '16 14:05 paulvanbladel

@apawsey What I did was in authentication.js line 129 Make a check for Math.round(new Date().getTime() / 1000) <= exp;

If the token has expired in this check a getLogoutRedirect should be added at the top of this file that looks in the config file just like the login redirect does. Otherwise you can just call logout.

if (exp) {

    if(Math.round(new Date().getTime() / 1000) <= exp){
        return true;
    }
    else{
        this.logout();
        return false;
    }
  }`

In the future I may ask to remain logged in when expiry is approaching and call to renew the token. Similar to banking sites. Hope this helps!

CD-UNCC avatar May 20 '16 15:05 CD-UNCC

Has anyone implemented a solution to this? I'm interested in knowing how others have intercepted when a token is about to expire and enable a user to 'refresh' the token.

stuartbale avatar Jul 28 '16 23:07 stuartbale

@stuartbale aurelia-auth is constantly checking the expire time of the token and you can incorporate your custom logic there. See my above post.

authentication.js line 129

Here you can change the conditional to fit your needs.

CD-UNCC avatar Aug 08 '16 14:08 CD-UNCC

@CD-UNCC I don't think that "aurelia-auth is constantly checking the expire time of the token" - according to the code for tokenInterceptor(), this check is only being made during AJAX requests, by a request interceptor. It is similar to @apawsey 's statement that the check is done on navigation - but not exactly, because this check is done on request -> and the only purpose of it is to add the "Authorization: Bearer: XXX" header.

@paulvanbladel Paul - I've had a similar query, but more generally about the full Oauth2 use case implementation, specifically regarding expiration and "refresh tokens". My understanding of the OAuth2 intention is that this sequence should occur -

  1. Client makes request with accessToken.
  2. Server/provider receives request; responds with 401 Unauthorized for expired token, otherwise returns response.
  3. If client received 401 Unauthorized response, client re-tries authentication request for token with grant-type:refresh, using refreshToken instead of accessToken.
  4. Server returns a new accessToken to client.
  5. Client retries original request using new token.

Since this aurelia-auth module is strongly based on OAuth, is there anything in the module to help with this pattern? Or is the expectation that all consumers/developers using the module will implement this sort of logic?

Thanks, Don

don-bluelinegrid avatar Aug 12 '16 13:08 don-bluelinegrid

Has anyone fixed and/or forked this as yet?

seagullmouse avatar Nov 30 '17 14:11 seagullmouse