Skip to content

Commit

Permalink
Revert "Try getting token from headers more directly, use locals for …
Browse files Browse the repository at this point in the history
…route bl…" (#145)

This reverts commit 0de04e1.
  • Loading branch information
franknoirot authored Jul 20, 2024
1 parent 0de04e1 commit 0100257
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ declare global {
// interface Error {}
interface Locals {
user?: Models['User_type'] | Models['Error_type']
token?: string
}
// interface PageData {}
// interface Platform {}
Expand Down
4 changes: 1 addition & 3 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import { hooksUserMocks, isUserMock } from '$lib/mocks'
export const handle = async ({ event, resolve }) => {
const mock = event.request.headers.get(PLAYWRIGHT_MOCKING_HEADER)
const token = import.meta.env.PROD
? event.request.headers.get(AUTH_COOKIE_NAME)
? event.cookies.get(AUTH_COOKIE_NAME)
: import.meta.env.VITE_TOKEN

if (!token) {
return resolve(event)
} else {
event.locals.token = token
}

const currentUser = await event
Expand Down
1 change: 0 additions & 1 deletion src/routes/(sidebarLayout)/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { redirect } from '@sveltejs/kit'
export const load = async ({ locals, cookies }) => {
// redirect user if not logged in
if (!locals.user) {
locals.token = undefined
cookies.delete(AUTH_COOKIE_NAME, { domain: DOMAIN, path: '/' })
throw redirect(302, '/')
}
Expand Down
11 changes: 9 additions & 2 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { AUTH_COOKIE_NAME } from '$lib/cookies.js'

/** @type {import('./$types').LayoutData} */
export const load = async ({ locals }) => {
export const load = async ({ locals, cookies }) => {
const token =
import.meta.env.MODE === 'production'
? cookies.get(AUTH_COOKIE_NAME)
: import.meta.env.VITE_TOKEN

return {
user: !locals.user || 'error_code' in locals.user ? undefined : locals.user,
token: locals.token
token
}
}
10 changes: 6 additions & 4 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { paths } from '$lib/paths.js'
import { AUTH_COOKIE_NAME } from '$lib/cookies.js'
import { redirect } from '@sveltejs/kit'

export const load = async ({ url, locals }) => {
if (locals.token && locals.user) {
throw redirect(302, paths.DASHBOARD + (url.search || ''))
export const load = async ({ cookies, url }) => {
const token = import.meta.env.PROD ? cookies.get(AUTH_COOKIE_NAME) : import.meta.env.VITE_TOKEN

if (token) {
throw redirect(302, '/dashboard' + (url.search || ''))
}
}

0 comments on commit 0100257

Please sign in to comment.