|
1 | 1 | import GoTrueAdminApi from './GoTrueAdminApi'
|
2 |
| -import { DEFAULT_HEADERS, EXPIRY_MARGIN, GOTRUE_URL, STORAGE_KEY } from './lib/constants' |
| 2 | +import { |
| 3 | + DEFAULT_HEADERS, |
| 4 | + EXPIRY_MARGIN_MS, |
| 5 | + AUTO_REFRESH_TICK_DURATION_MS, |
| 6 | + AUTO_REFRESH_TICK_THRESHOLD, |
| 7 | + GOTRUE_URL, |
| 8 | + STORAGE_KEY, |
| 9 | +} from './lib/constants' |
3 | 10 | import {
|
4 | 11 | AuthError,
|
5 | 12 | AuthImplicitGrantRedirectError,
|
@@ -109,13 +116,6 @@ const DEFAULT_OPTIONS: Omit<Required<GoTrueClientOptions>, 'fetch' | 'storage' |
|
109 | 116 | hasCustomAuthorizationHeader: false,
|
110 | 117 | }
|
111 | 118 |
|
112 |
| -/** Current session will be checked for refresh at this interval. */ |
113 |
| -const AUTO_REFRESH_TICK_DURATION = 30 * 1000 |
114 |
| - |
115 |
| -/** |
116 |
| - * A token refresh will be attempted this many ticks before the current session expires. */ |
117 |
| -const AUTO_REFRESH_TICK_THRESHOLD = 3 |
118 |
| - |
119 | 119 | async function lockNoOp<R>(name: string, acquireTimeout: number, fn: () => Promise<R>): Promise<R> {
|
120 | 120 | return await fn()
|
121 | 121 | }
|
@@ -1107,8 +1107,13 @@ export default class GoTrueClient {
|
1107 | 1107 | return { data: { session: null }, error: null }
|
1108 | 1108 | }
|
1109 | 1109 |
|
| 1110 | + // A session is considered expired before the access token _actually_ |
| 1111 | + // expires. When the autoRefreshToken option is off (or when the tab is |
| 1112 | + // in the background), very eager users of getSession() -- like |
| 1113 | + // realtime-js -- might send a valid JWT which will expire by the time it |
| 1114 | + // reaches the server. |
1110 | 1115 | const hasExpired = currentSession.expires_at
|
1111 |
| - ? currentSession.expires_at <= Date.now() / 1000 |
| 1116 | + ? currentSession.expires_at * 1000 - Date.now() < EXPIRY_MARGIN_MS |
1112 | 1117 | : false
|
1113 | 1118 |
|
1114 | 1119 | this._debug(
|
@@ -1503,7 +1508,7 @@ export default class GoTrueClient {
|
1503 | 1508 | }
|
1504 | 1509 |
|
1505 | 1510 | const actuallyExpiresIn = expiresAt - timeNow
|
1506 |
| - if (actuallyExpiresIn * 1000 <= AUTO_REFRESH_TICK_DURATION) { |
| 1511 | + if (actuallyExpiresIn * 1000 <= AUTO_REFRESH_TICK_DURATION_MS) { |
1507 | 1512 | console.warn(
|
1508 | 1513 | `@supabase/gotrue-js: Session as retrieved from URL expires in ${actuallyExpiresIn}s, should have been closer to ${expiresIn}s`
|
1509 | 1514 | )
|
@@ -1850,7 +1855,7 @@ export default class GoTrueClient {
|
1850 | 1855 | error &&
|
1851 | 1856 | isAuthRetryableFetchError(error) &&
|
1852 | 1857 | // retryable only if the request can be sent before the backoff overflows the tick duration
|
1853 |
| - Date.now() + nextBackOffInterval - startedAt < AUTO_REFRESH_TICK_DURATION |
| 1858 | + Date.now() + nextBackOffInterval - startedAt < AUTO_REFRESH_TICK_DURATION_MS |
1854 | 1859 | )
|
1855 | 1860 | }
|
1856 | 1861 | )
|
@@ -1923,12 +1928,12 @@ export default class GoTrueClient {
|
1923 | 1928 | return
|
1924 | 1929 | }
|
1925 | 1930 |
|
1926 |
| - const timeNow = Math.round(Date.now() / 1000) |
1927 |
| - const expiresWithMargin = (currentSession.expires_at ?? Infinity) < timeNow + EXPIRY_MARGIN |
| 1931 | + const expiresWithMargin = |
| 1932 | + (currentSession.expires_at ?? Infinity) * 1000 - Date.now() < EXPIRY_MARGIN_MS |
1928 | 1933 |
|
1929 | 1934 | this._debug(
|
1930 | 1935 | debugName,
|
1931 |
| - `session has${expiresWithMargin ? '' : ' not'} expired with margin of ${EXPIRY_MARGIN}s` |
| 1936 | + `session has${expiresWithMargin ? '' : ' not'} expired with margin of ${EXPIRY_MARGIN_MS}s` |
1932 | 1937 | )
|
1933 | 1938 |
|
1934 | 1939 | if (expiresWithMargin) {
|
@@ -2101,7 +2106,7 @@ export default class GoTrueClient {
|
2101 | 2106 |
|
2102 | 2107 | this._debug('#_startAutoRefresh()')
|
2103 | 2108 |
|
2104 |
| - const ticker = setInterval(() => this._autoRefreshTokenTick(), AUTO_REFRESH_TICK_DURATION) |
| 2109 | + const ticker = setInterval(() => this._autoRefreshTokenTick(), AUTO_REFRESH_TICK_DURATION_MS) |
2105 | 2110 | this.autoRefreshTicker = ticker
|
2106 | 2111 |
|
2107 | 2112 | if (ticker && typeof ticker === 'object' && typeof ticker.unref === 'function') {
|
@@ -2208,12 +2213,12 @@ export default class GoTrueClient {
|
2208 | 2213 |
|
2209 | 2214 | // session will expire in this many ticks (or has already expired if <= 0)
|
2210 | 2215 | const expiresInTicks = Math.floor(
|
2211 |
| - (session.expires_at * 1000 - now) / AUTO_REFRESH_TICK_DURATION |
| 2216 | + (session.expires_at * 1000 - now) / AUTO_REFRESH_TICK_DURATION_MS |
2212 | 2217 | )
|
2213 | 2218 |
|
2214 | 2219 | this._debug(
|
2215 | 2220 | '#_autoRefreshTokenTick()',
|
2216 |
| - `access token expires in ${expiresInTicks} ticks, a tick lasts ${AUTO_REFRESH_TICK_DURATION}ms, refresh threshold is ${AUTO_REFRESH_TICK_THRESHOLD} ticks` |
| 2221 | + `access token expires in ${expiresInTicks} ticks, a tick lasts ${AUTO_REFRESH_TICK_DURATION_MS}ms, refresh threshold is ${AUTO_REFRESH_TICK_THRESHOLD} ticks` |
2217 | 2222 | )
|
2218 | 2223 |
|
2219 | 2224 | if (expiresInTicks <= AUTO_REFRESH_TICK_THRESHOLD) {
|
|
0 commit comments