SpoonX / aurelia-authentication

Authentication plugin for aurelia.
http://aurelia-authentication.spoonx.org
MIT License
90 stars 60 forks source link

setTimeout(ttl) is not persistent #415

Closed fieldfoxWim closed 4 years ago

fieldfoxWim commented 4 years ago

Hi All

Context I am using aurelia-authentication to authenticate with AWS Cognito. I have my own expressjs middleware in between. I have everything up and running and main functionality is doing well!

Special requirement I need to use Refreshtokens to keep users logged in. AWS Cognito requires that the access_token is still valid for at least 5min to be able to use the refresh token.

Solution 1 - partially working I use setTimeout to force an earlier invocation of updateToken(), every 30 minutes

return this.authService.login({username: this.email, password:this.password, grant_type: "password"})
      .then(() => {
                 this.authService.setTimeout(1800000);
        });

Problem The timeout is only executed once, and when the automatic updateToken() kicks in it resets the timeout to Ttl of the access_token.

Thanks anyway to share this great library!

Thank you Wim Devos

RWOverdijk commented 4 years ago

Ouch, cognito. That sucks.

I hope someone can help you but it's not me.

fieldfoxWim commented 4 years ago

I solved it by creating my own refresh timers

aurelia.start().then(() => {
    const auth = aurelia.container.get(AuthService);
    const tokenRefreshTimer = 900; //seconds
    if (auth.isAuthenticated()) {
      if (auth.getTtl() <= tokenRefreshTimer) {
        auth.updateToken().then(
          () => {}).catch(console.error);
      }

      PLATFORM.global.setInterval(() => {
        auth.updateToken().then(
          () => {}
        ).catch(console.error);
      }, tokenRefreshTimer*1000)

      aurelia.setRoot('app')
    } else {
      aurelia.setRoot('login')
    }
  });
login() {
    this.busy = true;

    const self = this;
    return this.authService.login({username: this.email, password:this.password, grant_type: "password"})
      .then(() => {
        PLATFORM.global.setInterval(() => {
          this.authService.updateToken().then(
            () => { }
        ).catch(console.error);
        }, 60000)
        this.aurelia.setRoot('app');
      })
...

When I have time I might fork and do a pull request. Anyway If somebody wants to use Cognito - the above helps with the refreshtimers

Regards Wim

fieldfoxWim commented 4 years ago

Ofcourse the setting autoUpdateToken must be set to false!