From df50462d069d950d32b496f162f092d8c40b2489 Mon Sep 17 00:00:00 2001 From: April Hall Date: Sun, 9 Feb 2025 21:26:33 -0500 Subject: [PATCH] fix: Add redirects based on auth state --- src/lib/functions/auth/auth-client.ts | 5 ----- src/routes/+page.server.ts | 13 +++++++++++-- src/routes/channel/[channel]/+page.server.ts | 13 +++++++++++-- src/routes/login/+page.server.ts | 13 +++++++++++-- src/routes/signup/+page.server.ts | 13 +++++++++++-- 5 files changed, 44 insertions(+), 13 deletions(-) delete mode 100644 src/lib/functions/auth/auth-client.ts diff --git a/src/lib/functions/auth/auth-client.ts b/src/lib/functions/auth/auth-client.ts deleted file mode 100644 index 5638b2a..0000000 --- a/src/lib/functions/auth/auth-client.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { createAuthClient } from 'better-auth/svelte'; -import { BETTER_AUTH_URL } from '$env/static/private'; -export const authClient = createAuthClient({ - baseURL: BETTER_AUTH_URL, -}); diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index bbd717b..08f08f3 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,12 +1,21 @@ import { db } from '$lib/server/db'; +import { auth } from '$lib/server/db/auth'; import { newChannelSchema } from '$lib/types/schema'; import { fail, redirect } from '@sveltejs/kit'; import { message, setError, superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import type { Actions } from './$types'; -export function load(): void { - redirect(308, '/channel/general'); +export async function load({ request }): Promise { + const session = await auth.api.getSession({ + headers: request.headers, + }); + + if (session) { + redirect(301, '/channel/general'); + } else { + redirect(401, '/signup'); + } } export const actions = { diff --git a/src/routes/channel/[channel]/+page.server.ts b/src/routes/channel/[channel]/+page.server.ts index fb6b8f4..371b3fb 100644 --- a/src/routes/channel/[channel]/+page.server.ts +++ b/src/routes/channel/[channel]/+page.server.ts @@ -1,8 +1,17 @@ import { db } from '$lib/server/db'; import type { TypeMessage } from '$lib/types'; -import { error } from '@sveltejs/kit'; +import { error, redirect } from '@sveltejs/kit'; +import { auth } from '$lib/server/db/auth'; + +export async function load({ params, request }): Promise<{ messages: TypeMessage[] }> { + const session = await auth.api.getSession({ + headers: request.headers, + }); + + if (!session) { + redirect(307, '/signup'); + } -export async function load({ params }): Promise<{ messages: TypeMessage[] }> { let messages: TypeMessage[]; const rows = await db.getMessages(params.channel, 50); diff --git a/src/routes/login/+page.server.ts b/src/routes/login/+page.server.ts index dc7ed3e..174d438 100644 --- a/src/routes/login/+page.server.ts +++ b/src/routes/login/+page.server.ts @@ -1,14 +1,23 @@ import { dev } from '$app/environment'; import { auth } from '$lib/server/db/auth'; import { loginSchema } from '$lib/types/schema'; +import { redirect } from '@sveltejs/kit'; import { fail, message, setError, superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import type { Actions } from './$types'; -export const load = async () => { +export async function load({ request }) { + const session = await auth.api.getSession({ + headers: request.headers, + }); + + if (session) { + redirect(307, '/channel/general'); + } + const form = await superValidate(zod(loginSchema)); return { form }; -}; +} export const actions = { login: async ({ request, cookies }) => { diff --git a/src/routes/signup/+page.server.ts b/src/routes/signup/+page.server.ts index 286606a..66f554f 100644 --- a/src/routes/signup/+page.server.ts +++ b/src/routes/signup/+page.server.ts @@ -1,14 +1,23 @@ import { dev } from '$app/environment'; import { auth } from '$lib/server/db/auth'; import { signupSchema } from '$lib/types/schema'; +import { redirect } from '@sveltejs/kit'; import { fail, message, setError, superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import type { Actions } from './$types'; -export const load = async () => { +export async function load({ request }) { + const session = await auth.api.getSession({ + headers: request.headers, + }); + + if (session) { + redirect(307, '/channel/general'); + } + const form = await superValidate(zod(signupSchema)); return { form }; -}; +} export const actions = { signup: async ({ request, cookies }) => {