Skip to content

Commit

Permalink
Merge pull request #101 from thebengeu/expires
Browse files Browse the repository at this point in the history
fix: supabase/supabase-js#178 handle JWT expiry <= 60s
  • Loading branch information
kiwicopple authored Jun 14, 2021
2 parents 3be3eb0 + 7502d3a commit c2a1975
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,12 @@ export default class GoTrueClient {
this.currentUser = session.user

const expiresAt = session.expires_at
const timeNow = Math.round(Date.now() / 1000)
if (expiresAt) this._startAutoRefreshToken((expiresAt - timeNow - 60) * 1000)
if (expiresAt) {
const timeNow = Math.round(Date.now() / 1000)
const expiresIn = expiresAt - timeNow
const refreshDurationBeforeExpires = expiresIn > 60 ? 60 : 0.5
this._startAutoRefreshToken((expiresIn - refreshDurationBeforeExpires) * 1000)
}

// Do we need any extra check before persist session
// access_token or user ?
Expand All @@ -576,7 +580,7 @@ export default class GoTrueClient {
*/
private _startAutoRefreshToken(value: number) {
if (this.refreshTokenTimer) clearTimeout(this.refreshTokenTimer)
if (!value || !this.autoRefreshToken) return
if (value <= 0 || !this.autoRefreshToken) return

this.refreshTokenTimer = setTimeout(() => this._callRefreshToken(), value)
}
Expand Down

0 comments on commit c2a1975

Please sign in to comment.