I am working on an application, which uses OAuth - Token based authentication.
This is how the flow looks like, considering we have the access and refresh token.
- Api call -> intercepter appends access-token -> api returns 200
- Api call -> intercepter appends expired access-token -> api returns 401 -> intercepter refreshes token using refresh token -> interceptor retries same req -> returns 200
- Api call -> intercepter appends expired access-token -> api returns 401 -> intercepter refreshes token using refresh token(refresh token is also expired) -> prompt guest to sign-in -> guest signed-in -> retry request
This all works great and fine - I am considering to optimise it a bit i.e i don't want to call api and wait for 401 to return. Instead check for token expiration beforehand, get the new access token and then call the api with valid token.
This approach of calculating the expiry of token using android system time might work - but can be misused sometimes when user changes the android time.
Wondering if there a better solution to avoid the expiry issue of time based on android system time.