akveo / ngx-admin-bundle-support

Support repository for ngx-admin backend bundles with issues tracking, instructions and code samples
58 stars 32 forks source link

After Refresh token request new token doesn't saved/updated in local storage #79

Open seom-aa opened 4 years ago

seom-aa commented 4 years ago

I have Node.js MongoDB Starter Bundle (v04-03-2020) It use by default NbPasswordAuthStrategy and token: { class: NbAuthOAuth2JWTToken, key: 'token', },

For the sake of simlicity i don't write tokens. I think it doesn't matter

Current behavior: After succesful login, access & refresh tokens successfully stored in local storage in such format:

{
 createdAt: 1591249154000
 name: "nb:auth:oauth2:jwt:token"
 ownerStrategyName: "email"
 value: "{"expires_in":36,"access_token":"...","refresh_token":"..."}"
}

After expiration period i see request to backend (endpoint '/refresh-token') In backend AuthService.js -> refreshToken(token) i write console.log(token)

{
  payload: {
    expires_in: 60,
    access_token: '...',
    refresh_token: '...'
  },
  token: {
    expires_in: 60,
    access_token: '...',
    refresh_token: '...'
  },
  ownerStrategyName: 'email',
  createdAt: '2020-06-20T05:39:14.000Z',
  accessTokenPayload: {
    id: '5ed887b084294d5fa87dc6f3',
    role: 'user',
    iat: 1591249154,
    exp: 1591249190
  }
}

First of all it check token format:

if (!token.access_token || !token.refresh_token) {
      throw new Error('Invalid token format');
}

And of course it throw Error (because need to check token.token.access_token)!!! Ok. I made a little change

refreshToken(token) {
  token = token.token
  if (!token.access_token || !token.refresh_token) {
    throw new Error('Invalid token format');
  }

... and it's ok

New token sended to a frontend and I see in a browser console response:

{ access_token: "..."
expires_in: 60
refresh_token: "..."}

But in local storage tokens left unchanged!

Expected behavior: After refresh token request local storage tokens should be updated and this.tokenService.tokenChange() should fired, but its not happend

Steps to reproduce: Just login as user@user.user and after expiration period of time tokens should be updated in local storage

Other information: Ubuntu Server 18.04 npm 6.14.4 node v12.17.0 Chrome 83.0.4103.61 (64)