@supabase/gotrue-js
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -1,2 +0,2 @@ | ||
import { UserAttributes } from './lib/types'; | ||
import { Provider, UserAttributes } from './lib/types'; | ||
export default class Api { | ||
@@ -10,2 +10,4 @@ url: string; | ||
* Creates a new user using their email address. | ||
* @param email The email address of the user. | ||
* @param password The password of the user. | ||
*/ | ||
@@ -19,2 +21,7 @@ signUpWithEmail(email: string, password: string): Promise<{ | ||
}>; | ||
/** | ||
* Logs in an existing using their email address. | ||
* @param email The email address of the user. | ||
* @param password The password of the user. | ||
*/ | ||
signInWithEmail(email: string, password: string): Promise<{ | ||
@@ -27,2 +34,6 @@ data: any; | ||
}>; | ||
/** | ||
* Sends a reset request to an email address. | ||
* @param email The email address of the user. | ||
*/ | ||
resetPasswordForEmail(email: string): Promise<{ | ||
@@ -35,2 +46,6 @@ data: any; | ||
}>; | ||
/** | ||
* Removes a logged-in session. | ||
* @param jwt A valid, logged-in JWT. | ||
*/ | ||
signOut(jwt: string): Promise<{ | ||
@@ -43,6 +58,23 @@ data: unknown; | ||
}>; | ||
/** | ||
* Generates the relevant login URL for a third-party provider. | ||
* @param provider One of the providers supported by GoTrue. | ||
*/ | ||
getUrlForProvider(provider: Provider): string; | ||
/** | ||
* Gets the user details. | ||
* @param jwt A valid, logged-in JWT. | ||
*/ | ||
getUser(jwt: string): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
} | undefined>; | ||
}>; | ||
/** | ||
* Updates the user data. | ||
* @param jwt A valid, logged-in JWT. | ||
* @param attributes The data you want to update. | ||
*/ | ||
updateUser(jwt: string, attributes: UserAttributes): Promise<{ | ||
@@ -55,2 +87,6 @@ data: any; | ||
}>; | ||
/** | ||
* Generates a new JWT. | ||
* @param refreshToken A valid refresh token that was returned on login. | ||
*/ | ||
refreshAccessToken(refreshToken: string): Promise<{ | ||
@@ -57,0 +93,0 @@ data: any; |
@@ -20,2 +20,4 @@ "use strict"; | ||
* Creates a new user using their email address. | ||
* @param email The email address of the user. | ||
* @param password The password of the user. | ||
*/ | ||
@@ -26,9 +28,16 @@ signUpWithEmail(email, password) { | ||
let data = yield fetch_1.post(`${this.url}/signup`, { email, password }, { headers: this.headers }); | ||
console.log('signUpWithEmail data', data); | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
console.log('signUpWithEmail error', error); | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Logs in an existing using their email address. | ||
* @param email The email address of the user. | ||
* @param password The password of the user. | ||
*/ | ||
signInWithEmail(email, password) { | ||
@@ -38,9 +47,14 @@ return __awaiter(this, void 0, void 0, function* () { | ||
let data = yield fetch_1.post(`${this.url}/token?grant_type=password`, { email, password }, { headers: this.headers }); | ||
console.log('signInWithEmaildata', data); | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Sends a reset request to an email address. | ||
* @param email The email address of the user. | ||
*/ | ||
resetPasswordForEmail(email) { | ||
@@ -53,6 +67,10 @@ return __awaiter(this, void 0, void 0, function* () { | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Removes a logged-in session. | ||
* @param jwt A valid, logged-in JWT. | ||
*/ | ||
signOut(jwt) { | ||
@@ -63,3 +81,2 @@ return __awaiter(this, void 0, void 0, function* () { | ||
headers['Authorization'] = `Bearer ${jwt}`; | ||
console.log('headers', headers); | ||
let data = yield fetch_1.post(`${this.url}/logout`, {}, { headers, noResolveJson: true }); | ||
@@ -69,6 +86,17 @@ return { data, error: null }; | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Generates the relevant login URL for a third-party provider. | ||
* @param provider One of the providers supported by GoTrue. | ||
*/ | ||
getUrlForProvider(provider) { | ||
return `${this.url}/authorize?provider=${provider}`; | ||
} | ||
/** | ||
* Gets the user details. | ||
* @param jwt A valid, logged-in JWT. | ||
*/ | ||
getUser(jwt) { | ||
@@ -80,8 +108,14 @@ return __awaiter(this, void 0, void 0, function* () { | ||
let data = yield fetch_1.get(`${this.url}/user`, { headers }); | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Updates the user data. | ||
* @param jwt A valid, logged-in JWT. | ||
* @param attributes The data you want to update. | ||
*/ | ||
updateUser(jwt, attributes) { | ||
@@ -96,6 +130,10 @@ return __awaiter(this, void 0, void 0, function* () { | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Generates a new JWT. | ||
* @param refreshToken A valid refresh token that was returned on login. | ||
*/ | ||
refreshAccessToken(refreshToken) { | ||
@@ -108,3 +146,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
@@ -111,0 +149,0 @@ }); |
import Api from './Api'; | ||
import { Session, User, UserAttributes } from './lib/types'; | ||
import { Session, User, UserAttributes, Provider, Subscription } from './lib/types'; | ||
export default class Client { | ||
@@ -9,4 +9,27 @@ api: Api; | ||
persistSession: boolean; | ||
constructor({ url, autoRefreshToken, persistSession, headers, }: any); | ||
signUp({ email, password }: { | ||
stateChangeEmmitters: Map<string, Subscription>; | ||
/** | ||
* Create a new client for use in the browser. | ||
* @param options.url The URL of the GoTrue server. | ||
* @param options.headers Any additional headers to send to the GoTrue server. | ||
* @param options.detectSessionInUrl Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user. | ||
* @param options.autoRefreshToken Set to "true" if you want to automatically refresh the token before expiring. | ||
* @param options.persistSession Set to "true" if you want to automatically save the user session into local storage. | ||
*/ | ||
constructor(options: { | ||
url?: string; | ||
headers?: { | ||
[key: string]: string; | ||
}; | ||
detectSessionInUrl?: boolean; | ||
autoRefreshToken?: boolean; | ||
persistSession?: boolean; | ||
}); | ||
/** | ||
* Creates a new user. | ||
* @param credentials The user login details. | ||
* @param credentials.email The user's email address. | ||
* @param credentials.password The user's password. | ||
*/ | ||
signUp(credentials: { | ||
email: string; | ||
@@ -21,12 +44,23 @@ password: string; | ||
}>; | ||
signIn({ email, password }: { | ||
email: string; | ||
password: string; | ||
/** | ||
* Log in an existing user, or login via a third-party provider. | ||
* @param credentials The user login details. | ||
* @param credentials.email The user's email address. | ||
* @param credentials.password The user's password. | ||
* @param credentials.provider One of the providers supported by GoTrue. | ||
*/ | ||
signIn(credentials: { | ||
email?: string; | ||
password?: string; | ||
provider?: Provider; | ||
}): Promise<{ | ||
data: null; | ||
error: any; | ||
} | { | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
/** | ||
* Returns the user data, if there is a logged in user. | ||
*/ | ||
user(): Promise<{ | ||
@@ -39,2 +73,5 @@ data: User | null; | ||
}>; | ||
/** | ||
* Updates user data, if there is a logged in user. | ||
*/ | ||
update(attributes: UserAttributes): Promise<{ | ||
@@ -47,2 +84,18 @@ data: User | null; | ||
}>; | ||
/** | ||
* Gets the session data from a URL string | ||
* @param options.storeSession Optionally store the session in the browser | ||
*/ | ||
getSessionFromUrl(options?: { | ||
storeSession?: boolean; | ||
}): Promise<{ | ||
data: Session; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
/** | ||
* Signs out the current user, if there is a logged in user. | ||
*/ | ||
signOut(): Promise<true | { | ||
@@ -52,8 +105,8 @@ data: null; | ||
}>; | ||
_saveSession(session: Session): void; | ||
_persistSession(currentSession: Session, currentUser: User, secondsToExpiry: number): void; | ||
_removeSession(): void; | ||
_recoverSession(): null; | ||
_callRefreshToken(): Promise<{ | ||
data: any; | ||
/** | ||
* Receive a notification every time an auth event happens. | ||
* @returns {Subscription} A subscription object which can be used to unsubcribe itself. | ||
*/ | ||
onAuthStateChange(callback: Function): { | ||
data: Subscription | undefined; | ||
error: null; | ||
@@ -63,4 +116,12 @@ } | { | ||
error: any; | ||
} | undefined>; | ||
}; | ||
private _handeEmailSignIn; | ||
private _handeProviderSignIn; | ||
private _saveSession; | ||
private _persistSession; | ||
private _removeSession; | ||
private _recoverSession; | ||
private _callRefreshToken; | ||
private _notifyAllSubscribers; | ||
} | ||
//# sourceMappingURL=Client.d.ts.map |
@@ -18,13 +18,45 @@ "use strict"; | ||
const constants_1 = require("./lib/constants"); | ||
const constants_2 = require("./lib/constants"); | ||
const types_1 = require("./lib/types"); | ||
const DEFAULT_OPTIONS = { | ||
url: constants_1.GOTRUE_URL, | ||
autoRefreshToken: true, | ||
persistSession: true, | ||
detectSessionInUrl: true, | ||
headers: constants_1.DEFAULT_HEADERS, | ||
}; | ||
class Client { | ||
constructor({ url = constants_2.GOTRUE_URL, autoRefreshToken = true, persistSession = true, headers = constants_2.DEFAULT_HEADERS, }) { | ||
/** | ||
* Create a new client for use in the browser. | ||
* @param options.url The URL of the GoTrue server. | ||
* @param options.headers Any additional headers to send to the GoTrue server. | ||
* @param options.detectSessionInUrl Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user. | ||
* @param options.autoRefreshToken Set to "true" if you want to automatically refresh the token before expiring. | ||
* @param options.persistSession Set to "true" if you want to automatically save the user session into local storage. | ||
*/ | ||
constructor(options) { | ||
this.stateChangeEmmitters = new Map(); | ||
const settings = Object.assign(Object.assign({}, DEFAULT_OPTIONS), options); | ||
this.currentUser = null; | ||
this.currentSession = null; | ||
this.autoRefreshToken = autoRefreshToken; | ||
this.persistSession = persistSession; | ||
this.api = new Api_1.default({ url, headers }); | ||
this.autoRefreshToken = settings.autoRefreshToken; | ||
this.persistSession = settings.persistSession; | ||
this.api = new Api_1.default({ url: settings.url, headers: settings.headers }); | ||
this._recoverSession(); | ||
// Handle the OAuth redirect | ||
try { | ||
if (settings.detectSessionInUrl && helpers_1.isBrowser() && !!helpers_1.getParameterByName('access_token')) { | ||
this.getSessionFromUrl({ storeSession: true }); | ||
} | ||
} | ||
catch (error) { | ||
console.log('Error getting session from URL.'); | ||
} | ||
} | ||
signUp({ email, password }) { | ||
/** | ||
* Creates a new user. | ||
* @param credentials The user login details. | ||
* @param credentials.email The user's email address. | ||
* @param credentials.password The user's password. | ||
*/ | ||
signUp(credentials) { | ||
var _a; | ||
@@ -34,27 +66,43 @@ return __awaiter(this, void 0, void 0, function* () { | ||
this._removeSession(); | ||
let data = yield this.api.signUpWithEmail(email, password); | ||
if ((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.confirmed_at) | ||
let { data, error } = yield this.api.signUpWithEmail(credentials.email, credentials.password); | ||
if (error) | ||
throw new Error(error); | ||
if ((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.confirmed_at) { | ||
this._saveSession(data); | ||
this._notifyAllSubscribers(types_1.AuthChangeEvent.SIGNED_IN); | ||
} | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
signIn({ email, password }) { | ||
var _a; | ||
/** | ||
* Log in an existing user, or login via a third-party provider. | ||
* @param credentials The user login details. | ||
* @param credentials.email The user's email address. | ||
* @param credentials.password The user's password. | ||
* @param credentials.provider One of the providers supported by GoTrue. | ||
*/ | ||
signIn(credentials) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
this._removeSession(); | ||
let data = yield this.api.signInWithEmail(email, password); | ||
if ((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.confirmed_at) | ||
this._saveSession(data); | ||
return { data, error: null }; | ||
let { email, password, provider } = credentials; | ||
if (email && password) | ||
return this._handeEmailSignIn(email, password); | ||
if (provider) | ||
return this._handeProviderSignIn(provider); | ||
else | ||
throw new Error(`You must provide either an email or a third-party provider.`); | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Returns the user data, if there is a logged in user. | ||
*/ | ||
user() { | ||
@@ -66,3 +114,5 @@ var _a; | ||
throw new Error('Not logged in.'); | ||
let data = yield this.api.getUser(this.currentSession.access_token); | ||
let { data, error } = yield this.api.getUser(this.currentSession.access_token); | ||
if (error) | ||
throw new Error(error); | ||
this.currentUser = data; | ||
@@ -72,6 +122,9 @@ return { data: this.currentUser, error: null }; | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Updates user data, if there is a logged in user. | ||
*/ | ||
update(attributes) { | ||
@@ -83,11 +136,62 @@ var _a; | ||
throw new Error('Not logged in.'); | ||
let data = yield this.api.updateUser(this.currentSession.access_token, attributes); | ||
let { data, error } = yield this.api.updateUser(this.currentSession.access_token, attributes); | ||
if (error) | ||
throw new Error(error); | ||
this.currentUser = data; | ||
this._notifyAllSubscribers(types_1.AuthChangeEvent.USER_UPDATED); | ||
return { data: this.currentUser, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Gets the session data from a URL string | ||
* @param options.storeSession Optionally store the session in the browser | ||
*/ | ||
getSessionFromUrl(options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (!helpers_1.isBrowser()) | ||
throw new Error('No browser detected.'); | ||
const error_description = helpers_1.getParameterByName('error_description'); | ||
if (error_description) | ||
throw new Error(error_description); | ||
const access_token = helpers_1.getParameterByName('access_token'); | ||
const expires_in = helpers_1.getParameterByName('expires_in'); | ||
const refresh_token = helpers_1.getParameterByName('refresh_token'); | ||
const token_type = helpers_1.getParameterByName('token_type'); | ||
if (!access_token) | ||
throw new Error('No access_token detected.'); | ||
if (!expires_in) | ||
throw new Error('No expires_in detected.'); | ||
if (!refresh_token) | ||
throw new Error('No refresh_token detected.'); | ||
if (!token_type) | ||
throw new Error('No token_type detected.'); | ||
let { data: user, error } = yield this.api.getUser(access_token); | ||
if (error) | ||
throw new Error(error); | ||
const session = { | ||
access_token, | ||
expires_in: parseInt(expires_in), | ||
refresh_token, | ||
token_type, | ||
user, | ||
}; | ||
if (options === null || options === void 0 ? void 0 : options.storeSession) { | ||
this._saveSession(session); | ||
this._notifyAllSubscribers(types_1.AuthChangeEvent.SIGNED_IN); | ||
} | ||
return { data: session, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Signs out the current user, if there is a logged in user. | ||
*/ | ||
signOut() { | ||
@@ -100,12 +204,68 @@ return __awaiter(this, void 0, void 0, function* () { | ||
this._removeSession(); | ||
this._notifyAllSubscribers(types_1.AuthChangeEvent.SIGNED_OUT); | ||
return true; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Receive a notification every time an auth event happens. | ||
* @returns {Subscription} A subscription object which can be used to unsubcribe itself. | ||
*/ | ||
onAuthStateChange(callback) { | ||
try { | ||
const id = helpers_1.uuid(); | ||
let self = this; | ||
const subscription = { | ||
id, | ||
callback, | ||
unsubscribe: () => self.stateChangeEmmitters.delete(id), | ||
}; | ||
this.stateChangeEmmitters.set(id, subscription); | ||
return { data: this.stateChangeEmmitters.get(id), error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error }; | ||
} | ||
} | ||
_handeEmailSignIn(email, password) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
let { data, error } = yield this.api.signInWithEmail(email, password); | ||
if (!!error) | ||
return { data: null, error }; | ||
if ((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.confirmed_at) { | ||
this._saveSession(data); | ||
this._notifyAllSubscribers(types_1.AuthChangeEvent.SIGNED_IN); | ||
} | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
console.log('error_handeEmailSignIn', error); | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
_handeProviderSignIn(provider) { | ||
let url = this.api.getUrlForProvider(provider); | ||
try { | ||
// try to open on the browser | ||
if (helpers_1.isBrowser()) { | ||
window.location.href = url; | ||
} | ||
return { data: url, error: null }; | ||
} | ||
catch (error) { | ||
// fallback to returning the URL | ||
if (!!url) | ||
return { data: url, error: null }; | ||
else | ||
return { data: null, error }; | ||
} | ||
} | ||
_saveSession(session) { | ||
this.currentSession = session; | ||
this.currentUser = session['user']; | ||
let tokenExpirySeconds = session['expires_in']; | ||
@@ -116,9 +276,9 @@ if (this.autoRefreshToken && tokenExpirySeconds) { | ||
if (this.persistSession) { | ||
this._persistSession(this.currentSession, this.currentUser, tokenExpirySeconds); | ||
this._persistSession(this.currentSession, tokenExpirySeconds); | ||
} | ||
} | ||
_persistSession(currentSession, currentUser, secondsToExpiry) { | ||
_persistSession(currentSession, secondsToExpiry) { | ||
const timeNow = Math.round(Date.now() / 1000); | ||
const expiresAt = timeNow + secondsToExpiry; | ||
const data = { currentSession, currentUser, expiresAt }; | ||
const data = { currentSession, expiresAt }; | ||
helpers_1.isBrowser() && localStorage.setItem(constants_1.STORAGE_KEY, JSON.stringify(data)); | ||
@@ -170,3 +330,3 @@ } | ||
if (this.persistSession && this.currentUser) { | ||
this._persistSession(this.currentSession, this.currentUser, tokenExpirySeconds); | ||
this._persistSession(this.currentSession, tokenExpirySeconds); | ||
} | ||
@@ -178,8 +338,11 @@ } | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
_notifyAllSubscribers(event) { | ||
this.stateChangeEmmitters.forEach((x) => x.callback(event, this.currentSession)); | ||
} | ||
} | ||
exports.default = Client; | ||
//# sourceMappingURL=Client.js.map |
@@ -17,2 +17,12 @@ "use strict"; | ||
const isomorphic_unfetch_1 = __importDefault(require("isomorphic-unfetch")); | ||
const handleError = (error, reject) => { | ||
if (typeof error.json === 'function') { | ||
error.json().then((msg) => { | ||
return reject(new Error(msg.error_description)); | ||
}); | ||
} | ||
else { | ||
return reject(error); | ||
} | ||
}; | ||
function get(url, options) { | ||
@@ -25,5 +35,12 @@ return __awaiter(this, void 0, void 0, function* () { | ||
}) | ||
.then((r) => r.json()) | ||
.then((result) => { | ||
if (!result.ok) | ||
throw result; | ||
else if (options === null || options === void 0 ? void 0 : options.noResolveJson) | ||
return resolve; | ||
else | ||
return result.json(); | ||
}) | ||
.then((data) => resolve(data)) | ||
.catch((error) => reject(error)); | ||
.catch((error) => handleError(error, reject)); | ||
}); | ||
@@ -41,5 +58,12 @@ }); | ||
}) | ||
.then((r) => (!(options === null || options === void 0 ? void 0 : options.noResolveJson) ? r.json() : null)) | ||
.then((result) => { | ||
if (!result.ok) | ||
throw result; | ||
else if (options === null || options === void 0 ? void 0 : options.noResolveJson) | ||
return resolve; | ||
else | ||
return result.json(); | ||
}) | ||
.then((data) => resolve(data)) | ||
.catch((error) => reject(error)); | ||
.catch((error) => handleError(error, reject)); | ||
}); | ||
@@ -57,5 +81,12 @@ }); | ||
}) | ||
.then((r) => (!(options === null || options === void 0 ? void 0 : options.noResolveJson) ? r.json() : {})) | ||
.then((result) => { | ||
if (!result.ok) | ||
throw result; | ||
else if (options === null || options === void 0 ? void 0 : options.noResolveJson) | ||
return resolve; | ||
else | ||
return result.json(); | ||
}) | ||
.then((data) => resolve(data)) | ||
.catch((error) => reject(error)); | ||
.catch((error) => handleError(error, reject)); | ||
}); | ||
@@ -62,0 +93,0 @@ }); |
export declare function uuid(): string; | ||
export declare const isBrowser: () => boolean; | ||
export declare function getParameterByName(name: string, url?: string): string | null; | ||
//# sourceMappingURL=helpers.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isBrowser = exports.uuid = void 0; | ||
exports.getParameterByName = exports.isBrowser = exports.uuid = void 0; | ||
function uuid() { | ||
@@ -12,2 +12,14 @@ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
exports.isBrowser = () => typeof window !== 'undefined'; | ||
function getParameterByName(name, url) { | ||
if (!url) | ||
url = window.location.href; | ||
name = name.replace(/[\[\]]/g, '\\$&'); | ||
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), results = regex.exec(url); | ||
if (!results) | ||
return null; | ||
if (!results[2]) | ||
return ''; | ||
return decodeURIComponent(results[2].replace(/\+/g, ' ')); | ||
} | ||
exports.getParameterByName = getParameterByName; | ||
//# sourceMappingURL=helpers.js.map |
@@ -0,1 +1,12 @@ | ||
export declare enum Provider { | ||
BITBUCKET = "bitbucket", | ||
GITHUB = "github", | ||
GITLAB = "gitlab", | ||
GOOGLE = "google" | ||
} | ||
export declare enum AuthChangeEvent { | ||
SIGNED_IN = "SIGNED_IN", | ||
SIGNED_OUT = "SIGNED_OUT", | ||
USER_UPDATED = "USER_UPDATED" | ||
} | ||
export interface Session { | ||
@@ -21,7 +32,33 @@ access_token: string; | ||
export interface UserAttributes { | ||
/** | ||
* The user's email. | ||
*/ | ||
email?: string; | ||
/** | ||
* The user's password. | ||
*/ | ||
password?: string; | ||
/** | ||
* An email change token. | ||
*/ | ||
email_change_token?: string; | ||
/** | ||
* A custom data object. Can be any JSON. | ||
*/ | ||
data?: object; | ||
} | ||
export interface Subscription { | ||
/** | ||
* The subscriber UUID. This will be set by the client. | ||
*/ | ||
id: string; | ||
/** | ||
* The function to call every time there is an event. eg: (eventName) => {} | ||
*/ | ||
callback: Function; | ||
/** | ||
* Call this to remove the listener. | ||
*/ | ||
unsubscribe: Function; | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AuthChangeEvent = exports.Provider = void 0; | ||
var Provider; | ||
(function (Provider) { | ||
Provider["BITBUCKET"] = "bitbucket"; | ||
Provider["GITHUB"] = "github"; | ||
Provider["GITLAB"] = "gitlab"; | ||
Provider["GOOGLE"] = "google"; | ||
})(Provider = exports.Provider || (exports.Provider = {})); | ||
var AuthChangeEvent; | ||
(function (AuthChangeEvent) { | ||
AuthChangeEvent["SIGNED_IN"] = "SIGNED_IN"; | ||
AuthChangeEvent["SIGNED_OUT"] = "SIGNED_OUT"; | ||
AuthChangeEvent["USER_UPDATED"] = "USER_UPDATED"; | ||
})(AuthChangeEvent = exports.AuthChangeEvent || (exports.AuthChangeEvent = {})); | ||
//# sourceMappingURL=types.js.map |
@@ -1,2 +0,2 @@ | ||
import { UserAttributes } from './lib/types'; | ||
import { Provider, UserAttributes } from './lib/types'; | ||
export default class Api { | ||
@@ -10,2 +10,4 @@ url: string; | ||
* Creates a new user using their email address. | ||
* @param email The email address of the user. | ||
* @param password The password of the user. | ||
*/ | ||
@@ -19,2 +21,7 @@ signUpWithEmail(email: string, password: string): Promise<{ | ||
}>; | ||
/** | ||
* Logs in an existing using their email address. | ||
* @param email The email address of the user. | ||
* @param password The password of the user. | ||
*/ | ||
signInWithEmail(email: string, password: string): Promise<{ | ||
@@ -27,2 +34,6 @@ data: any; | ||
}>; | ||
/** | ||
* Sends a reset request to an email address. | ||
* @param email The email address of the user. | ||
*/ | ||
resetPasswordForEmail(email: string): Promise<{ | ||
@@ -35,2 +46,6 @@ data: any; | ||
}>; | ||
/** | ||
* Removes a logged-in session. | ||
* @param jwt A valid, logged-in JWT. | ||
*/ | ||
signOut(jwt: string): Promise<{ | ||
@@ -43,6 +58,23 @@ data: unknown; | ||
}>; | ||
/** | ||
* Generates the relevant login URL for a third-party provider. | ||
* @param provider One of the providers supported by GoTrue. | ||
*/ | ||
getUrlForProvider(provider: Provider): string; | ||
/** | ||
* Gets the user details. | ||
* @param jwt A valid, logged-in JWT. | ||
*/ | ||
getUser(jwt: string): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
} | undefined>; | ||
}>; | ||
/** | ||
* Updates the user data. | ||
* @param jwt A valid, logged-in JWT. | ||
* @param attributes The data you want to update. | ||
*/ | ||
updateUser(jwt: string, attributes: UserAttributes): Promise<{ | ||
@@ -55,2 +87,6 @@ data: any; | ||
}>; | ||
/** | ||
* Generates a new JWT. | ||
* @param refreshToken A valid refresh token that was returned on login. | ||
*/ | ||
refreshAccessToken(refreshToken: string): Promise<{ | ||
@@ -57,0 +93,0 @@ data: any; |
@@ -18,2 +18,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
* Creates a new user using their email address. | ||
* @param email The email address of the user. | ||
* @param password The password of the user. | ||
*/ | ||
@@ -24,9 +26,16 @@ signUpWithEmail(email, password) { | ||
let data = yield post(`${this.url}/signup`, { email, password }, { headers: this.headers }); | ||
console.log('signUpWithEmail data', data); | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
console.log('signUpWithEmail error', error); | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Logs in an existing using their email address. | ||
* @param email The email address of the user. | ||
* @param password The password of the user. | ||
*/ | ||
signInWithEmail(email, password) { | ||
@@ -36,9 +45,14 @@ return __awaiter(this, void 0, void 0, function* () { | ||
let data = yield post(`${this.url}/token?grant_type=password`, { email, password }, { headers: this.headers }); | ||
console.log('signInWithEmaildata', data); | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Sends a reset request to an email address. | ||
* @param email The email address of the user. | ||
*/ | ||
resetPasswordForEmail(email) { | ||
@@ -51,6 +65,10 @@ return __awaiter(this, void 0, void 0, function* () { | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Removes a logged-in session. | ||
* @param jwt A valid, logged-in JWT. | ||
*/ | ||
signOut(jwt) { | ||
@@ -61,3 +79,2 @@ return __awaiter(this, void 0, void 0, function* () { | ||
headers['Authorization'] = `Bearer ${jwt}`; | ||
console.log('headers', headers); | ||
let data = yield post(`${this.url}/logout`, {}, { headers, noResolveJson: true }); | ||
@@ -67,6 +84,17 @@ return { data, error: null }; | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Generates the relevant login URL for a third-party provider. | ||
* @param provider One of the providers supported by GoTrue. | ||
*/ | ||
getUrlForProvider(provider) { | ||
return `${this.url}/authorize?provider=${provider}`; | ||
} | ||
/** | ||
* Gets the user details. | ||
* @param jwt A valid, logged-in JWT. | ||
*/ | ||
getUser(jwt) { | ||
@@ -78,8 +106,14 @@ return __awaiter(this, void 0, void 0, function* () { | ||
let data = yield get(`${this.url}/user`, { headers }); | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Updates the user data. | ||
* @param jwt A valid, logged-in JWT. | ||
* @param attributes The data you want to update. | ||
*/ | ||
updateUser(jwt, attributes) { | ||
@@ -94,6 +128,10 @@ return __awaiter(this, void 0, void 0, function* () { | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Generates a new JWT. | ||
* @param refreshToken A valid refresh token that was returned on login. | ||
*/ | ||
refreshAccessToken(refreshToken) { | ||
@@ -106,3 +144,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
@@ -109,0 +147,0 @@ }); |
import Api from './Api'; | ||
import { Session, User, UserAttributes } from './lib/types'; | ||
import { Session, User, UserAttributes, Provider, Subscription } from './lib/types'; | ||
export default class Client { | ||
@@ -9,4 +9,27 @@ api: Api; | ||
persistSession: boolean; | ||
constructor({ url, autoRefreshToken, persistSession, headers, }: any); | ||
signUp({ email, password }: { | ||
stateChangeEmmitters: Map<string, Subscription>; | ||
/** | ||
* Create a new client for use in the browser. | ||
* @param options.url The URL of the GoTrue server. | ||
* @param options.headers Any additional headers to send to the GoTrue server. | ||
* @param options.detectSessionInUrl Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user. | ||
* @param options.autoRefreshToken Set to "true" if you want to automatically refresh the token before expiring. | ||
* @param options.persistSession Set to "true" if you want to automatically save the user session into local storage. | ||
*/ | ||
constructor(options: { | ||
url?: string; | ||
headers?: { | ||
[key: string]: string; | ||
}; | ||
detectSessionInUrl?: boolean; | ||
autoRefreshToken?: boolean; | ||
persistSession?: boolean; | ||
}); | ||
/** | ||
* Creates a new user. | ||
* @param credentials The user login details. | ||
* @param credentials.email The user's email address. | ||
* @param credentials.password The user's password. | ||
*/ | ||
signUp(credentials: { | ||
email: string; | ||
@@ -21,12 +44,23 @@ password: string; | ||
}>; | ||
signIn({ email, password }: { | ||
email: string; | ||
password: string; | ||
/** | ||
* Log in an existing user, or login via a third-party provider. | ||
* @param credentials The user login details. | ||
* @param credentials.email The user's email address. | ||
* @param credentials.password The user's password. | ||
* @param credentials.provider One of the providers supported by GoTrue. | ||
*/ | ||
signIn(credentials: { | ||
email?: string; | ||
password?: string; | ||
provider?: Provider; | ||
}): Promise<{ | ||
data: null; | ||
error: any; | ||
} | { | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
/** | ||
* Returns the user data, if there is a logged in user. | ||
*/ | ||
user(): Promise<{ | ||
@@ -39,2 +73,5 @@ data: User | null; | ||
}>; | ||
/** | ||
* Updates user data, if there is a logged in user. | ||
*/ | ||
update(attributes: UserAttributes): Promise<{ | ||
@@ -47,2 +84,18 @@ data: User | null; | ||
}>; | ||
/** | ||
* Gets the session data from a URL string | ||
* @param options.storeSession Optionally store the session in the browser | ||
*/ | ||
getSessionFromUrl(options?: { | ||
storeSession?: boolean; | ||
}): Promise<{ | ||
data: Session; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
/** | ||
* Signs out the current user, if there is a logged in user. | ||
*/ | ||
signOut(): Promise<true | { | ||
@@ -52,8 +105,8 @@ data: null; | ||
}>; | ||
_saveSession(session: Session): void; | ||
_persistSession(currentSession: Session, currentUser: User, secondsToExpiry: number): void; | ||
_removeSession(): void; | ||
_recoverSession(): null; | ||
_callRefreshToken(): Promise<{ | ||
data: any; | ||
/** | ||
* Receive a notification every time an auth event happens. | ||
* @returns {Subscription} A subscription object which can be used to unsubcribe itself. | ||
*/ | ||
onAuthStateChange(callback: Function): { | ||
data: Subscription | undefined; | ||
error: null; | ||
@@ -63,4 +116,12 @@ } | { | ||
error: any; | ||
} | undefined>; | ||
}; | ||
private _handeEmailSignIn; | ||
private _handeProviderSignIn; | ||
private _saveSession; | ||
private _persistSession; | ||
private _removeSession; | ||
private _recoverSession; | ||
private _callRefreshToken; | ||
private _notifyAllSubscribers; | ||
} | ||
//# sourceMappingURL=Client.d.ts.map |
@@ -11,15 +11,47 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import Api from './Api'; | ||
import { isBrowser } from './lib/helpers'; | ||
import { STORAGE_KEY } from './lib/constants'; | ||
import { GOTRUE_URL, DEFAULT_HEADERS } from './lib/constants'; | ||
import { isBrowser, getParameterByName, uuid } from './lib/helpers'; | ||
import { GOTRUE_URL, DEFAULT_HEADERS, STORAGE_KEY } from './lib/constants'; | ||
import { AuthChangeEvent } from './lib/types'; | ||
const DEFAULT_OPTIONS = { | ||
url: GOTRUE_URL, | ||
autoRefreshToken: true, | ||
persistSession: true, | ||
detectSessionInUrl: true, | ||
headers: DEFAULT_HEADERS, | ||
}; | ||
export default class Client { | ||
constructor({ url = GOTRUE_URL, autoRefreshToken = true, persistSession = true, headers = DEFAULT_HEADERS, }) { | ||
/** | ||
* Create a new client for use in the browser. | ||
* @param options.url The URL of the GoTrue server. | ||
* @param options.headers Any additional headers to send to the GoTrue server. | ||
* @param options.detectSessionInUrl Set to "true" if you want to automatically detects OAuth grants in the URL and signs in the user. | ||
* @param options.autoRefreshToken Set to "true" if you want to automatically refresh the token before expiring. | ||
* @param options.persistSession Set to "true" if you want to automatically save the user session into local storage. | ||
*/ | ||
constructor(options) { | ||
this.stateChangeEmmitters = new Map(); | ||
const settings = Object.assign(Object.assign({}, DEFAULT_OPTIONS), options); | ||
this.currentUser = null; | ||
this.currentSession = null; | ||
this.autoRefreshToken = autoRefreshToken; | ||
this.persistSession = persistSession; | ||
this.api = new Api({ url, headers }); | ||
this.autoRefreshToken = settings.autoRefreshToken; | ||
this.persistSession = settings.persistSession; | ||
this.api = new Api({ url: settings.url, headers: settings.headers }); | ||
this._recoverSession(); | ||
// Handle the OAuth redirect | ||
try { | ||
if (settings.detectSessionInUrl && isBrowser() && !!getParameterByName('access_token')) { | ||
this.getSessionFromUrl({ storeSession: true }); | ||
} | ||
} | ||
catch (error) { | ||
console.log('Error getting session from URL.'); | ||
} | ||
} | ||
signUp({ email, password }) { | ||
/** | ||
* Creates a new user. | ||
* @param credentials The user login details. | ||
* @param credentials.email The user's email address. | ||
* @param credentials.password The user's password. | ||
*/ | ||
signUp(credentials) { | ||
var _a; | ||
@@ -29,27 +61,43 @@ return __awaiter(this, void 0, void 0, function* () { | ||
this._removeSession(); | ||
let data = yield this.api.signUpWithEmail(email, password); | ||
if ((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.confirmed_at) | ||
let { data, error } = yield this.api.signUpWithEmail(credentials.email, credentials.password); | ||
if (error) | ||
throw new Error(error); | ||
if ((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.confirmed_at) { | ||
this._saveSession(data); | ||
this._notifyAllSubscribers(AuthChangeEvent.SIGNED_IN); | ||
} | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
signIn({ email, password }) { | ||
var _a; | ||
/** | ||
* Log in an existing user, or login via a third-party provider. | ||
* @param credentials The user login details. | ||
* @param credentials.email The user's email address. | ||
* @param credentials.password The user's password. | ||
* @param credentials.provider One of the providers supported by GoTrue. | ||
*/ | ||
signIn(credentials) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
this._removeSession(); | ||
let data = yield this.api.signInWithEmail(email, password); | ||
if ((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.confirmed_at) | ||
this._saveSession(data); | ||
return { data, error: null }; | ||
let { email, password, provider } = credentials; | ||
if (email && password) | ||
return this._handeEmailSignIn(email, password); | ||
if (provider) | ||
return this._handeProviderSignIn(provider); | ||
else | ||
throw new Error(`You must provide either an email or a third-party provider.`); | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Returns the user data, if there is a logged in user. | ||
*/ | ||
user() { | ||
@@ -61,3 +109,5 @@ var _a; | ||
throw new Error('Not logged in.'); | ||
let data = yield this.api.getUser(this.currentSession.access_token); | ||
let { data, error } = yield this.api.getUser(this.currentSession.access_token); | ||
if (error) | ||
throw new Error(error); | ||
this.currentUser = data; | ||
@@ -67,6 +117,9 @@ return { data: this.currentUser, error: null }; | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Updates user data, if there is a logged in user. | ||
*/ | ||
update(attributes) { | ||
@@ -78,11 +131,62 @@ var _a; | ||
throw new Error('Not logged in.'); | ||
let data = yield this.api.updateUser(this.currentSession.access_token, attributes); | ||
let { data, error } = yield this.api.updateUser(this.currentSession.access_token, attributes); | ||
if (error) | ||
throw new Error(error); | ||
this.currentUser = data; | ||
this._notifyAllSubscribers(AuthChangeEvent.USER_UPDATED); | ||
return { data: this.currentUser, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Gets the session data from a URL string | ||
* @param options.storeSession Optionally store the session in the browser | ||
*/ | ||
getSessionFromUrl(options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (!isBrowser()) | ||
throw new Error('No browser detected.'); | ||
const error_description = getParameterByName('error_description'); | ||
if (error_description) | ||
throw new Error(error_description); | ||
const access_token = getParameterByName('access_token'); | ||
const expires_in = getParameterByName('expires_in'); | ||
const refresh_token = getParameterByName('refresh_token'); | ||
const token_type = getParameterByName('token_type'); | ||
if (!access_token) | ||
throw new Error('No access_token detected.'); | ||
if (!expires_in) | ||
throw new Error('No expires_in detected.'); | ||
if (!refresh_token) | ||
throw new Error('No refresh_token detected.'); | ||
if (!token_type) | ||
throw new Error('No token_type detected.'); | ||
let { data: user, error } = yield this.api.getUser(access_token); | ||
if (error) | ||
throw new Error(error); | ||
const session = { | ||
access_token, | ||
expires_in: parseInt(expires_in), | ||
refresh_token, | ||
token_type, | ||
user, | ||
}; | ||
if (options === null || options === void 0 ? void 0 : options.storeSession) { | ||
this._saveSession(session); | ||
this._notifyAllSubscribers(AuthChangeEvent.SIGNED_IN); | ||
} | ||
return { data: session, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Signs out the current user, if there is a logged in user. | ||
*/ | ||
signOut() { | ||
@@ -95,12 +199,68 @@ return __awaiter(this, void 0, void 0, function* () { | ||
this._removeSession(); | ||
this._notifyAllSubscribers(AuthChangeEvent.SIGNED_OUT); | ||
return true; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
/** | ||
* Receive a notification every time an auth event happens. | ||
* @returns {Subscription} A subscription object which can be used to unsubcribe itself. | ||
*/ | ||
onAuthStateChange(callback) { | ||
try { | ||
const id = uuid(); | ||
let self = this; | ||
const subscription = { | ||
id, | ||
callback, | ||
unsubscribe: () => self.stateChangeEmmitters.delete(id), | ||
}; | ||
this.stateChangeEmmitters.set(id, subscription); | ||
return { data: this.stateChangeEmmitters.get(id), error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error }; | ||
} | ||
} | ||
_handeEmailSignIn(email, password) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
let { data, error } = yield this.api.signInWithEmail(email, password); | ||
if (!!error) | ||
return { data: null, error }; | ||
if ((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.confirmed_at) { | ||
this._saveSession(data); | ||
this._notifyAllSubscribers(AuthChangeEvent.SIGNED_IN); | ||
} | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
console.log('error_handeEmailSignIn', error); | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
_handeProviderSignIn(provider) { | ||
let url = this.api.getUrlForProvider(provider); | ||
try { | ||
// try to open on the browser | ||
if (isBrowser()) { | ||
window.location.href = url; | ||
} | ||
return { data: url, error: null }; | ||
} | ||
catch (error) { | ||
// fallback to returning the URL | ||
if (!!url) | ||
return { data: url, error: null }; | ||
else | ||
return { data: null, error }; | ||
} | ||
} | ||
_saveSession(session) { | ||
this.currentSession = session; | ||
this.currentUser = session['user']; | ||
let tokenExpirySeconds = session['expires_in']; | ||
@@ -111,9 +271,9 @@ if (this.autoRefreshToken && tokenExpirySeconds) { | ||
if (this.persistSession) { | ||
this._persistSession(this.currentSession, this.currentUser, tokenExpirySeconds); | ||
this._persistSession(this.currentSession, tokenExpirySeconds); | ||
} | ||
} | ||
_persistSession(currentSession, currentUser, secondsToExpiry) { | ||
_persistSession(currentSession, secondsToExpiry) { | ||
const timeNow = Math.round(Date.now() / 1000); | ||
const expiresAt = timeNow + secondsToExpiry; | ||
const data = { currentSession, currentUser, expiresAt }; | ||
const data = { currentSession, expiresAt }; | ||
isBrowser() && localStorage.setItem(STORAGE_KEY, JSON.stringify(data)); | ||
@@ -165,3 +325,3 @@ } | ||
if (this.persistSession && this.currentUser) { | ||
this._persistSession(this.currentSession, this.currentUser, tokenExpirySeconds); | ||
this._persistSession(this.currentSession, tokenExpirySeconds); | ||
} | ||
@@ -173,7 +333,10 @@ } | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
return { data: null, error }; | ||
} | ||
}); | ||
} | ||
_notifyAllSubscribers(event) { | ||
this.stateChangeEmmitters.forEach((x) => x.callback(event, this.currentSession)); | ||
} | ||
} | ||
//# sourceMappingURL=Client.js.map |
@@ -11,2 +11,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import fetch from 'isomorphic-unfetch'; | ||
const handleError = (error, reject) => { | ||
if (typeof error.json === 'function') { | ||
error.json().then((msg) => { | ||
return reject(new Error(msg.error_description)); | ||
}); | ||
} | ||
else { | ||
return reject(error); | ||
} | ||
}; | ||
export function get(url, options) { | ||
@@ -19,5 +29,12 @@ return __awaiter(this, void 0, void 0, function* () { | ||
}) | ||
.then((r) => r.json()) | ||
.then((result) => { | ||
if (!result.ok) | ||
throw result; | ||
else if (options === null || options === void 0 ? void 0 : options.noResolveJson) | ||
return resolve; | ||
else | ||
return result.json(); | ||
}) | ||
.then((data) => resolve(data)) | ||
.catch((error) => reject(error)); | ||
.catch((error) => handleError(error, reject)); | ||
}); | ||
@@ -34,5 +51,12 @@ }); | ||
}) | ||
.then((r) => (!(options === null || options === void 0 ? void 0 : options.noResolveJson) ? r.json() : null)) | ||
.then((result) => { | ||
if (!result.ok) | ||
throw result; | ||
else if (options === null || options === void 0 ? void 0 : options.noResolveJson) | ||
return resolve; | ||
else | ||
return result.json(); | ||
}) | ||
.then((data) => resolve(data)) | ||
.catch((error) => reject(error)); | ||
.catch((error) => handleError(error, reject)); | ||
}); | ||
@@ -49,5 +73,12 @@ }); | ||
}) | ||
.then((r) => (!(options === null || options === void 0 ? void 0 : options.noResolveJson) ? r.json() : {})) | ||
.then((result) => { | ||
if (!result.ok) | ||
throw result; | ||
else if (options === null || options === void 0 ? void 0 : options.noResolveJson) | ||
return resolve; | ||
else | ||
return result.json(); | ||
}) | ||
.then((data) => resolve(data)) | ||
.catch((error) => reject(error)); | ||
.catch((error) => handleError(error, reject)); | ||
}); | ||
@@ -54,0 +85,0 @@ }); |
export declare function uuid(): string; | ||
export declare const isBrowser: () => boolean; | ||
export declare function getParameterByName(name: string, url?: string): string | null; | ||
//# sourceMappingURL=helpers.d.ts.map |
@@ -8,2 +8,13 @@ export function uuid() { | ||
export const isBrowser = () => typeof window !== 'undefined'; | ||
export function getParameterByName(name, url) { | ||
if (!url) | ||
url = window.location.href; | ||
name = name.replace(/[\[\]]/g, '\\$&'); | ||
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), results = regex.exec(url); | ||
if (!results) | ||
return null; | ||
if (!results[2]) | ||
return ''; | ||
return decodeURIComponent(results[2].replace(/\+/g, ' ')); | ||
} | ||
//# sourceMappingURL=helpers.js.map |
@@ -0,1 +1,12 @@ | ||
export declare enum Provider { | ||
BITBUCKET = "bitbucket", | ||
GITHUB = "github", | ||
GITLAB = "gitlab", | ||
GOOGLE = "google" | ||
} | ||
export declare enum AuthChangeEvent { | ||
SIGNED_IN = "SIGNED_IN", | ||
SIGNED_OUT = "SIGNED_OUT", | ||
USER_UPDATED = "USER_UPDATED" | ||
} | ||
export interface Session { | ||
@@ -21,7 +32,33 @@ access_token: string; | ||
export interface UserAttributes { | ||
/** | ||
* The user's email. | ||
*/ | ||
email?: string; | ||
/** | ||
* The user's password. | ||
*/ | ||
password?: string; | ||
/** | ||
* An email change token. | ||
*/ | ||
email_change_token?: string; | ||
/** | ||
* A custom data object. Can be any JSON. | ||
*/ | ||
data?: object; | ||
} | ||
export interface Subscription { | ||
/** | ||
* The subscriber UUID. This will be set by the client. | ||
*/ | ||
id: string; | ||
/** | ||
* The function to call every time there is an event. eg: (eventName) => {} | ||
*/ | ||
callback: Function; | ||
/** | ||
* Call this to remove the listener. | ||
*/ | ||
unsubscribe: Function; | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
@@ -1,2 +0,14 @@ | ||
export {}; | ||
export var Provider; | ||
(function (Provider) { | ||
Provider["BITBUCKET"] = "bitbucket"; | ||
Provider["GITHUB"] = "github"; | ||
Provider["GITLAB"] = "gitlab"; | ||
Provider["GOOGLE"] = "google"; | ||
})(Provider || (Provider = {})); | ||
export var AuthChangeEvent; | ||
(function (AuthChangeEvent) { | ||
AuthChangeEvent["SIGNED_IN"] = "SIGNED_IN"; | ||
AuthChangeEvent["SIGNED_OUT"] = "SIGNED_OUT"; | ||
AuthChangeEvent["USER_UPDATED"] = "USER_UPDATED"; | ||
})(AuthChangeEvent || (AuthChangeEvent = {})); | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "@supabase/gotrue-js", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Isomorphic GoTrue client", | ||
@@ -26,4 +26,4 @@ "keywords": [ | ||
"test": "run-s test:db && jest --runInBand", | ||
"test:db": "cd test/db && docker-compose down && docker-compose up -d && sleep 5", | ||
"test:clean": "cd test/db && docker-compose down", | ||
"test:db": "cd infra/db && docker-compose down && docker-compose up -d && sleep 5", | ||
"test:clean": "cd infra/db && docker-compose down", | ||
"docs": "typedoc --mode file --target ES6 --theme minimal", | ||
@@ -30,0 +30,0 @@ "docs:json": "typedoc --json docs/spec.json --mode modules --includeDeclarations --excludeExternals" |
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
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
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
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
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
112458
1814