@supabase/gotrue-js
Advanced tools
Comparing version 1.12.3 to 1.12.4
@@ -21,2 +21,3 @@ import GoTrueApi from './GoTrueApi'; | ||
protected stateChangeEmitters: Map<string, Subscription>; | ||
protected refreshTokenTimer?: ReturnType<typeof setTimeout>; | ||
/** | ||
@@ -132,5 +133,2 @@ * Create a new client for use in the browser. | ||
private _handleProviderSignIn; | ||
private _saveSession; | ||
private _persistSession; | ||
private _removeSession; | ||
/** | ||
@@ -143,2 +141,3 @@ * Attempts to get the session from LocalStorage | ||
* Recovers the session from LocalStorage and refreshes | ||
* Note: this method is async to accommodate for AsyncStorage e.g. in React native. | ||
*/ | ||
@@ -148,3 +147,15 @@ private _recoverAndRefresh; | ||
private _notifyAllSubscribers; | ||
/** | ||
* set currentSession and currentUser | ||
* process to _startAutoRefreshToken if possible | ||
*/ | ||
private _saveSession; | ||
private _persistSession; | ||
private _removeSession; | ||
/** | ||
* Clear and re-create refresh token timer | ||
* @param value time intervals in milliseconds | ||
*/ | ||
private _startAutoRefreshToken; | ||
} | ||
//# sourceMappingURL=GoTrueClient.d.ts.map |
@@ -161,7 +161,6 @@ "use strict"; | ||
throw new Error('Not logged in.'); | ||
yield this._callRefreshToken(); | ||
const { data, error } = yield this.api.getUser(this.currentSession.access_token); | ||
// currentSession and currentUser will be updated to latest on _callRefreshToken | ||
const { error } = yield this._callRefreshToken(); | ||
if (error) | ||
throw error; | ||
this.currentUser = data; | ||
return { data: this.currentSession, user: this.currentUser, error: null }; | ||
@@ -183,11 +182,11 @@ } | ||
throw new Error('Not logged in.'); | ||
const { data, error } = yield this.api.updateUser(this.currentSession.access_token, attributes); | ||
const { user, error } = yield this.api.updateUser(this.currentSession.access_token, attributes); | ||
if (error) | ||
throw error; | ||
this.currentUser = data; | ||
const session = Object.assign(Object.assign({}, this.currentSession), { user: data }); | ||
this.currentSession = session; | ||
if (!user) | ||
throw Error('Invalid user data.'); | ||
const session = Object.assign(Object.assign({}, this.currentSession), { user }); | ||
this._saveSession(session); | ||
this._notifyAllSubscribers('USER_UPDATED'); | ||
return { data, user: this.currentUser, error: null }; | ||
return { data: user, user, error: null }; | ||
} | ||
@@ -333,20 +332,2 @@ catch (error) { | ||
} | ||
_saveSession(session) { | ||
this.currentSession = session; | ||
this.currentUser = session.user; | ||
if (this.persistSession && session.expires_at) { | ||
this._persistSession(this.currentSession); | ||
} | ||
} | ||
_persistSession(currentSession) { | ||
const data = { currentSession, expiresAt: currentSession.expires_at }; | ||
helpers_1.isBrowser() && this.localStorage.setItem(constants_1.STORAGE_KEY, JSON.stringify(data)); | ||
} | ||
_removeSession() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.currentSession = null; | ||
this.currentUser = null; | ||
helpers_1.isBrowser() && (yield this.localStorage.removeItem(constants_1.STORAGE_KEY)); | ||
}); | ||
} | ||
/** | ||
@@ -366,5 +347,5 @@ * Attempts to get the session from LocalStorage | ||
const timeNow = Math.round(Date.now() / 1000); | ||
if (expiresAt > timeNow && (currentSession === null || currentSession === void 0 ? void 0 : currentSession.user)) { | ||
this.currentSession = currentSession; | ||
this.currentUser = currentSession.user; | ||
if (expiresAt >= timeNow && (currentSession === null || currentSession === void 0 ? void 0 : currentSession.user)) { | ||
this._saveSession(currentSession); | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
} | ||
@@ -378,11 +359,11 @@ } | ||
* Recovers the session from LocalStorage and refreshes | ||
* Note: this method is async to accommodate for AsyncStorage e.g. in React native. | ||
*/ | ||
_recoverAndRefresh() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// Note: this method is async to accommodate for AsyncStorage e.g. in React native. | ||
const json = helpers_1.isBrowser() && (yield this.localStorage.getItem(constants_1.STORAGE_KEY)); | ||
if (!json) { | ||
return null; | ||
} | ||
try { | ||
const json = helpers_1.isBrowser() && (yield this.localStorage.getItem(constants_1.STORAGE_KEY)); | ||
if (!json) { | ||
return null; | ||
} | ||
const data = JSON.parse(json); | ||
@@ -408,7 +389,5 @@ const { currentSession, expiresAt } = data; | ||
else { | ||
this.currentSession = currentSession; | ||
this.currentUser = currentSession.user; | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
// schedule a refresh 60 seconds before token due to expire | ||
setTimeout(() => this._callRefreshToken(), (expiresAt - timeNow - 60) * 1000); | ||
// should be handle on _recoverSession method already | ||
// this._saveSession(currentSession) | ||
// this._notifyAllSubscribers('SIGNED_IN') | ||
} | ||
@@ -431,17 +410,8 @@ } | ||
const { data, error } = yield this.api.refreshAccessToken(refresh_token); | ||
if (data === null || data === void 0 ? void 0 : data.access_token) { | ||
this.currentSession = data; | ||
this.currentUser = this.currentSession.user; | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
const tokenExpirySeconds = data.expires_in; | ||
if (this.autoRefreshToken && tokenExpirySeconds) { | ||
setTimeout(() => this._callRefreshToken(), (tokenExpirySeconds - 60) * 1000); | ||
} | ||
if (this.persistSession && this.currentUser) { | ||
this._persistSession(this.currentSession); | ||
} | ||
} | ||
else { | ||
if (error) | ||
throw error; | ||
} | ||
if (!data) | ||
throw Error('Invalid session data.'); | ||
this._saveSession(data); | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
return { data, error: null }; | ||
@@ -457,4 +427,45 @@ } | ||
} | ||
/** | ||
* set currentSession and currentUser | ||
* process to _startAutoRefreshToken if possible | ||
*/ | ||
_saveSession(session) { | ||
this.currentSession = session; | ||
this.currentUser = session.user; | ||
const expiresAt = session.expires_at; | ||
const timeNow = Math.round(Date.now() / 1000); | ||
if (expiresAt) | ||
this._startAutoRefreshToken((expiresAt - timeNow - 60) * 1000); | ||
// Do we need any extra check before persist session | ||
// access_token or user ? | ||
if (this.persistSession && session.expires_at) { | ||
this._persistSession(this.currentSession); | ||
} | ||
} | ||
_persistSession(currentSession) { | ||
const data = { currentSession, expiresAt: currentSession.expires_at }; | ||
helpers_1.isBrowser() && this.localStorage.setItem(constants_1.STORAGE_KEY, JSON.stringify(data)); | ||
} | ||
_removeSession() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.currentSession = null; | ||
this.currentUser = null; | ||
if (this.refreshTokenTimer) | ||
clearTimeout(this.refreshTokenTimer); | ||
helpers_1.isBrowser() && (yield this.localStorage.removeItem(constants_1.STORAGE_KEY)); | ||
}); | ||
} | ||
/** | ||
* Clear and re-create refresh token timer | ||
* @param value time intervals in milliseconds | ||
*/ | ||
_startAutoRefreshToken(value) { | ||
if (this.refreshTokenTimer) | ||
clearTimeout(this.refreshTokenTimer); | ||
if (!value || !this.autoRefreshToken) | ||
return; | ||
this.refreshTokenTimer = setTimeout(() => this._callRefreshToken(), value); | ||
} | ||
} | ||
exports.default = GoTrueClient; | ||
//# sourceMappingURL=GoTrueClient.js.map |
@@ -21,2 +21,3 @@ import GoTrueApi from './GoTrueApi'; | ||
protected stateChangeEmitters: Map<string, Subscription>; | ||
protected refreshTokenTimer?: ReturnType<typeof setTimeout>; | ||
/** | ||
@@ -132,5 +133,2 @@ * Create a new client for use in the browser. | ||
private _handleProviderSignIn; | ||
private _saveSession; | ||
private _persistSession; | ||
private _removeSession; | ||
/** | ||
@@ -143,2 +141,3 @@ * Attempts to get the session from LocalStorage | ||
* Recovers the session from LocalStorage and refreshes | ||
* Note: this method is async to accommodate for AsyncStorage e.g. in React native. | ||
*/ | ||
@@ -148,3 +147,15 @@ private _recoverAndRefresh; | ||
private _notifyAllSubscribers; | ||
/** | ||
* set currentSession and currentUser | ||
* process to _startAutoRefreshToken if possible | ||
*/ | ||
private _saveSession; | ||
private _persistSession; | ||
private _removeSession; | ||
/** | ||
* Clear and re-create refresh token timer | ||
* @param value time intervals in milliseconds | ||
*/ | ||
private _startAutoRefreshToken; | ||
} | ||
//# sourceMappingURL=GoTrueClient.d.ts.map |
@@ -156,7 +156,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
throw new Error('Not logged in.'); | ||
yield this._callRefreshToken(); | ||
const { data, error } = yield this.api.getUser(this.currentSession.access_token); | ||
// currentSession and currentUser will be updated to latest on _callRefreshToken | ||
const { error } = yield this._callRefreshToken(); | ||
if (error) | ||
throw error; | ||
this.currentUser = data; | ||
return { data: this.currentSession, user: this.currentUser, error: null }; | ||
@@ -178,11 +177,11 @@ } | ||
throw new Error('Not logged in.'); | ||
const { data, error } = yield this.api.updateUser(this.currentSession.access_token, attributes); | ||
const { user, error } = yield this.api.updateUser(this.currentSession.access_token, attributes); | ||
if (error) | ||
throw error; | ||
this.currentUser = data; | ||
const session = Object.assign(Object.assign({}, this.currentSession), { user: data }); | ||
this.currentSession = session; | ||
if (!user) | ||
throw Error('Invalid user data.'); | ||
const session = Object.assign(Object.assign({}, this.currentSession), { user }); | ||
this._saveSession(session); | ||
this._notifyAllSubscribers('USER_UPDATED'); | ||
return { data, user: this.currentUser, error: null }; | ||
return { data: user, user, error: null }; | ||
} | ||
@@ -328,20 +327,2 @@ catch (error) { | ||
} | ||
_saveSession(session) { | ||
this.currentSession = session; | ||
this.currentUser = session.user; | ||
if (this.persistSession && session.expires_at) { | ||
this._persistSession(this.currentSession); | ||
} | ||
} | ||
_persistSession(currentSession) { | ||
const data = { currentSession, expiresAt: currentSession.expires_at }; | ||
isBrowser() && this.localStorage.setItem(STORAGE_KEY, JSON.stringify(data)); | ||
} | ||
_removeSession() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.currentSession = null; | ||
this.currentUser = null; | ||
isBrowser() && (yield this.localStorage.removeItem(STORAGE_KEY)); | ||
}); | ||
} | ||
/** | ||
@@ -361,5 +342,5 @@ * Attempts to get the session from LocalStorage | ||
const timeNow = Math.round(Date.now() / 1000); | ||
if (expiresAt > timeNow && (currentSession === null || currentSession === void 0 ? void 0 : currentSession.user)) { | ||
this.currentSession = currentSession; | ||
this.currentUser = currentSession.user; | ||
if (expiresAt >= timeNow && (currentSession === null || currentSession === void 0 ? void 0 : currentSession.user)) { | ||
this._saveSession(currentSession); | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
} | ||
@@ -373,11 +354,11 @@ } | ||
* Recovers the session from LocalStorage and refreshes | ||
* Note: this method is async to accommodate for AsyncStorage e.g. in React native. | ||
*/ | ||
_recoverAndRefresh() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// Note: this method is async to accommodate for AsyncStorage e.g. in React native. | ||
const json = isBrowser() && (yield this.localStorage.getItem(STORAGE_KEY)); | ||
if (!json) { | ||
return null; | ||
} | ||
try { | ||
const json = isBrowser() && (yield this.localStorage.getItem(STORAGE_KEY)); | ||
if (!json) { | ||
return null; | ||
} | ||
const data = JSON.parse(json); | ||
@@ -403,7 +384,5 @@ const { currentSession, expiresAt } = data; | ||
else { | ||
this.currentSession = currentSession; | ||
this.currentUser = currentSession.user; | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
// schedule a refresh 60 seconds before token due to expire | ||
setTimeout(() => this._callRefreshToken(), (expiresAt - timeNow - 60) * 1000); | ||
// should be handle on _recoverSession method already | ||
// this._saveSession(currentSession) | ||
// this._notifyAllSubscribers('SIGNED_IN') | ||
} | ||
@@ -426,17 +405,8 @@ } | ||
const { data, error } = yield this.api.refreshAccessToken(refresh_token); | ||
if (data === null || data === void 0 ? void 0 : data.access_token) { | ||
this.currentSession = data; | ||
this.currentUser = this.currentSession.user; | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
const tokenExpirySeconds = data.expires_in; | ||
if (this.autoRefreshToken && tokenExpirySeconds) { | ||
setTimeout(() => this._callRefreshToken(), (tokenExpirySeconds - 60) * 1000); | ||
} | ||
if (this.persistSession && this.currentUser) { | ||
this._persistSession(this.currentSession); | ||
} | ||
} | ||
else { | ||
if (error) | ||
throw error; | ||
} | ||
if (!data) | ||
throw Error('Invalid session data.'); | ||
this._saveSession(data); | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
return { data, error: null }; | ||
@@ -452,3 +422,44 @@ } | ||
} | ||
/** | ||
* set currentSession and currentUser | ||
* process to _startAutoRefreshToken if possible | ||
*/ | ||
_saveSession(session) { | ||
this.currentSession = session; | ||
this.currentUser = session.user; | ||
const expiresAt = session.expires_at; | ||
const timeNow = Math.round(Date.now() / 1000); | ||
if (expiresAt) | ||
this._startAutoRefreshToken((expiresAt - timeNow - 60) * 1000); | ||
// Do we need any extra check before persist session | ||
// access_token or user ? | ||
if (this.persistSession && session.expires_at) { | ||
this._persistSession(this.currentSession); | ||
} | ||
} | ||
_persistSession(currentSession) { | ||
const data = { currentSession, expiresAt: currentSession.expires_at }; | ||
isBrowser() && this.localStorage.setItem(STORAGE_KEY, JSON.stringify(data)); | ||
} | ||
_removeSession() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.currentSession = null; | ||
this.currentUser = null; | ||
if (this.refreshTokenTimer) | ||
clearTimeout(this.refreshTokenTimer); | ||
isBrowser() && (yield this.localStorage.removeItem(STORAGE_KEY)); | ||
}); | ||
} | ||
/** | ||
* Clear and re-create refresh token timer | ||
* @param value time intervals in milliseconds | ||
*/ | ||
_startAutoRefreshToken(value) { | ||
if (this.refreshTokenTimer) | ||
clearTimeout(this.refreshTokenTimer); | ||
if (!value || !this.autoRefreshToken) | ||
return; | ||
this.refreshTokenTimer = setTimeout(() => this._callRefreshToken(), value); | ||
} | ||
} | ||
//# sourceMappingURL=GoTrueClient.js.map |
{ | ||
"name": "@supabase/gotrue-js", | ||
"version": "1.12.3", | ||
"version": "1.12.4", | ||
"description": "Isomorphic GoTrue client", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
188846
2910