Skip to content

Commit 07c8795

Browse files
author
Aleksander Grygier
committed
reafactor: Supabase data
1 parent 4cf058e commit 07c8795

File tree

6 files changed

+12
-57
lines changed

6 files changed

+12
-57
lines changed

apps/web/src/app.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@ declare global {
77
namespace App {
88
// interface Error {}
99
interface Locals {
10-
safeGetSession: () => Promise<{ session: Session | null; user: User | null }>;
1110
supabase: SupabaseClient;
12-
session: Session | null;
1311
user: User | null;
1412
}
1513

16-
interface PageData {
17-
session: Session | null;
18-
}
19-
2014
// interface PageState {}
2115
// interface Platform {}
2216
}

apps/web/src/handle-auth-guard.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

apps/web/src/handle-supabase.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type { Handle } from '@sveltejs/kit';
55

66
const handleSupabase: Handle = async ({ event, resolve }) => {
77
event.locals.supabase = createServerClient(
8-
envPublic.PUBLIC_SUPABASE_URL,
9-
envPrivate.SUPABASE_SERVICE_KEY ?? envPublic.PUBLIC_SUPABASE_ANON_KEY,
8+
`${envPublic.PUBLIC_SUPABASE_URL}`,
9+
`${envPrivate.SUPABASE_SERVICE_KEY ?? envPublic.PUBLIC_SUPABASE_ANON_KEY}`,
1010
{
1111
cookies: {
1212
getAll() {
@@ -21,27 +21,6 @@ const handleSupabase: Handle = async ({ event, resolve }) => {
2121
}
2222
);
2323

24-
event.locals.safeGetSession = async () => {
25-
const {
26-
data: { session }
27-
} = await event.locals.supabase.auth.getSession();
28-
29-
if (!session) {
30-
return { session: null, user: null };
31-
}
32-
33-
const {
34-
data: { user },
35-
error
36-
} = await event.locals.supabase.auth.getUser();
37-
if (error) {
38-
// JWT validation has failed
39-
return { session: null, user: null };
40-
}
41-
42-
return { session, user };
43-
};
44-
4524
return resolve(event, {
4625
filterSerializedResponseHeaders(name: string) {
4726
return name === 'content-range' || name === 'x-supabase-api-version';

apps/web/src/hooks.server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { sequence } from '@sveltejs/kit/hooks';
22
import handleSupabase from './handle-supabase';
3-
import handleAuthGuard from './handle-auth-guard';
43
import type { HandleServerError } from '@sveltejs/kit';
54

6-
export const handle = sequence(handleSupabase, handleAuthGuard);
5+
export const handle = sequence(handleSupabase);
76

87
export const handleError: HandleServerError = async ({ error }) => {
98
return {

apps/web/src/routes/+layout.server.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { LayoutServerLoad } from './$types';
22

3-
export const load: LayoutServerLoad = async ({
4-
depends,
5-
locals: { safeGetSession, supabase },
6-
cookies
7-
}) => {
8-
const { session, user } = await safeGetSession();
3+
export const load: LayoutServerLoad = async ({ depends, locals: { supabase }, cookies }) => {
4+
const {
5+
data: { user }
6+
} = await supabase.auth.getUser();
7+
8+
if (!user) return { cookies: cookies.getAll() };
99

1010
depends('get_user_recordings');
1111

@@ -42,7 +42,6 @@ export const load: LayoutServerLoad = async ({
4242
return {
4343
cookies: cookies.getAll(),
4444
recordings: recordingsWithSignedUrls,
45-
session,
4645
user
4746
};
4847
};

apps/web/src/routes/[account=account]/+page.server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { ServerLoad } from '@sveltejs/kit';
2+
import { redirect } from '@sveltejs/kit';
23

34
export const load: ServerLoad = async ({ locals }) => {
5+
if (!locals.user) redirect(303, '/sign-in-with-google');
6+
47
return {
58
recordings: []
69
};

0 commit comments

Comments
 (0)