@supabase/gotrue-js
Advanced tools
Comparing version 0.0.0 to 1.1.0
@@ -0,20 +1,60 @@ | ||
import Api from './Api'; | ||
import { Session, User, UserAttributes } from './lib/types'; | ||
export default class Client { | ||
url: string; | ||
headers: { | ||
[key: string]: string; | ||
}; | ||
schema?: string; | ||
/** | ||
* Creates a GoTrue instance for user interactions. | ||
* | ||
* @param url URL of the GoTrue instance. | ||
* @param headers Custom headers. | ||
*/ | ||
constructor(url: string, { headers }?: { | ||
headers?: { | ||
[key: string]: string; | ||
}; | ||
schema?: string; | ||
}); | ||
api: Api; | ||
currentUser: User | null; | ||
currentSession?: Session | null; | ||
autoRefreshToken: boolean; | ||
persistSession: boolean; | ||
constructor({ url, autoRefreshToken, persistSession, headers, }: any); | ||
signUp({ email, password }: { | ||
email: string; | ||
password: string; | ||
}): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
signIn({ email, password }: { | ||
email: string; | ||
password: string; | ||
}): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
user(): Promise<{ | ||
data: User | null; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
update(attributes: UserAttributes): Promise<{ | ||
data: User | null; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
signOut(): Promise<true | { | ||
data: null; | ||
error: any; | ||
}>; | ||
_saveSession(session: Session): void; | ||
_persistSession(currentSession: Session, currentUser: User, secondsToExpiry: number): void; | ||
_removeSession(): void; | ||
_recoverSession(): null; | ||
_callRefreshToken(): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
} | undefined>; | ||
} | ||
//# sourceMappingURL=Client.d.ts.map |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Api_1 = __importDefault(require("./Api")); | ||
const helpers_1 = require("./lib/helpers"); | ||
const constants_1 = require("./lib/constants"); | ||
const constants_2 = require("./lib/constants"); | ||
class Client { | ||
/** | ||
* Creates a GoTrue instance for user interactions. | ||
* | ||
* @param url URL of the GoTrue instance. | ||
* @param headers Custom headers. | ||
*/ | ||
constructor(url, { headers = {} } = {}) { | ||
this.url = url; | ||
this.headers = headers; | ||
constructor({ url = constants_2.GOTRUE_URL, autoRefreshToken = true, persistSession = true, headers = constants_2.DEFAULT_HEADERS, }) { | ||
this.currentUser = null; | ||
this.currentSession = null; | ||
this.autoRefreshToken = autoRefreshToken; | ||
this.persistSession = persistSession; | ||
this.api = new Api_1.default({ url, headers }); | ||
this._recoverSession(); | ||
} | ||
signUp({ email, password }) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
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) | ||
this._saveSession(data); | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
signIn({ email, password }) { | ||
var _a; | ||
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 }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
user() { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (!((_a = this.currentSession) === null || _a === void 0 ? void 0 : _a.access_token)) | ||
throw new Error('Not logged in.'); | ||
let data = yield this.api.getUser(this.currentSession.access_token); | ||
this.currentUser = data; | ||
return { data: this.currentUser, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
update(attributes) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (!((_a = this.currentSession) === null || _a === void 0 ? void 0 : _a.access_token)) | ||
throw new Error('Not logged in.'); | ||
let data = yield this.api.updateUser(this.currentSession.access_token, attributes); | ||
this.currentUser = data; | ||
return { data: this.currentUser, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
signOut() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (this.currentSession) { | ||
yield this.api.signOut(this.currentSession.access_token); | ||
} | ||
this._removeSession(); | ||
return true; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
_saveSession(session) { | ||
this.currentSession = session; | ||
this.currentUser = session['user']; | ||
let tokenExpirySeconds = session['expires_in']; | ||
if (this.autoRefreshToken && tokenExpirySeconds) { | ||
setTimeout(this._callRefreshToken, (tokenExpirySeconds - 60) * 1000); | ||
} | ||
if (this.persistSession) { | ||
this._persistSession(this.currentSession, this.currentUser, tokenExpirySeconds); | ||
} | ||
} | ||
_persistSession(currentSession, currentUser, secondsToExpiry) { | ||
const timeNow = Math.round(Date.now() / 1000); | ||
const expiresAt = timeNow + secondsToExpiry; | ||
const data = { currentSession, currentUser, expiresAt }; | ||
helpers_1.isBrowser() && localStorage.setItem(constants_1.STORAGE_KEY, JSON.stringify(data)); | ||
} | ||
_removeSession() { | ||
this.currentSession = null; | ||
this.currentUser = null; | ||
helpers_1.isBrowser() && localStorage.removeItem(constants_1.STORAGE_KEY); | ||
} | ||
_recoverSession() { | ||
const json = helpers_1.isBrowser() && localStorage.getItem(constants_1.STORAGE_KEY); | ||
if (json) { | ||
try { | ||
const data = JSON.parse(json); | ||
const { currentSession, currentUser, expiresAt } = data; | ||
const timeNow = Math.round(Date.now() / 1000); | ||
if (expiresAt < timeNow) { | ||
console.log('Saved session has expired.'); | ||
this._removeSession(); | ||
} | ||
else { | ||
this.currentSession = currentSession; | ||
this.currentUser = currentUser; | ||
// schedule a refresh 60 seconds before token due to expire | ||
setTimeout(this._callRefreshToken, (expiresAt - timeNow - 60) * 1000); | ||
} | ||
} | ||
catch (err) { | ||
console.error(err); | ||
return null; | ||
} | ||
} | ||
return null; | ||
} | ||
_callRefreshToken() { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if ((_a = this.currentSession) === null || _a === void 0 ? void 0 : _a.refresh_token) { | ||
let data = yield this.api.refreshAccessToken((_b = this.currentSession) === null || _b === void 0 ? void 0 : _b.refresh_token); | ||
if (data === null || data === void 0 ? void 0 : data.access_token) { | ||
this.currentSession.access_token = data.body['access_token']; | ||
this.currentSession.refresh_token = data.body['refresh_token']; | ||
let tokenExpirySeconds = data.body['expires_in']; | ||
if (this.autoRefreshToken && tokenExpirySeconds) { | ||
setTimeout(this._callRefreshToken, (tokenExpirySeconds - 60) * 1000); | ||
} | ||
if (this.persistSession && this.currentUser) { | ||
this._persistSession(this.currentSession, this.currentUser, tokenExpirySeconds); | ||
} | ||
} | ||
return { data, error: null }; | ||
} | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
} | ||
exports.default = Client; | ||
//# sourceMappingURL=Client.js.map |
@@ -1,4 +0,4 @@ | ||
import Admin from './Admin'; | ||
import Api from './Api'; | ||
import Client from './Client'; | ||
export { Admin, Client }; | ||
export { Api, Client }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,7 +6,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Client = exports.Admin = void 0; | ||
const Admin_1 = __importDefault(require("./Admin")); | ||
exports.Admin = Admin_1.default; | ||
exports.Client = exports.Api = void 0; | ||
require('dotenv').config(); | ||
const Api_1 = __importDefault(require("./Api")); | ||
exports.Api = Api_1.default; | ||
const Client_1 = __importDefault(require("./Client")); | ||
exports.Client = Client_1.default; | ||
//# sourceMappingURL=index.js.map |
@@ -0,20 +1,60 @@ | ||
import Api from './Api'; | ||
import { Session, User, UserAttributes } from './lib/types'; | ||
export default class Client { | ||
url: string; | ||
headers: { | ||
[key: string]: string; | ||
}; | ||
schema?: string; | ||
/** | ||
* Creates a GoTrue instance for user interactions. | ||
* | ||
* @param url URL of the GoTrue instance. | ||
* @param headers Custom headers. | ||
*/ | ||
constructor(url: string, { headers }?: { | ||
headers?: { | ||
[key: string]: string; | ||
}; | ||
schema?: string; | ||
}); | ||
api: Api; | ||
currentUser: User | null; | ||
currentSession?: Session | null; | ||
autoRefreshToken: boolean; | ||
persistSession: boolean; | ||
constructor({ url, autoRefreshToken, persistSession, headers, }: any); | ||
signUp({ email, password }: { | ||
email: string; | ||
password: string; | ||
}): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
signIn({ email, password }: { | ||
email: string; | ||
password: string; | ||
}): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
user(): Promise<{ | ||
data: User | null; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
update(attributes: UserAttributes): Promise<{ | ||
data: User | null; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
}>; | ||
signOut(): Promise<true | { | ||
data: null; | ||
error: any; | ||
}>; | ||
_saveSession(session: Session): void; | ||
_persistSession(currentSession: Session, currentUser: User, secondsToExpiry: number): void; | ||
_removeSession(): void; | ||
_recoverSession(): null; | ||
_callRefreshToken(): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
} | undefined>; | ||
} | ||
//# sourceMappingURL=Client.d.ts.map |
@@ -0,13 +1,170 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import Api from './Api'; | ||
import { isBrowser } from './lib/helpers'; | ||
import { STORAGE_KEY } from './lib/constants'; | ||
import { GOTRUE_URL, DEFAULT_HEADERS } from './lib/constants'; | ||
export default class Client { | ||
/** | ||
* Creates a GoTrue instance for user interactions. | ||
* | ||
* @param url URL of the GoTrue instance. | ||
* @param headers Custom headers. | ||
*/ | ||
constructor(url, { headers = {} } = {}) { | ||
this.url = url; | ||
this.headers = headers; | ||
constructor({ url = GOTRUE_URL, autoRefreshToken = true, persistSession = true, headers = DEFAULT_HEADERS, }) { | ||
this.currentUser = null; | ||
this.currentSession = null; | ||
this.autoRefreshToken = autoRefreshToken; | ||
this.persistSession = persistSession; | ||
this.api = new Api({ url, headers }); | ||
this._recoverSession(); | ||
} | ||
signUp({ email, password }) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
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) | ||
this._saveSession(data); | ||
return { data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
signIn({ email, password }) { | ||
var _a; | ||
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 }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
user() { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (!((_a = this.currentSession) === null || _a === void 0 ? void 0 : _a.access_token)) | ||
throw new Error('Not logged in.'); | ||
let data = yield this.api.getUser(this.currentSession.access_token); | ||
this.currentUser = data; | ||
return { data: this.currentUser, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
update(attributes) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (!((_a = this.currentSession) === null || _a === void 0 ? void 0 : _a.access_token)) | ||
throw new Error('Not logged in.'); | ||
let data = yield this.api.updateUser(this.currentSession.access_token, attributes); | ||
this.currentUser = data; | ||
return { data: this.currentUser, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
signOut() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (this.currentSession) { | ||
yield this.api.signOut(this.currentSession.access_token); | ||
} | ||
this._removeSession(); | ||
return true; | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
_saveSession(session) { | ||
this.currentSession = session; | ||
this.currentUser = session['user']; | ||
let tokenExpirySeconds = session['expires_in']; | ||
if (this.autoRefreshToken && tokenExpirySeconds) { | ||
setTimeout(this._callRefreshToken, (tokenExpirySeconds - 60) * 1000); | ||
} | ||
if (this.persistSession) { | ||
this._persistSession(this.currentSession, this.currentUser, tokenExpirySeconds); | ||
} | ||
} | ||
_persistSession(currentSession, currentUser, secondsToExpiry) { | ||
const timeNow = Math.round(Date.now() / 1000); | ||
const expiresAt = timeNow + secondsToExpiry; | ||
const data = { currentSession, currentUser, expiresAt }; | ||
isBrowser() && localStorage.setItem(STORAGE_KEY, JSON.stringify(data)); | ||
} | ||
_removeSession() { | ||
this.currentSession = null; | ||
this.currentUser = null; | ||
isBrowser() && localStorage.removeItem(STORAGE_KEY); | ||
} | ||
_recoverSession() { | ||
const json = isBrowser() && localStorage.getItem(STORAGE_KEY); | ||
if (json) { | ||
try { | ||
const data = JSON.parse(json); | ||
const { currentSession, currentUser, expiresAt } = data; | ||
const timeNow = Math.round(Date.now() / 1000); | ||
if (expiresAt < timeNow) { | ||
console.log('Saved session has expired.'); | ||
this._removeSession(); | ||
} | ||
else { | ||
this.currentSession = currentSession; | ||
this.currentUser = currentUser; | ||
// schedule a refresh 60 seconds before token due to expire | ||
setTimeout(this._callRefreshToken, (expiresAt - timeNow - 60) * 1000); | ||
} | ||
} | ||
catch (err) { | ||
console.error(err); | ||
return null; | ||
} | ||
} | ||
return null; | ||
} | ||
_callRefreshToken() { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if ((_a = this.currentSession) === null || _a === void 0 ? void 0 : _a.refresh_token) { | ||
let data = yield this.api.refreshAccessToken((_b = this.currentSession) === null || _b === void 0 ? void 0 : _b.refresh_token); | ||
if (data === null || data === void 0 ? void 0 : data.access_token) { | ||
this.currentSession.access_token = data.body['access_token']; | ||
this.currentSession.refresh_token = data.body['refresh_token']; | ||
let tokenExpirySeconds = data.body['expires_in']; | ||
if (this.autoRefreshToken && tokenExpirySeconds) { | ||
setTimeout(this._callRefreshToken, (tokenExpirySeconds - 60) * 1000); | ||
} | ||
if (this.persistSession && this.currentUser) { | ||
this._persistSession(this.currentSession, this.currentUser, tokenExpirySeconds); | ||
} | ||
} | ||
return { data, error: null }; | ||
} | ||
} | ||
catch (error) { | ||
return { data: null, error: error.toString() }; | ||
} | ||
}); | ||
} | ||
} | ||
//# sourceMappingURL=Client.js.map |
@@ -1,4 +0,4 @@ | ||
import Admin from './Admin'; | ||
import Api from './Api'; | ||
import Client from './Client'; | ||
export { Admin, Client }; | ||
export { Api, Client }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,4 +0,5 @@ | ||
import Admin from './Admin'; | ||
require('dotenv').config(); | ||
import Api from './Api'; | ||
import Client from './Client'; | ||
export { Admin, Client }; | ||
export { Api, Client }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@supabase/gotrue-js", | ||
"version": "0.0.0", | ||
"version": "1.1.0", | ||
"description": "Isomorphic GoTrue client", | ||
@@ -38,3 +38,2 @@ "keywords": [ | ||
"dotenv": "^8.2.0", | ||
"faker": "^5.1.0", | ||
"jest": "^26.4.1", | ||
@@ -41,0 +40,0 @@ "npm-run-all": "^4.1.5", |
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
65034
10
59
837
0
4