Replies: 4 comments 13 replies
-
Just return return new Response(null, {
status: 300,
headers: { location: '/login' }
}) |
Beta Was this translation helpful? Give feedback.
-
From the docs https://kit.svelte.dev/docs/modules#sveltejs-kit-redirect
Perhaps the |
Beta Was this translation helpful? Give feedback.
-
I ran into the same issue a while back. I concluded that redirecting from hooks was very problematic given that hooks wasn't firing on client side navigation. My solution was to put Unauthorized errors in hooks, but to put my redirect guards into the |
Beta Was this translation helpful? Give feedback.
-
I ran into same problem and i found a solution. This might not the the your required solution. import type { Handle } from '@sveltejs/kit';
// custom redirect from joy of code `https://github.com/JoysOfCode/sveltekit-auth-cookies/blob/migration/src/hooks.ts`
function redirect(location: string, body?: string) {
return new Response(body, {
status: 303,
headers: { location }
});
}
const unProtectedRoutes: string[] = ['/', '/login']
export const handle: Handle = async ({ event, resolve }) => {
const session = event.cookies.get('session');
if (!session && !unProtectedRoutes.includes(event.url.pathname)) return redirect('/login', 'No authenticated user.');
const currentUser = await db.fetch(session as string);
if (currentUser) {
event.locals.user = {
name: currentUser.name,
email: currentUser.email,
type: currentUser.user_type,
active: currentUser.active,
phone: currentUser.phone
};
} else if (!unProtectedRoutes.includes(event.url.pathname)) {
return redirect('/login')
}
return resolve(event);
}; you can do it like that there is bit code missing. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Throw redirect(300,'/login') does not work.
Please help.
Or is it that now the only way to do so is through +page.server.ts via the load handler for the corresponding pathname?
Beta Was this translation helpful? Give feedback.
All reactions