From 9b9efa17685edd5f8d6820e610657ab0156813a8 Mon Sep 17 00:00:00 2001 From: MrExplode Date: Wed, 29 Nov 2023 10:32:58 +0100 Subject: [PATCH] feat: Redirect to login on valid token --- src/routes/dash/+page.server.ts | 10 ++++++++-- src/routes/dash/login/+page.server.ts | 17 +++++++++-------- src/routes/dash/register/+page.server.ts | 6 +++++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/routes/dash/+page.server.ts b/src/routes/dash/+page.server.ts index f932a65..b696d10 100644 --- a/src/routes/dash/+page.server.ts +++ b/src/routes/dash/+page.server.ts @@ -1,5 +1,5 @@ import { useApi } from '$lib/server/api' -import type { ProfileInfo } from '$lib/types' +import { isError, type ProfileInfo } from '$lib/types' import { redirect } from '@sveltejs/kit' import type { PageServerLoad } from './$types' @@ -7,7 +7,13 @@ export const load: PageServerLoad = async ({ cookies }) => { const token = cookies.get('token') if (token === undefined) throw redirect(307, '/dash/login') - const profileData = (await useApi(token as string, '/profile/info', 'GET')) as ProfileInfo + const res = await useApi(token as string, '/profile/info', 'GET') + if (isError(res)) { + // invalid authentication + throw redirect(307, '/dash/login') + } + + const profileData = res as ProfileInfo return { profileData } diff --git a/src/routes/dash/login/+page.server.ts b/src/routes/dash/login/+page.server.ts index bd1fbde..aa09d86 100644 --- a/src/routes/dash/login/+page.server.ts +++ b/src/routes/dash/login/+page.server.ts @@ -1,15 +1,16 @@ import type { PageServerLoad, Actions } from './$types' -import { fail } from '@sveltejs/kit' -import { setError, superValidate } from 'sveltekit-superforms/server' -import { loginSchema } from '$lib/schema' +import { setError, superValidate, actionResult } from 'sveltekit-superforms/server' import { redirect } from '@sveltejs/kit' -import { actionResult } from 'sveltekit-superforms/server' -import { error } from '@sveltejs/kit' -import { useApi } from '$lib/server/api' -import type { ApiError, LoginRequest, LoginResponse } from '$lib/server/apiTypes' import { isError } from '$lib/types' +import { loginSchema } from '$lib/schema' +import type { ApiError, LoginRequest, LoginResponse } from '$lib/server/apiTypes' +import { useApi } from '$lib/server/api' + +export const load: PageServerLoad = ({ cookies }) => { + // already logged in, straight to dashboard + const token = cookies.get('token') + if (token !== undefined) throw redirect(307, '/dash') -export const load: PageServerLoad = () => { return { form: superValidate(loginSchema) } diff --git a/src/routes/dash/register/+page.server.ts b/src/routes/dash/register/+page.server.ts index 2cb690b..778a530 100644 --- a/src/routes/dash/register/+page.server.ts +++ b/src/routes/dash/register/+page.server.ts @@ -6,7 +6,11 @@ import type { ApiError, RegisterRequest } from '$lib/server/apiTypes' import { useApi } from '$lib/server/api' import { isError } from '$lib/types' -export const load: PageServerLoad = () => { +export const load: PageServerLoad = ({ cookies }) => { + // already logged in, straight to dashboard + const token = cookies.get('token') + if (token !== undefined) throw redirect(307, '/dash') + return { form: superValidate(registerSchema) }