{meta.title}
} +a0:session
| Strictly necessary | 1 Day | Authentication Purposes. | |a0:state
| Strictly necessary | 2 Hours | Authentication Purposes. | |youtube-nocookie
| Strictly necessary | Persistent |Privacy-enhanced cookie, essential for embedded videos. [Link.](https://support.google.com/youtube/answer/171780)
Privacy Enhanced Mode allows you to embed YouTube videos without using cookies that track viewing behavior. This means no activity is collected to personalize the viewing experience.| + +export const Page = ({ children }) =>
Monday - Friday | 2 business days
Monday - Friday | 1 business day
Monday - Friday | Business hours are from 6am to 6pm (local time), except where otherwise stated. + +export const Page = ({ children }) =>
{JSON.stringify({ user, allRepos }, null, 2)};
}
```
+
+export const Page = ({ children }) => I am not logged in
{:else}Welcome {$page.data.session.user.email}
@@ -208,7 +169,7 @@ You can now determine if a user is authenticated on the client-side by checking ## Client-side data fetching with RLS -For [row level security](/docs/learn/auth-deep-dive/auth-row-level-security) to work properly when fetching data client-side, you need to make sure to import the `{ supabaseClient }` from `$lib/db` and only run your query once the user is defined client-side in `$page.data.session`: +For [row level security](/docs/learn/auth-deep-dive/auth-row-level-security) to work properly when fetching data client-side, you need to make sure to import the `{ supabaseClient }` from `$lib/db` and only run your query once the session is defined client-side in `$page.data`: ```html -{#if $page.data.session.user} +{#if $page.data.session}client-side data fetching with RLS
{JSON.stringify(loadedData, null, 2)}
{/if}
@@ -246,47 +207,25 @@ For [row level security](/docs/learn/auth-deep-dive/auth-row-level-security) to
{JSON.stringify(user, null, 2)}
```
-For [row level security](/docs/learn/auth-deep-dive/auth-row-level-security) to work in a server environment, you need to use the `withAuth` helper to check if the user is authenticated. The helper extends the event with `session` and `getSupabaseClient()`:
+For [row level security](/docs/learn/auth-deep-dive/auth-row-level-security) to work in a server environment, you need to use the `getSupabase` helper to check if the user is authenticated. The helper requires the `event` and returns `session` and `supabaseClient`:
```ts title=src/routes/profile/+page.ts
-import type { PageLoad } from './$types';
-import { withAuth } from '@supabase/auth-helpers-sveltekit';
-import { redirect } from '@sveltejs/kit';
+import type { PageLoad } from './$types'
+import { getSupabase } from '@supabase/auth-helpers-sveltekit'
+import { redirect } from '@sveltejs/kit'
-interface TestTable {
- id: string;
- created_at: string;
-}
-
-export const load: PageLoad = withAuth(async ({ getSupabaseClient, session }) => {
- if (!session.user) {
- throw redirect(303, '/');
+export const load: PageLoad = async (event) => {
+ const { session, supabaseClient } = await getSupabase(event)
+ if (!session) {
+ throw redirect(303, '/')
}
- const { data: tableData } = await getSupabaseClient()
- .fromserver-side fetched data with RLS:
+{JSON.stringify(tableData, null, 2)}
+user:
+{JSON.stringify(user, null, 2)}
+```
+
+```ts title=src/routes/protected-route/+page.ts
+import { withAuth } from '@supabase/auth-helpers-sveltekit'
+import { redirect } from '@sveltejs/kit'
+import type { PageLoad } from './$types'
+
+export const load: PageLoad = withAuth(async ({ session, getSupabaseClient }) => {
+ if (!session.user) {
+ throw redirect(303, '/')
+ }
+
+ const { data: tableData } = await getSupabaseClient().from('test').select('*')
+ return { tableData, user: session.user }
})
```
-## Migrate from 0.6.11 and below to 0.7.0 {#migration}
+{JSON.stringify(tableData, null, 2)}
+{JSON.stringify(user, null, 2)}
+```
+
+```ts title=src/routes/protected-route/+page.ts
+// src/routes/profile/+page.ts
+import type { PageLoad } from './$types'
+import { getSupabase } from '@supabase/auth-helpers-sveltekit'
+import { redirect } from '@sveltejs/kit'
+
+export const load: PageLoad = async (event) => {
+ const { session, supabaseClient } = await getSupabase(event)
+ if (!session) {
+ throw redirect(303, '/')
+ }
+ const { data: tableData } = await supabaseClient.from('test').select('*')
+
+ return {
+ user: session.user,
+ tableData,
+ }
+}
+```
+
+