Skip to content

Commit

Permalink
feat: Redirect to login on valid token
Browse files Browse the repository at this point in the history
  • Loading branch information
MrExplode committed Nov 29, 2023
1 parent e76ef05 commit 9b9efa1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/routes/dash/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
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'

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
}
Expand Down
17 changes: 9 additions & 8 deletions src/routes/dash/login/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion src/routes/dash/register/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 9b9efa1

Please sign in to comment.