mirror of
https://github.com/LukeHagar/Sveltey.git
synced 2025-12-06 04:21:38 +00:00
adjusted auth guard
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { createServerClient } from '@supabase/ssr';
|
import { createServerClient } from '@supabase/ssr';
|
||||||
import { type Handle, redirect } from '@sveltejs/kit';
|
import { type Handle, redirect } from '@sveltejs/kit';
|
||||||
import { sequence } from '@sveltejs/kit/hooks';
|
import { sequence } from '@sveltejs/kit/hooks';
|
||||||
|
|
||||||
import { PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY } from '$env/static/public';
|
import { PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY } from '$env/static/public';
|
||||||
|
|
||||||
const supabase: Handle = async ({ event, resolve }) => {
|
const supabase: Handle = async ({ event, resolve }) => {
|
||||||
@@ -67,20 +68,16 @@ const authGuard: Handle = async ({ event, resolve }) => {
|
|||||||
event.locals.session = session;
|
event.locals.session = session;
|
||||||
event.locals.user = user;
|
event.locals.user = user;
|
||||||
|
|
||||||
console.log(event.locals);
|
console.log({session, user});
|
||||||
|
|
||||||
// Protect routes that require authentication
|
// Protect routes that require authentication
|
||||||
if (event.url.pathname.startsWith('/app')) {
|
if (!event.locals.session && event.url.pathname.startsWith('/app')) {
|
||||||
if (!session) {
|
redirect(303, '/auth')
|
||||||
throw redirect(303, '/auth');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect authenticated users away from auth pages
|
// Redirect authenticated users away from auth pages
|
||||||
if (event.url.pathname !== "/auth/logout" && event.url.pathname.startsWith('/auth' )) {
|
if (event.locals.session && event.url.pathname === '/auth') {
|
||||||
if (session) {
|
redirect(303, '/app/dashboard')
|
||||||
throw redirect(303, '/app/dashboard');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolve(event);
|
return resolve(event);
|
||||||
|
|||||||
Reference in New Issue
Block a user