@supabase/auth-helpers-sveltekit
Advanced tools
Comparing version 0.2.0 to 0.3.0
# @supabase/auth-helpers-sveltekit | ||
## 0.3.0 | ||
### Minor Changes | ||
- f399820: Using shared package as a dependency | ||
Update sveltekit package with latest code to update tokens | ||
## 0.2.0 | ||
@@ -4,0 +11,0 @@ |
import type { Handle } from '@sveltejs/kit'; | ||
import { type CookieOptions } from '../types'; | ||
export declare const handleCallback: (cookieOptions?: CookieOptions) => Handle; | ||
import { type CookieOptions } from '@supabase/auth-helpers-shared'; | ||
export interface HandleCallbackOptions { | ||
cookieOptions?: CookieOptions; | ||
} | ||
export declare const handleCallback: (options?: HandleCallbackOptions) => Handle; |
@@ -1,3 +0,4 @@ | ||
import { setCookies, COOKIE_OPTIONS, SvelteKitRequestAdapter, SvelteKitResponseAdapter } from '../types'; | ||
export const handleCallback = (cookieOptions = COOKIE_OPTIONS) => { | ||
import { setCookies, COOKIE_OPTIONS, SvelteKitRequestAdapter, SvelteKitResponseAdapter } from '@supabase/auth-helpers-shared'; | ||
import getUser from '../utils/getUser'; | ||
export const handleCallback = (options = {}) => { | ||
const handle = async ({ event, resolve }) => { | ||
@@ -16,5 +17,9 @@ const req = event.request; | ||
} | ||
const cookieOptions = { ...COOKIE_OPTIONS, ...options.cookieOptions }; | ||
const { event: bodyEvent, session } = await req.json(); | ||
if (!bodyEvent) | ||
throw new Error('Auth event missing!'); | ||
if (bodyEvent === 'USER_UPDATED') { | ||
await getUser({ req, res }, { forceRefresh: true }); | ||
} | ||
if (bodyEvent === 'SIGNED_IN') { | ||
@@ -24,15 +29,27 @@ if (!session) | ||
setCookies(new SvelteKitRequestAdapter(req), new SvelteKitResponseAdapter(res), [ | ||
{ key: 'access-token', value: session.access_token }, | ||
{ key: 'refresh-token', value: session.refresh_token } | ||
].map((token) => ({ | ||
name: `${cookieOptions.name}-${token.key}`, | ||
value: token.value, | ||
domain: cookieOptions.domain, | ||
maxAge: cookieOptions.lifetime ?? 0, | ||
path: cookieOptions.path, | ||
sameSite: cookieOptions.sameSite | ||
}))); | ||
session.access_token | ||
? { key: 'access-token', value: session.access_token } | ||
: null, | ||
session.refresh_token | ||
? { key: 'refresh-token', value: session.refresh_token } | ||
: null, | ||
session.provider_token | ||
? { key: 'provider-token', value: session.provider_token } | ||
: null | ||
].reduce((acc, token) => { | ||
if (token) { | ||
acc.push({ | ||
name: `${cookieOptions.name}-${token.key}`, | ||
value: token.value, | ||
domain: cookieOptions.domain, | ||
maxAge: cookieOptions.lifetime ?? 0, | ||
path: cookieOptions.path, | ||
sameSite: cookieOptions.sameSite | ||
}); | ||
} | ||
return acc; | ||
}, [])); | ||
} | ||
if (bodyEvent === 'SIGNED_OUT') { | ||
setCookies(new SvelteKitRequestAdapter(req), new SvelteKitResponseAdapter(res), ['access-token', 'refresh-token'].map((key) => ({ | ||
if (bodyEvent === 'SIGNED_OUT' || bodyEvent === 'USER_DELETED') { | ||
setCookies(new SvelteKitRequestAdapter(req), new SvelteKitResponseAdapter(res), ['access-token', 'refresh-token', 'provider-token'].map((key) => ({ | ||
name: `${cookieOptions.name}-${key}`, | ||
@@ -39,0 +56,0 @@ value: '', |
import type { Handle } from '@sveltejs/kit'; | ||
import { type CookieOptions } from '../types'; | ||
export declare const handleUser: (cookieOptions?: CookieOptions) => Handle; | ||
import { type CookieOptions } from '@supabase/auth-helpers-shared'; | ||
export interface HandleUserOptions { | ||
cookieOptions?: CookieOptions; | ||
tokenRefreshMargin?: number; | ||
} | ||
export declare const handleUser: (options?: HandleUserOptions) => Handle; |
@@ -1,6 +0,5 @@ | ||
import { COOKIE_OPTIONS, parseCookie } from '../types'; | ||
import { COOKIE_OPTIONS, parseCookie, jwtDecoder, TOKEN_REFRESH_MARGIN } from '@supabase/auth-helpers-shared'; | ||
import { skHelper } from '../instance'; | ||
import getUser from '../utils/getUser'; | ||
import { jwtDecoder } from '../utils/jwt'; | ||
export const handleUser = (cookieOptions = COOKIE_OPTIONS) => { | ||
export const handleUser = (options = {}) => { | ||
const handle = async ({ event, resolve }) => { | ||
@@ -13,2 +12,4 @@ const req = event.request; | ||
} | ||
const cookieOptions = { ...COOKIE_OPTIONS, ...options.cookieOptions }; | ||
const tokenRefreshMargin = options.tokenRefreshMargin ?? TOKEN_REFRESH_MARGIN; | ||
const cookies = parseCookie(req.headers.get('cookie')); | ||
@@ -25,6 +26,6 @@ const access_token = cookies[`${cookieOptions.name}-access-token`]; | ||
const timeNow = Math.round(Date.now() / 1000); | ||
if (jwtUser.exp < timeNow) { | ||
if (jwtUser.exp < timeNow + tokenRefreshMargin) { | ||
const res = await resolve(event); | ||
// JWT is expired, let's refresh from Gotrue | ||
const response = await getUser({ req, res }, cookieOptions); | ||
const response = await getUser({ req, res }, { cookieOptions, tokenRefreshMargin }); | ||
event.locals.user = response.user; | ||
@@ -31,0 +32,0 @@ event.locals.accessToken = response.accessToken; |
@@ -102,3 +102,3 @@ # @supabase/auth-helpers-sveltekit | ||
Wrap your `src/routes/__layout.svelte` component with the `UserHelper` component: | ||
Wrap your `src/routes/__layout.svelte` component with the `SuperUserHelper` component: | ||
@@ -111,3 +111,3 @@ ```html | ||
import { supabaseClient } from '$lib/db'; | ||
import { UserHelper } from '@supabase/auth-helpers-svelte'; | ||
import { SuperUserHelper } from '@supabase/auth-helpers-svelte'; | ||
@@ -119,5 +119,5 @@ const onUserUpdate = async (user) => { | ||
<UserHelper {supabaseClient} {session} {onUserUpdate}> | ||
<SuperUserHelper {supabaseClient} {session} {onUserUpdate}> | ||
<slot /> | ||
</UserHelper> | ||
</SuperUserHelper> | ||
``` | ||
@@ -124,0 +124,0 @@ |
import type { ApiError, User } from '@supabase/supabase-js'; | ||
export * from './shared'; | ||
export interface Locals { | ||
@@ -4,0 +3,0 @@ user: User; |
@@ -1,1 +0,1 @@ | ||
export * from './shared'; | ||
export {}; |
import type { User } from '@supabase/supabase-js'; | ||
import { type CookieOptions } from '../types'; | ||
import { type CookieOptions } from '@supabase/auth-helpers-shared'; | ||
interface RequestResponse { | ||
@@ -7,6 +7,12 @@ req: Request; | ||
} | ||
export default function getUser({ req, res }: RequestResponse, cookieOptions?: CookieOptions): Promise<{ | ||
export interface GetUserOptions { | ||
cookieOptions?: CookieOptions; | ||
forceRefresh?: boolean; | ||
tokenRefreshMargin?: number; | ||
} | ||
export default function getUser({ req, res }: RequestResponse, options?: GetUserOptions): Promise<{ | ||
user: User | null; | ||
accessToken: string | null; | ||
error?: string; | ||
}>; | ||
export {}; |
import { createClient } from '@supabase/supabase-js'; | ||
import { skHelper } from '../instance'; | ||
import { setCookies, parseCookie, COOKIE_OPTIONS, SvelteKitRequestAdapter, SvelteKitResponseAdapter } from '../types'; | ||
import { jwtDecoder } from '../utils/jwt'; | ||
export default async function getUser({ req, res }, cookieOptions = COOKIE_OPTIONS) { | ||
import { setCookies, parseCookie, COOKIE_OPTIONS, SvelteKitRequestAdapter, SvelteKitResponseAdapter, jwtDecoder, TOKEN_REFRESH_MARGIN } from '@supabase/auth-helpers-shared'; | ||
export default async function getUser({ req, res }, options = { forceRefresh: false }) { | ||
try { | ||
@@ -14,2 +13,4 @@ const { apiInfo: { supabaseUrl, supabaseAnonKey } } = skHelper(); | ||
} | ||
const cookieOptions = { ...COOKIE_OPTIONS, ...options.cookieOptions }; | ||
const tokenRefreshMargin = options.tokenRefreshMargin ?? TOKEN_REFRESH_MARGIN; | ||
const cookies = parseCookie(req.headers.get('cookie')); | ||
@@ -30,3 +31,3 @@ const supabase = createClient(supabaseUrl, supabaseAnonKey); | ||
const timeNow = Math.round(Date.now() / 1000); | ||
if (jwtUser.exp < timeNow) { | ||
if (options.forceRefresh || jwtUser.exp < timeNow + tokenRefreshMargin) { | ||
// JWT is expired, let's refresh from Gotrue | ||
@@ -62,4 +63,5 @@ if (!refresh_token) { | ||
catch (e) { | ||
return { user: null, accessToken: null }; | ||
const error = e; | ||
return { user: null, accessToken: null, error: error.message }; | ||
} | ||
} |
import type { SupabaseClient } from '@supabase/supabase-js'; | ||
import type { CookieOptions } from '../types'; | ||
import { type CookieOptions } from '@supabase/auth-helpers-shared'; | ||
/** | ||
@@ -4,0 +4,0 @@ * This is a helper method to wrap your SupabaseClient to inject a user's access_token to make use of RLS on the server side. |
@@ -1,3 +0,3 @@ | ||
import { COOKIE_OPTIONS } from '../shared'; | ||
import { skHelper } from '../instance'; | ||
import { COOKIE_OPTIONS } from '@supabase/auth-helpers-shared'; | ||
/** | ||
@@ -4,0 +4,0 @@ * This is a helper method to wrap your SupabaseClient to inject a user's access_token to make use of RLS on the server side. |
{ | ||
"name": "@supabase/auth-helpers-sveltekit", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A collection of framework specific Auth utilities for working with Supabase.", | ||
@@ -34,3 +34,3 @@ "types": "./dist/index.d.ts", | ||
"@sveltejs/kit": "next", | ||
"copyfiles": "^2.4.1", | ||
"del-cli": "^4.0.1", | ||
"svelte": "^3.48.0", | ||
@@ -42,12 +42,9 @@ "svelte2tsx": "^0.5.10", | ||
"dependencies": { | ||
"@supabase/supabase-js": "^1.35.3", | ||
"del-cli": "^4.0.1", | ||
"jose": "^4.8.1" | ||
"@supabase/auth-helpers-shared": "0.1.0", | ||
"@supabase/supabase-js": "^1.35.3" | ||
}, | ||
"scripts": { | ||
"build": "pnpm copy:shared && pnpm package && pnpm rm:shared", | ||
"package": "svelte-kit package && del-cli dist/package.json", | ||
"copy:shared": "copyfiles ../shared/** src/shared -e ../shared/*.md -e ../shared/*.json", | ||
"rm:shared": "del-cli src/shared" | ||
"build": "pnpm package", | ||
"package": "svelte-kit package && del-cli dist/package.json" | ||
} | ||
} |
@@ -102,3 +102,3 @@ # @supabase/auth-helpers-sveltekit | ||
Wrap your `src/routes/__layout.svelte` component with the `UserHelper` component: | ||
Wrap your `src/routes/__layout.svelte` component with the `SuperUserHelper` component: | ||
@@ -111,3 +111,3 @@ ```html | ||
import { supabaseClient } from '$lib/db'; | ||
import { UserHelper } from '@supabase/auth-helpers-svelte'; | ||
import { SuperUserHelper } from '@supabase/auth-helpers-svelte'; | ||
@@ -119,5 +119,5 @@ const onUserUpdate = async (user) => { | ||
<UserHelper {supabaseClient} {session} {onUserUpdate}> | ||
<SuperUserHelper {supabaseClient} {session} {onUserUpdate}> | ||
<slot /> | ||
</UserHelper> | ||
</SuperUserHelper> | ||
``` | ||
@@ -124,0 +124,0 @@ |
32444
2
424
34
+ Added@supabase/auth-helpers-shared@0.1.0(transitive)
- Removeddel-cli@^4.0.1
- Removedjose@^4.8.1
- Removed@babel/code-frame@7.26.2(transitive)
- Removed@babel/helper-validator-identifier@7.25.9(transitive)
- Removed@nodelib/fs.scandir@2.1.5(transitive)
- Removed@nodelib/fs.stat@2.0.5(transitive)
- Removed@nodelib/fs.walk@1.2.8(transitive)
- Removed@types/minimist@1.2.5(transitive)
- Removed@types/normalize-package-data@2.4.4(transitive)
- Removedaggregate-error@3.1.0(transitive)
- Removedarray-union@2.1.0(transitive)
- Removedarrify@1.0.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedbraces@3.0.3(transitive)
- Removedcamelcase@6.3.0(transitive)
- Removedcamelcase-keys@7.0.2(transitive)
- Removedclean-stack@2.2.0(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removeddecamelize@1.2.05.0.1(transitive)
- Removeddecamelize-keys@1.1.1(transitive)
- Removeddel@6.1.1(transitive)
- Removeddel-cli@4.0.1(transitive)
- Removeddir-glob@3.0.1(transitive)
- Removederror-ex@1.3.2(transitive)
- Removedfast-glob@3.3.3(transitive)
- Removedfastq@1.19.0(transitive)
- Removedfill-range@7.1.1(transitive)
- Removedfind-up@5.0.0(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedglob@7.2.3(transitive)
- Removedglob-parent@5.1.2(transitive)
- Removedglobby@11.1.0(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhard-rejection@2.1.0(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhosted-git-info@4.1.0(transitive)
- Removedignore@5.3.2(transitive)
- Removedindent-string@4.0.05.0.0(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-arrayish@0.2.1(transitive)
- Removedis-core-module@2.16.1(transitive)
- Removedis-extglob@2.1.1(transitive)
- Removedis-glob@4.0.3(transitive)
- Removedis-number@7.0.0(transitive)
- Removedis-path-cwd@2.2.0(transitive)
- Removedis-path-inside@3.0.3(transitive)
- Removedis-plain-obj@1.1.0(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedjson-parse-even-better-errors@2.3.1(transitive)
- Removedkind-of@6.0.3(transitive)
- Removedlines-and-columns@1.2.4(transitive)
- Removedlocate-path@6.0.0(transitive)
- Removedlru-cache@6.0.0(transitive)
- Removedmap-obj@1.0.14.3.0(transitive)
- Removedmeow@10.1.5(transitive)
- Removedmerge2@1.4.1(transitive)
- Removedmicromatch@4.0.8(transitive)
- Removedmin-indent@1.0.1(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedminimist-options@4.1.0(transitive)
- Removednormalize-package-data@3.0.3(transitive)
- Removedonce@1.4.0(transitive)
- Removedp-limit@3.1.0(transitive)
- Removedp-locate@5.0.0(transitive)
- Removedp-map@4.0.0(transitive)
- Removedparse-json@5.2.0(transitive)
- Removedpath-exists@4.0.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpath-type@4.0.0(transitive)
- Removedpicocolors@1.1.1(transitive)
- Removedpicomatch@2.3.1(transitive)
- Removedqueue-microtask@1.2.3(transitive)
- Removedquick-lru@5.1.1(transitive)
- Removedread-pkg@6.0.0(transitive)
- Removedread-pkg-up@8.0.0(transitive)
- Removedredent@4.0.0(transitive)
- Removedreusify@1.0.4(transitive)
- Removedrimraf@3.0.2(transitive)
- Removedrun-parallel@1.2.0(transitive)
- Removedsemver@7.7.1(transitive)
- Removedslash@3.0.0(transitive)
- Removedspdx-correct@3.2.0(transitive)
- Removedspdx-exceptions@2.5.0(transitive)
- Removedspdx-expression-parse@3.0.1(transitive)
- Removedspdx-license-ids@3.0.21(transitive)
- Removedstrip-indent@4.0.0(transitive)
- Removedto-regex-range@5.0.1(transitive)
- Removedtrim-newlines@4.1.1(transitive)
- Removedtype-fest@1.4.0(transitive)
- Removedvalidate-npm-package-license@3.0.4(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedyallist@4.0.0(transitive)
- Removedyargs-parser@20.2.9(transitive)
- Removedyocto-queue@0.1.0(transitive)