@supabase/gotrue-js
Advanced tools
Comparing version 1.9.3 to 1.10.0
@@ -1,2 +0,2 @@ | ||
import { Session, Provider, UserAttributes } from './lib/types'; | ||
import { Session, Provider, UserAttributes, CookieOptions, User } from './lib/types'; | ||
export default class GoTrueApi { | ||
@@ -7,3 +7,4 @@ protected url: string; | ||
}; | ||
constructor({ url, headers, }: { | ||
protected cookieOptions: CookieOptions; | ||
constructor({ url, headers, cookieOptions, }: { | ||
url: string; | ||
@@ -13,2 +14,3 @@ headers: { | ||
}; | ||
cookieOptions?: CookieOptions; | ||
}); | ||
@@ -74,7 +76,5 @@ /** | ||
getUser(jwt: string): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
user: User | null; | ||
data: User | null; | ||
error: Error | null; | ||
}>; | ||
@@ -87,7 +87,5 @@ /** | ||
updateUser(jwt: string, attributes: UserAttributes): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
user: User | null; | ||
data: User | null; | ||
error: Error | null; | ||
}>; | ||
@@ -99,9 +97,20 @@ /** | ||
refreshAccessToken(refreshToken: string): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
data: Session | null; | ||
error: Error | null; | ||
}>; | ||
/** | ||
* Set/delete the auth cookie based on the AuthChangeEvent. | ||
* Works for Next.js & Express (requires cookie-parser middleware). | ||
*/ | ||
setAuthCookie(req: any, res: any): void; | ||
/** | ||
* Get user by reading the cookie from the request. | ||
* Works for Next.js & Express (requires cookie-parser middleware). | ||
*/ | ||
getUserByCookie(req: any): Promise<{ | ||
user: User | null; | ||
data: User | null; | ||
error: Error | null; | ||
}>; | ||
} | ||
//# sourceMappingURL=GoTrueApi.d.ts.map |
@@ -13,6 +13,9 @@ "use strict"; | ||
const fetch_1 = require("./lib/fetch"); | ||
const constants_1 = require("./lib/constants"); | ||
const cookies_1 = require("./lib/cookies"); | ||
class GoTrueApi { | ||
constructor({ url = '', headers = {}, }) { | ||
constructor({ url = '', headers = {}, cookieOptions, }) { | ||
this.url = url; | ||
this.headers = headers; | ||
this.cookieOptions = Object.assign(Object.assign({}, constants_1.COOKIE_OPTIONS), cookieOptions); | ||
} | ||
@@ -130,6 +133,6 @@ /** | ||
let data = yield fetch_1.get(`${this.url}/user`, { headers }); | ||
return { data, error: null }; | ||
return { user: data, data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error }; | ||
return { user: null, data: null, error }; | ||
} | ||
@@ -149,6 +152,6 @@ }); | ||
let data = yield fetch_1.put(`${this.url}/user`, attributes, { headers }); | ||
return { data, error: null }; | ||
return { user: data, data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error }; | ||
return { user: null, data: null, error }; | ||
} | ||
@@ -172,4 +175,56 @@ }); | ||
} | ||
/** | ||
* Set/delete the auth cookie based on the AuthChangeEvent. | ||
* Works for Next.js & Express (requires cookie-parser middleware). | ||
*/ | ||
setAuthCookie(req, res) { | ||
if (req.method === 'POST') { | ||
const { event, session } = req.body; | ||
if (!event) | ||
throw new Error('Auth event missing!'); | ||
if (event === 'SIGNED_IN') { | ||
if (!session) | ||
throw new Error('Auth session missing!'); | ||
cookies_1.setCookie(req, res, { | ||
name: this.cookieOptions.name, | ||
value: session.access_token, | ||
domain: this.cookieOptions.domain, | ||
maxAge: this.cookieOptions.lifetime, | ||
path: this.cookieOptions.path, | ||
sameSite: this.cookieOptions.sameSite, | ||
}); | ||
} | ||
if (event === 'SIGNED_OUT') | ||
cookies_1.deleteCookie(req, res, this.cookieOptions.name); | ||
res.status(200).json({}); | ||
} | ||
else { | ||
res.setHeader('Allow', 'POST'); | ||
res.status(405).end('Method Not Allowed'); | ||
} | ||
} | ||
/** | ||
* Get user by reading the cookie from the request. | ||
* Works for Next.js & Express (requires cookie-parser middleware). | ||
*/ | ||
getUserByCookie(req) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (!req.cookies) | ||
throw new Error('Not able to parse cookies! When using Express make sure the cookie-parser middleware is in use!'); | ||
if (!req.cookies[this.cookieOptions.name]) | ||
throw new Error('No cookie found!'); | ||
const token = req.cookies[this.cookieOptions.name]; | ||
const { user, error } = yield this.getUser(token); | ||
if (error) | ||
throw error; | ||
return { user, data: user, error: null }; | ||
} | ||
catch (error) { | ||
return { user: null, data: null, error }; | ||
} | ||
}); | ||
} | ||
} | ||
exports.default = GoTrueApi; | ||
//# sourceMappingURL=GoTrueApi.js.map |
import GoTrueApi from './GoTrueApi'; | ||
import { Session, User, UserAttributes, Provider, Subscription, AuthChangeEvent } from './lib/types'; | ||
import { Session, User, UserAttributes, Provider, Subscription, AuthChangeEvent, CookieOptions } from './lib/types'; | ||
export default class GoTrueClient { | ||
@@ -39,2 +39,3 @@ /** | ||
localStorage?: Storage; | ||
cookieOptions?: CookieOptions; | ||
}); | ||
@@ -41,0 +42,0 @@ /** |
@@ -44,3 +44,7 @@ "use strict"; | ||
this.localStorage = new helpers_1.LocalStorage(settings.localStorage); | ||
this.api = new GoTrueApi_1.default({ url: settings.url, headers: settings.headers }); | ||
this.api = new GoTrueApi_1.default({ | ||
url: settings.url, | ||
headers: settings.headers, | ||
cookieOptions: settings.cookieOptions, | ||
}); | ||
this._recoverSession(); | ||
@@ -188,3 +192,3 @@ // Handle the OAuth redirect | ||
throw new Error('No token_type detected.'); | ||
let { data: user, error } = yield this.api.getUser(access_token); | ||
const { user, error } = yield this.api.getUser(access_token); | ||
if (error) | ||
@@ -197,3 +201,3 @@ throw error; | ||
token_type, | ||
user, | ||
user: user, | ||
}; | ||
@@ -325,5 +329,2 @@ if (options === null || options === void 0 ? void 0 : options.storeSession) { | ||
} | ||
else { | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
} | ||
} | ||
@@ -364,2 +365,3 @@ else { | ||
this.currentUser = this.currentSession.user; | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
const tokenExpirySeconds = data.expires_in; | ||
@@ -366,0 +368,0 @@ if (this.autoRefreshToken && tokenExpirySeconds) { |
@@ -6,2 +6,9 @@ export declare const GOTRUE_URL: string; | ||
export declare const STORAGE_KEY: string; | ||
export declare const COOKIE_OPTIONS: { | ||
name: string; | ||
lifetime: number; | ||
domain: string; | ||
path: string; | ||
sameSite: string; | ||
}; | ||
//# sourceMappingURL=constants.d.ts.map |
"use strict"; | ||
var _a, _b, _c, _d; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.STORAGE_KEY = exports.EXPIRY_MARGIN = exports.DEFAULT_HEADERS = exports.AUDIENCE = exports.GOTRUE_URL = void 0; | ||
exports.COOKIE_OPTIONS = exports.STORAGE_KEY = exports.EXPIRY_MARGIN = exports.DEFAULT_HEADERS = exports.AUDIENCE = exports.GOTRUE_URL = void 0; | ||
exports.GOTRUE_URL = ((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.GOTRUE_URL) || 'http://localhost:9999'; | ||
@@ -10,2 +10,9 @@ exports.AUDIENCE = ((_b = process === null || process === void 0 ? void 0 : process.env) === null || _b === void 0 ? void 0 : _b.AUDIENCE) || ''; | ||
exports.STORAGE_KEY = ((_d = process === null || process === void 0 ? void 0 : process.env) === null || _d === void 0 ? void 0 : _d.STORAGE_KEY) || 'supabase.auth.token'; | ||
exports.COOKIE_OPTIONS = { | ||
name: 'sb:token', | ||
lifetime: 60 * 60 * 8, | ||
domain: '', | ||
path: '/', | ||
sameSite: 'lax', | ||
}; | ||
//# sourceMappingURL=constants.js.map |
@@ -59,2 +59,9 @@ export declare type Provider = 'bitbucket' | 'github' | 'gitlab' | 'google'; | ||
} | ||
export interface CookieOptions { | ||
name?: string; | ||
lifetime?: number; | ||
domain?: string; | ||
path?: string; | ||
sameSite?: string; | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { Session, Provider, UserAttributes } from './lib/types'; | ||
import { Session, Provider, UserAttributes, CookieOptions, User } from './lib/types'; | ||
export default class GoTrueApi { | ||
@@ -7,3 +7,4 @@ protected url: string; | ||
}; | ||
constructor({ url, headers, }: { | ||
protected cookieOptions: CookieOptions; | ||
constructor({ url, headers, cookieOptions, }: { | ||
url: string; | ||
@@ -13,2 +14,3 @@ headers: { | ||
}; | ||
cookieOptions?: CookieOptions; | ||
}); | ||
@@ -74,7 +76,5 @@ /** | ||
getUser(jwt: string): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
user: User | null; | ||
data: User | null; | ||
error: Error | null; | ||
}>; | ||
@@ -87,7 +87,5 @@ /** | ||
updateUser(jwt: string, attributes: UserAttributes): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
user: User | null; | ||
data: User | null; | ||
error: Error | null; | ||
}>; | ||
@@ -99,9 +97,20 @@ /** | ||
refreshAccessToken(refreshToken: string): Promise<{ | ||
data: any; | ||
error: null; | ||
} | { | ||
data: null; | ||
error: any; | ||
data: Session | null; | ||
error: Error | null; | ||
}>; | ||
/** | ||
* Set/delete the auth cookie based on the AuthChangeEvent. | ||
* Works for Next.js & Express (requires cookie-parser middleware). | ||
*/ | ||
setAuthCookie(req: any, res: any): void; | ||
/** | ||
* Get user by reading the cookie from the request. | ||
* Works for Next.js & Express (requires cookie-parser middleware). | ||
*/ | ||
getUserByCookie(req: any): Promise<{ | ||
user: User | null; | ||
data: User | null; | ||
error: Error | null; | ||
}>; | ||
} | ||
//# sourceMappingURL=GoTrueApi.d.ts.map |
@@ -11,6 +11,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { get, post, put } from './lib/fetch'; | ||
import { COOKIE_OPTIONS } from './lib/constants'; | ||
import { setCookie, deleteCookie } from './lib/cookies'; | ||
export default class GoTrueApi { | ||
constructor({ url = '', headers = {}, }) { | ||
constructor({ url = '', headers = {}, cookieOptions, }) { | ||
this.url = url; | ||
this.headers = headers; | ||
this.cookieOptions = Object.assign(Object.assign({}, COOKIE_OPTIONS), cookieOptions); | ||
} | ||
@@ -128,6 +131,6 @@ /** | ||
let data = yield get(`${this.url}/user`, { headers }); | ||
return { data, error: null }; | ||
return { user: data, data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error }; | ||
return { user: null, data: null, error }; | ||
} | ||
@@ -147,6 +150,6 @@ }); | ||
let data = yield put(`${this.url}/user`, attributes, { headers }); | ||
return { data, error: null }; | ||
return { user: data, data, error: null }; | ||
} | ||
catch (error) { | ||
return { data: null, error }; | ||
return { user: null, data: null, error }; | ||
} | ||
@@ -170,3 +173,55 @@ }); | ||
} | ||
/** | ||
* Set/delete the auth cookie based on the AuthChangeEvent. | ||
* Works for Next.js & Express (requires cookie-parser middleware). | ||
*/ | ||
setAuthCookie(req, res) { | ||
if (req.method === 'POST') { | ||
const { event, session } = req.body; | ||
if (!event) | ||
throw new Error('Auth event missing!'); | ||
if (event === 'SIGNED_IN') { | ||
if (!session) | ||
throw new Error('Auth session missing!'); | ||
setCookie(req, res, { | ||
name: this.cookieOptions.name, | ||
value: session.access_token, | ||
domain: this.cookieOptions.domain, | ||
maxAge: this.cookieOptions.lifetime, | ||
path: this.cookieOptions.path, | ||
sameSite: this.cookieOptions.sameSite, | ||
}); | ||
} | ||
if (event === 'SIGNED_OUT') | ||
deleteCookie(req, res, this.cookieOptions.name); | ||
res.status(200).json({}); | ||
} | ||
else { | ||
res.setHeader('Allow', 'POST'); | ||
res.status(405).end('Method Not Allowed'); | ||
} | ||
} | ||
/** | ||
* Get user by reading the cookie from the request. | ||
* Works for Next.js & Express (requires cookie-parser middleware). | ||
*/ | ||
getUserByCookie(req) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (!req.cookies) | ||
throw new Error('Not able to parse cookies! When using Express make sure the cookie-parser middleware is in use!'); | ||
if (!req.cookies[this.cookieOptions.name]) | ||
throw new Error('No cookie found!'); | ||
const token = req.cookies[this.cookieOptions.name]; | ||
const { user, error } = yield this.getUser(token); | ||
if (error) | ||
throw error; | ||
return { user, data: user, error: null }; | ||
} | ||
catch (error) { | ||
return { user: null, data: null, error }; | ||
} | ||
}); | ||
} | ||
} | ||
//# sourceMappingURL=GoTrueApi.js.map |
import GoTrueApi from './GoTrueApi'; | ||
import { Session, User, UserAttributes, Provider, Subscription, AuthChangeEvent } from './lib/types'; | ||
import { Session, User, UserAttributes, Provider, Subscription, AuthChangeEvent, CookieOptions } from './lib/types'; | ||
export default class GoTrueClient { | ||
@@ -39,2 +39,3 @@ /** | ||
localStorage?: Storage; | ||
cookieOptions?: CookieOptions; | ||
}); | ||
@@ -41,0 +42,0 @@ /** |
@@ -39,3 +39,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
this.localStorage = new LocalStorage(settings.localStorage); | ||
this.api = new GoTrueApi({ url: settings.url, headers: settings.headers }); | ||
this.api = new GoTrueApi({ | ||
url: settings.url, | ||
headers: settings.headers, | ||
cookieOptions: settings.cookieOptions, | ||
}); | ||
this._recoverSession(); | ||
@@ -183,3 +187,3 @@ // Handle the OAuth redirect | ||
throw new Error('No token_type detected.'); | ||
let { data: user, error } = yield this.api.getUser(access_token); | ||
const { user, error } = yield this.api.getUser(access_token); | ||
if (error) | ||
@@ -192,3 +196,3 @@ throw error; | ||
token_type, | ||
user, | ||
user: user, | ||
}; | ||
@@ -320,5 +324,2 @@ if (options === null || options === void 0 ? void 0 : options.storeSession) { | ||
} | ||
else { | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
} | ||
} | ||
@@ -359,2 +360,3 @@ else { | ||
this.currentUser = this.currentSession.user; | ||
this._notifyAllSubscribers('SIGNED_IN'); | ||
const tokenExpirySeconds = data.expires_in; | ||
@@ -361,0 +363,0 @@ if (this.autoRefreshToken && tokenExpirySeconds) { |
@@ -6,2 +6,9 @@ export declare const GOTRUE_URL: string; | ||
export declare const STORAGE_KEY: string; | ||
export declare const COOKIE_OPTIONS: { | ||
name: string; | ||
lifetime: number; | ||
domain: string; | ||
path: string; | ||
sameSite: string; | ||
}; | ||
//# sourceMappingURL=constants.d.ts.map |
@@ -7,2 +7,9 @@ var _a, _b, _c, _d; | ||
export const STORAGE_KEY = ((_d = process === null || process === void 0 ? void 0 : process.env) === null || _d === void 0 ? void 0 : _d.STORAGE_KEY) || 'supabase.auth.token'; | ||
export const COOKIE_OPTIONS = { | ||
name: 'sb:token', | ||
lifetime: 60 * 60 * 8, | ||
domain: '', | ||
path: '/', | ||
sameSite: 'lax', | ||
}; | ||
//# sourceMappingURL=constants.js.map |
@@ -59,2 +59,9 @@ export declare type Provider = 'bitbucket' | 'github' | 'gitlab' | 'google'; | ||
} | ||
export interface CookieOptions { | ||
name?: string; | ||
lifetime?: number; | ||
domain?: string; | ||
path?: string; | ||
sameSite?: string; | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "@supabase/gotrue-js", | ||
"version": "1.9.3", | ||
"version": "1.10.0", | ||
"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
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
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
163665
67
2507