Home > Enterprise >  Reditect from handle function in Hooks.js , SvelteKit
Reditect from handle function in Hooks.js , SvelteKit

Time:04-02

I tried this solution from StackOverflow and some other solutions
Redirecting from exported handle hook in sveltekit

I tried to redirect after authentication from the handle function inside hooks.js. I tried to like this

 return  response.redirect(event.url.origin "/login2", 303);

but this gives me an error

500
TypeError: response.redirect is not a functionat Object.handle (/mnt/golang/vscode Web/svelteKit/s16JWT/src/hooks.ts:48:30)at async respond (file:///mnt/golang/vscode Web/svelteKit/s16JWT/.svelte-kit/runtime/server/index.js:2685:20)at async file:///mnt/golang/vscode Web/svelteKit/s16JWT/node_modules/@sveltejs/kit/dist/chunks/index.js:299:24

i tried in some other methods in some other places inside my code base , only place i could redirect from <script context="module"></script> or <script></script> using goto() method or header redirect. help me to fix this problem

CodePudding user response:

This is the code I would use if I am setting up a redirect header with a link.

export const handle: Handle = async({event, resolve})=>{
 return new Response('Redirect', {status: 303, headers: { Location: '/login' }})
}

Basically you are sending a custom response with new custom headers.

There's no need to prepend an origin, though. I am sure you get that from the load function of the context module.

CodePudding user response:

I've run in to issues with redirect from handler a few times. if youve set other headers - cookies - then you might run in to problems redirecting.

  • Related