diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index bb94e1fe..55deb678 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,6 +4,7 @@ on: push: branches: - main + - next workflow_dispatch: jobs: diff --git a/examples/nextjs/db_types.ts b/examples/nextjs/db_types.ts new file mode 100644 index 00000000..f196c90d --- /dev/null +++ b/examples/nextjs/db_types.ts @@ -0,0 +1,118 @@ +export type Json = + | string + | number + | boolean + | null + | { [key: string]: Json } + | Json[]; + +export interface Database { + public: { + Tables: { + partners: { + Row: { + id: number; + slug: string; + type: Database['public']['Enums']['partner_type']; + category: string; + developer: string; + title: string; + description: string; + logo: string; + images: string[]; + video: string; + overview: string; + website: string; + docs: string; + approved: boolean; + }; + Insert: { + id?: number; + slug?: string; + type?: Database['public']['Enums']['partner_type']; + category?: string; + developer?: string; + title?: string; + description?: string; + logo?: string; + images?: string[]; + video?: string; + overview?: string; + website?: string; + docs?: string; + approved?: boolean; + }; + Update: { + id?: number; + slug?: string; + type?: Database['public']['Enums']['partner_type']; + category?: string; + developer?: string; + title?: string; + description?: string; + logo?: string; + images?: string[]; + video?: string; + overview?: string; + website?: string; + docs?: string; + approved?: boolean; + }; + }; + partner_contacts: { + Row: { + id: number; + type: Database['public']['Enums']['partner_type']; + company: string; + country: string; + details?: string; + email: string; + first: string; + last: string; + phone?: string; + size?: number; + title?: string; + website: string; + }; + Insert: { + id?: number; + type?: Database['public']['Enums']['partner_type']; + company?: string; + country?: string; + details?: string; + email?: string; + first?: string; + last?: string; + phone?: string; + size?: number; + title?: string; + website?: string; + }; + Update: { + id?: number; + type?: Database['public']['Enums']['partner_type']; + company?: string; + country?: string; + details?: string; + email?: string; + first?: string; + last?: string; + phone?: string; + size?: number; + title?: string; + website?: string; + }; + }; + }; + Views: {}; + Functions: { + derive_label_sort_from_label: { + Args: { label: string }; + Returns: string; + }; + }; + Enums: { + partner_type: 'technology' | 'expert'; + }; + }; +} diff --git a/examples/nextjs/middleware.ts b/examples/nextjs/middleware.ts index 56bb9c23..dd0bd52f 100644 --- a/examples/nextjs/middleware.ts +++ b/examples/nextjs/middleware.ts @@ -1,13 +1,15 @@ import { withMiddlewareAuth } from '@supabase/auth-helpers-nextjs'; export const middleware = withMiddlewareAuth({ - redirectTo: '/login', + redirectTo: '/', authGuard: { - isPermitted: async (user) => user.email?.endsWith('@example.com') ?? false, + isPermitted: async (user) => { + return user.email?.endsWith('@gmail.com') ?? false; + }, redirectTo: '/insufficient-permissions' } }); export const config = { - matcher: ['/middleware-protected/:path*'] + matcher: '/middleware-protected' }; diff --git a/examples/nextjs/package.json b/examples/nextjs/package.json index df8e6dc3..fdc6bee4 100644 --- a/examples/nextjs/package.json +++ b/examples/nextjs/package.json @@ -12,6 +12,7 @@ "dependencies": { "@supabase/auth-helpers-nextjs": "workspace:*", "@supabase/auth-helpers-react": "workspace:*", + "@supabase/auth-ui-react": "^0.1.8", "next": "^12.2.5", "react": "17.0.2", "react-dom": "17.0.2" diff --git a/examples/nextjs/pages/_app.tsx b/examples/nextjs/pages/_app.tsx index 307b37f9..3dab1779 100644 --- a/examples/nextjs/pages/_app.tsx +++ b/examples/nextjs/pages/_app.tsx @@ -1,17 +1,33 @@ -import '../styles/globals.css'; +import { useRouter } from 'next/router'; +import { createBrowserSupabaseClient } from '@supabase/auth-helpers-nextjs'; +import { SessionContextProvider } from '@supabase/auth-helpers-react'; import type { AppProps } from 'next/app'; -import { UserProvider } from '@supabase/auth-helpers-react'; -import { supabaseClient } from '@supabase/auth-helpers-nextjs'; -import Link from 'next/link'; +import { useState } from 'react'; +import { Database } from '../db_types'; +import '../styles/globals.css'; function MyApp({ Component, pageProps }: AppProps) { + const router = useRouter(); + const [supabaseClient] = useState(() => + createBrowserSupabaseClient() + ); + return ( - - - Logout - + + + - + ); } diff --git a/examples/nextjs/pages/api/auth/[...supabase].ts b/examples/nextjs/pages/api/auth/[...supabase].ts deleted file mode 100644 index 801fb656..00000000 --- a/examples/nextjs/pages/api/auth/[...supabase].ts +++ /dev/null @@ -1,6 +0,0 @@ -import { handleAuth } from '@supabase/auth-helpers-nextjs'; - -export default handleAuth({ - logout: { returnTo: '/' }, - cookieOptions: { lifetime: 1 * 365 * 24 * 60 * 60 } // Keep the user logged in for a year. -}); diff --git a/examples/nextjs/pages/api/force-refresh.ts b/examples/nextjs/pages/api/force-refresh.ts deleted file mode 100644 index bab47093..00000000 --- a/examples/nextjs/pages/api/force-refresh.ts +++ /dev/null @@ -1,11 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from 'next'; -import { getUser } from '@supabase/auth-helpers-nextjs'; - -export default async function ForceRefresh( - req: NextApiRequest, - res: NextApiResponse -) { - // Run queries with RLS on the server - const user = await getUser({ req, res }, { forceRefresh: true }); - res.json(user); -} diff --git a/examples/nextjs/pages/api/protected-route.ts b/examples/nextjs/pages/api/protected-route.ts index 5ecef4c5..73e72c8e 100644 --- a/examples/nextjs/pages/api/protected-route.ts +++ b/examples/nextjs/pages/api/protected-route.ts @@ -1,13 +1,8 @@ // pages/api/protected-route.ts -import { - withApiAuth, - supabaseServerClient -} from '@supabase/auth-helpers-nextjs'; +import { withApiAuth } from '@supabase/auth-helpers-nextjs'; -export default withApiAuth(async function ProtectedRoute(req, res) { +export default withApiAuth(async function ProtectedRoute(req, res, supabase) { // Run queries with RLS on the server - const { data } = await supabaseServerClient({ req, res }) - .from('test') - .select('*'); + const { data } = await supabase.from('test').select('*'); res.json(data); }); diff --git a/examples/nextjs/pages/github-provider-token.tsx b/examples/nextjs/pages/github-provider-token.tsx index 41a30c74..2dadffe4 100644 --- a/examples/nextjs/pages/github-provider-token.tsx +++ b/examples/nextjs/pages/github-provider-token.tsx @@ -1,10 +1,5 @@ // pages/protected-page.js -import { - User, - withPageAuth, - getUser, - getProviderToken -} from '@supabase/auth-helpers-nextjs'; +import { User, withPageAuth } from '@supabase/auth-helpers-nextjs'; import Link from 'next/link'; export default function ProtectedPage({ @@ -31,12 +26,22 @@ export default function ProtectedPage({ export const getServerSideProps = withPageAuth({ redirectTo: '/', - async getServerSideProps(ctx) { - // Retrieve provider_token from cookies - const provider_token = getProviderToken(ctx); - // Get logged in user's third-party id from metadata - const { user } = await getUser(ctx); - const userId = user?.user_metadata.user_name; + async getServerSideProps(ctx, supabase) { + const { + data: { session }, + error + } = await supabase.auth.getSession(); + if (error) { + throw error; + } + if (!session) { + return { props: {} }; + } + + // Retrieve provider_token & logged in user's third-party id from metadata + const { provider_token, user } = session; + const userId = user.user_metadata.user_name; + const allRepos = await ( await fetch( `https://api.github.com/search/repositories?q=user:${userId}`, @@ -48,6 +53,7 @@ export const getServerSideProps = withPageAuth({ } ) ).json(); + return { props: { allRepos, user } }; } }); diff --git a/examples/nextjs/pages/index.tsx b/examples/nextjs/pages/index.tsx index 0420d0c3..492c8f01 100644 --- a/examples/nextjs/pages/index.tsx +++ b/examples/nextjs/pages/index.tsx @@ -1,11 +1,17 @@ -import { useUser } from '@supabase/auth-helpers-react'; -import { supabaseClient } from '@supabase/auth-helpers-nextjs'; +import { + useSessionContext, + useSupabaseClient +} from '@supabase/auth-helpers-react'; +import { Auth, ThemeSupa } from '@supabase/auth-ui-react'; import type { NextPage } from 'next'; import Link from 'next/link'; import { useEffect, useState } from 'react'; +import { Database } from '../db_types'; const LoginPage: NextPage = () => { - const { isLoading, user, error } = useUser(); + const { isLoading, session, error } = useSessionContext(); + const supabaseClient = useSupabaseClient(); + const [data, setData] = useState(null); useEffect(() => { @@ -13,24 +19,34 @@ const LoginPage: NextPage = () => { const { data } = await supabaseClient.from('test').select('*').single(); setData(data); } - if (user) loadData(); - }, [user]); - if (!user) + loadData(); + }, [supabaseClient]); + + if (!session) return ( <> {error &&

{error.message}

} {isLoading ?

Loading...

:

Loaded!

} + ); @@ -41,7 +57,7 @@ const LoginPage: NextPage = () => { supabaseServerClient] |{' '} + {#if $page.data.session} + + {/if} diff --git a/examples/sveltekit-email-password/src/routes/(app)/+page.server.ts b/examples/sveltekit-email-password/src/routes/(app)/+page.server.ts index 1ea0573f..8d0ca055 100644 --- a/examples/sveltekit-email-password/src/routes/(app)/+page.server.ts +++ b/examples/sveltekit-email-password/src/routes/(app)/+page.server.ts @@ -1,10 +1,12 @@ -import { supabaseClient } from '$lib/db'; -import { invalid, redirect } from '@sveltejs/kit'; +import { getSupabase } from '@supabase/auth-helpers-sveltekit'; +import { AuthApiError } from '@supabase/supabase-js'; +import { invalid, redirect, type ValidationError } from '@sveltejs/kit'; import type { Actions } from './$types'; -import { saveSession } from '@supabase/auth-helpers-sveltekit/server'; export const actions: Actions = { - async default({ request, cookies, url }) { + async default(event): Promise> { + const { request } = event; + const { supabaseClient } = await getSupabase(event); const formData = await request.formData(); const email = formData.get('email') as string; @@ -24,14 +26,12 @@ export const actions: Actions = { }); } - const { data, error } = await supabaseClient.auth.api.signInWithEmail(email, password, { - redirectTo: `${url.origin}/logging-in` - }); + const { error } = await supabaseClient.auth.signInWithPassword({ email, password }); - if (error || !data) { - if (error?.status === 400) { + if (error) { + if (error instanceof AuthApiError && error.status === 400) { return invalid(400, { - error: 'Invalid credentials', + error: 'Invalid credentials.', values: { email } @@ -45,7 +45,6 @@ export const actions: Actions = { }); } - saveSession(cookies, data); throw redirect(303, '/dashboard'); } }; diff --git a/examples/sveltekit-email-password/src/routes/(app)/+page.svelte b/examples/sveltekit-email-password/src/routes/(app)/+page.svelte index 80947881..71983fb4 100644 --- a/examples/sveltekit-email-password/src/routes/(app)/+page.svelte +++ b/examples/sveltekit-email-password/src/routes/(app)/+page.svelte @@ -1,18 +1,20 @@ diff --git a/examples/sveltekit-email-password/src/routes/(app)/+page.ts b/examples/sveltekit-email-password/src/routes/(app)/+page.ts index 8f480618..fd229ca7 100644 --- a/examples/sveltekit-email-password/src/routes/(app)/+page.ts +++ b/examples/sveltekit-email-password/src/routes/(app)/+page.ts @@ -1,9 +1,10 @@ import type { PageLoad } from './$types'; import { redirect } from '@sveltejs/kit'; -import { withAuth } from '@supabase/auth-helpers-sveltekit'; +import { getSupabase } from '@supabase/auth-helpers-sveltekit'; -export const load: PageLoad = withAuth(({ session }) => { - if (session.user) { +export const load: PageLoad = async (event) => { + const { session } = await getSupabase(event); + if (session) { throw redirect(303, '/dashboard'); } -}); +}; diff --git a/examples/sveltekit-email-password/src/routes/(app)/dashboard/+page.ts b/examples/sveltekit-email-password/src/routes/(app)/dashboard/+page.ts index 3e1c6d45..e812013d 100644 --- a/examples/sveltekit-email-password/src/routes/(app)/dashboard/+page.ts +++ b/examples/sveltekit-email-password/src/routes/(app)/dashboard/+page.ts @@ -1,15 +1,16 @@ import type { PageLoad } from './$types'; -import { withAuth } from '@supabase/auth-helpers-sveltekit'; -import type { TestTable } from '$lib/types'; import { redirect } from '@sveltejs/kit'; +import { getSupabase } from '@supabase/auth-helpers-sveltekit'; -export const load: PageLoad = withAuth(async ({ getSupabaseClient, session }) => { - if (!session.user) { +export const load: PageLoad = async (event) => { + const { session, supabaseClient } = await getSupabase(event); + if (!session) { throw redirect(303, '/'); } - const { data: testTable } = await getSupabaseClient().from('test').select('*'); + + const { data: testTable } = await supabaseClient.from('test').select('*'); return { testTable, user: session.user }; -}); +}; diff --git a/examples/sveltekit-email-password/src/routes/(app)/logging-in/+page@.svelte b/examples/sveltekit-email-password/src/routes/(app)/logging-in/+page@.svelte index 4bebf07f..ee0f68d1 100644 --- a/examples/sveltekit-email-password/src/routes/(app)/logging-in/+page@.svelte +++ b/examples/sveltekit-email-password/src/routes/(app)/logging-in/+page@.svelte @@ -4,10 +4,11 @@ import { page } from '$app/stores'; $: { - const redirectTo = $page.url.searchParams.get('redirect') ?? '/dashboard'; + const redirectTo = $page.url.searchParams.get('redirect'); + // check if user has been set in session store then redirect - if (browser && $page.data.session.user) { - goto(redirectTo); + if (browser && $page.data.session) { + goto(redirectTo ?? '/dashboard'); } } diff --git a/examples/sveltekit-email-password/src/routes/(app)/signup/+page.server.ts b/examples/sveltekit-email-password/src/routes/(app)/signup/+page.server.ts index cb8d2ea1..dbf4959e 100644 --- a/examples/sveltekit-email-password/src/routes/(app)/signup/+page.server.ts +++ b/examples/sveltekit-email-password/src/routes/(app)/signup/+page.server.ts @@ -1,9 +1,15 @@ -import { supabaseClient } from '$lib/db'; -import { invalid } from '@sveltejs/kit'; +import { getSupabase } from '@supabase/auth-helpers-sveltekit'; +import { AuthApiError } from '@supabase/supabase-js'; +import { invalid, type ValidationError } from '@sveltejs/kit'; import type { Actions } from './$types'; export const actions: Actions = { - async default({ request, url }) { + async default( + event + ): Promise | { message: string }> { + const { request, url } = event; + const { supabaseClient } = await getSupabase(event); + const formData = await request.formData(); const email = formData.get('email') as string; @@ -16,25 +22,39 @@ export const actions: Actions = { } if (!password) { return invalid(400, { - error: 'Please enter a password' + error: 'Please enter a password', + values: { + email + } }); } - const { data, error } = await supabaseClient.auth.api.signUpWithEmail(email, password, { - redirectTo: url.origin + const { error } = await supabaseClient.auth.signUp({ + email, + password, + options: { emailRedirectTo: url.origin } }); - if (error || !data) { - if (error?.status === 400) { + if (error) { + if (error instanceof AuthApiError && error.status === 400) { return invalid(400, { - error: 'Invalid credentials' + error: 'Invalid credentials.', + values: { + email + } }); } + return invalid(500, { - error: 'Server error. Try again later.' + error: 'Server error. Try again later.', + values: { + email + } }); } - return { success: true }; + return { + message: 'Please check your email for a magic link to log into the website.' + }; } }; diff --git a/examples/sveltekit-email-password/src/routes/(app)/signup/+page.svelte b/examples/sveltekit-email-password/src/routes/(app)/signup/+page.svelte index b11ac20a..3ae55fbb 100644 --- a/examples/sveltekit-email-password/src/routes/(app)/signup/+page.svelte +++ b/examples/sveltekit-email-password/src/routes/(app)/signup/+page.svelte @@ -1,20 +1,15 @@ @@ -22,11 +17,11 @@

Sign up

- {#if errors} -
{errors.form}
+ {#if form?.error} +
{form.error}
{/if} - {#if message} -
{message}
+ {#if form?.message} +
{form.message}
{/if}
@@ -35,7 +30,7 @@ { +export const load: LayoutServerLoad = async (event) => { return { - session: locals.session + session: await getServerSession(event) }; }; diff --git a/examples/sveltekit-email-password/src/routes/+layout.svelte b/examples/sveltekit-email-password/src/routes/+layout.svelte index d716e363..259a3ac4 100644 --- a/examples/sveltekit-email-password/src/routes/+layout.svelte +++ b/examples/sveltekit-email-password/src/routes/+layout.svelte @@ -1,13 +1,18 @@ diff --git a/examples/sveltekit-email-password/src/routes/+layout.ts b/examples/sveltekit-email-password/src/routes/+layout.ts new file mode 100644 index 00000000..d1f6aa5f --- /dev/null +++ b/examples/sveltekit-email-password/src/routes/+layout.ts @@ -0,0 +1,7 @@ +import type { LayoutLoad } from './$types'; +import { getSupabase } from '@supabase/auth-helpers-sveltekit'; + +export const load: LayoutLoad = async (event) => { + const { session } = await getSupabase(event); + return { session }; +}; diff --git a/examples/sveltekit-email-password/src/routes/logout/+page.server.ts b/examples/sveltekit-email-password/src/routes/logout/+page.server.ts index 621b4c39..64cb9c01 100644 --- a/examples/sveltekit-email-password/src/routes/logout/+page.server.ts +++ b/examples/sveltekit-email-password/src/routes/logout/+page.server.ts @@ -1,10 +1,11 @@ -import { deleteSession } from '@supabase/auth-helpers-sveltekit/server'; +import { getSupabase } from '@supabase/auth-helpers-sveltekit'; import { redirect } from '@sveltejs/kit'; import type { Actions } from './$types'; export const actions: Actions = { - async default({ cookies }) { - deleteSession(cookies); + async default(event) { + const { supabaseClient } = await getSupabase(event); + await supabaseClient.auth.signOut(); throw redirect(303, '/'); } }; diff --git a/examples/sveltekit-magic-link/package.json b/examples/sveltekit-magic-link/package.json index f3e1e7b3..782d20b8 100644 --- a/examples/sveltekit-magic-link/package.json +++ b/examples/sveltekit-magic-link/package.json @@ -14,7 +14,7 @@ }, "devDependencies": { "@sveltejs/adapter-auto": "next", - "@sveltejs/kit": "1.0.0-next.499", + "@sveltejs/kit": "1.0.0-next.504", "@typescript-eslint/eslint-plugin": "^5.10.1", "@typescript-eslint/parser": "^5.10.1", "eslint": "^7.32.0", @@ -34,6 +34,6 @@ "type": "module", "dependencies": { "@supabase/auth-helpers-sveltekit": "workspace:*", - "@supabase/supabase-js": "^1.35.3" + "@supabase/supabase-js": "2.0.0" } } diff --git a/examples/sveltekit-magic-link/src/app.d.ts b/examples/sveltekit-magic-link/src/app.d.ts index a06438fb..0bb5755b 100644 --- a/examples/sveltekit-magic-link/src/app.d.ts +++ b/examples/sveltekit-magic-link/src/app.d.ts @@ -4,11 +4,9 @@ // for information about these interfaces // and what to do when importing types declare namespace App { - interface Locals { - session: import('@supabase/auth-helpers-sveltekit').SupabaseSession; - } + // interface Locals {} interface PageData { - session: import('@supabase/auth-helpers-sveltekit').SupabaseSession; + session: import('@supabase/supabase-js').Session | null; } // interface Error {} // interface Platform {} diff --git a/examples/sveltekit-magic-link/src/hooks.client.ts b/examples/sveltekit-magic-link/src/hooks.client.ts new file mode 100644 index 00000000..b99dd39e --- /dev/null +++ b/examples/sveltekit-magic-link/src/hooks.client.ts @@ -0,0 +1 @@ +import '$lib/db'; diff --git a/examples/sveltekit-magic-link/src/hooks.server.ts b/examples/sveltekit-magic-link/src/hooks.server.ts index 9a754db1..b99dd39e 100644 --- a/examples/sveltekit-magic-link/src/hooks.server.ts +++ b/examples/sveltekit-magic-link/src/hooks.server.ts @@ -1,4 +1 @@ import '$lib/db'; -import { auth } from '@supabase/auth-helpers-sveltekit/server'; - -export const handle = auth(); diff --git a/examples/sveltekit-magic-link/src/lib/db.ts b/examples/sveltekit-magic-link/src/lib/db.ts index 7920a2fc..bc713e01 100644 --- a/examples/sveltekit-magic-link/src/lib/db.ts +++ b/examples/sveltekit-magic-link/src/lib/db.ts @@ -1,16 +1,4 @@ -import { createClient } from '@supabase/supabase-js'; -import { setupSupabaseHelpers } from '@supabase/auth-helpers-sveltekit'; +import { createClient } from '@supabase/auth-helpers-sveltekit'; import { env } from '$env/dynamic/public'; -import { dev } from '$app/environment'; -export const supabaseClient = createClient(env.PUBLIC_SUPABASE_URL, env.PUBLIC_SUPABASE_ANON_KEY, { - persistSession: false, - autoRefreshToken: false -}); - -setupSupabaseHelpers({ - supabaseClient, - cookieOptions: { - secure: !dev - } -}); +export const supabaseClient = createClient(env.PUBLIC_SUPABASE_URL, env.PUBLIC_SUPABASE_ANON_KEY); diff --git a/examples/sveltekit-magic-link/src/lib/types.ts b/examples/sveltekit-magic-link/src/lib/types.ts deleted file mode 100644 index a3c3d4e4..00000000 --- a/examples/sveltekit-magic-link/src/lib/types.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface TestTable { - id: string; - created_at: string; -} diff --git a/examples/sveltekit-magic-link/src/routes/+layout.server.ts b/examples/sveltekit-magic-link/src/routes/+layout.server.ts index 6e9e6314..32a841c7 100644 --- a/examples/sveltekit-magic-link/src/routes/+layout.server.ts +++ b/examples/sveltekit-magic-link/src/routes/+layout.server.ts @@ -1,7 +1,8 @@ import type { LayoutServerLoad } from './$types'; +import { getServerSession } from '@supabase/auth-helpers-sveltekit'; -export const load: LayoutServerLoad = async ({ locals }) => { +export const load: LayoutServerLoad = async (event) => { return { - session: locals.session + session: await getServerSession(event) }; }; diff --git a/examples/sveltekit-magic-link/src/routes/+layout.svelte b/examples/sveltekit-magic-link/src/routes/+layout.svelte index 109c209f..a4ae5933 100644 --- a/examples/sveltekit-magic-link/src/routes/+layout.svelte +++ b/examples/sveltekit-magic-link/src/routes/+layout.svelte @@ -1,13 +1,18 @@ diff --git a/examples/sveltekit-magic-link/src/routes/+layout.ts b/examples/sveltekit-magic-link/src/routes/+layout.ts new file mode 100644 index 00000000..d1f6aa5f --- /dev/null +++ b/examples/sveltekit-magic-link/src/routes/+layout.ts @@ -0,0 +1,7 @@ +import type { LayoutLoad } from './$types'; +import { getSupabase } from '@supabase/auth-helpers-sveltekit'; + +export const load: LayoutLoad = async (event) => { + const { session } = await getSupabase(event); + return { session }; +}; diff --git a/examples/sveltekit-magic-link/src/routes/+page.server.ts b/examples/sveltekit-magic-link/src/routes/+page.server.ts index 1af0d96d..bf9bfe79 100644 --- a/examples/sveltekit-magic-link/src/routes/+page.server.ts +++ b/examples/sveltekit-magic-link/src/routes/+page.server.ts @@ -1,16 +1,21 @@ -import { supabaseClient } from '$lib/db'; +import { getSupabase } from '@supabase/auth-helpers-sveltekit'; import { invalid } from '@sveltejs/kit'; import type { Actions } from './$types'; export const actions: Actions = { - async default({ request, url }) { + async default(event) { + const { request, url } = event; + const { supabaseClient } = await getSupabase(event); + const formData = await request.formData(); const email = formData.get('email') as string; - const { error } = await supabaseClient.auth.signIn( - { email }, - { redirectTo: `${url.origin}/logging-in` } - ); + const { error } = await supabaseClient.auth.signInWithOtp({ + email, + options: { + emailRedirectTo: `${url.origin}/logging-in` + } + }); if (error) { return invalid(400, { diff --git a/examples/sveltekit-magic-link/src/routes/+page.ts b/examples/sveltekit-magic-link/src/routes/+page.ts index 43030000..fd229ca7 100644 --- a/examples/sveltekit-magic-link/src/routes/+page.ts +++ b/examples/sveltekit-magic-link/src/routes/+page.ts @@ -1,9 +1,10 @@ import type { PageLoad } from './$types'; import { redirect } from '@sveltejs/kit'; -import { withAuth } from '@supabase/auth-helpers-sveltekit'; +import { getSupabase } from '@supabase/auth-helpers-sveltekit'; -export const load: PageLoad = withAuth(async ({ session }) => { - if (session.user) { +export const load: PageLoad = async (event) => { + const { session } = await getSupabase(event); + if (session) { throw redirect(303, '/dashboard'); } -}); +}; diff --git a/examples/sveltekit-magic-link/src/routes/Layout.svelte b/examples/sveltekit-magic-link/src/routes/Layout.svelte index ae17ac51..456168cb 100644 --- a/examples/sveltekit-magic-link/src/routes/Layout.svelte +++ b/examples/sveltekit-magic-link/src/routes/Layout.svelte @@ -17,7 +17,7 @@ Supabase Auth Helpers Demo