Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
SvelteKit Hook Prevents Endpoint Request

Trying out SvelteKit and I'm having a hard time with hooks. The docs don't really seem to explain it all too well. My current understanding of hooks is that they basically allow you to interact with requests made to your server before they get to their destination? (I'm open to a better explanation - specifically the handle hook). My current issue is I made an endpoint called login. As the name suggests, it allows users to sign into my application by generating a token and storing it as a cookie on their client. This works until I add hooks. After reading the hooks description, I figured the handle hook is perfect for what I want to do - validate the token on each request - if invalid, reroute the user to the login screen, if valid, allow the request to continue uninterrupted.


export const handle: Handle = async ({ event, resolve }) => {

    const isLogin = event.url.pathname.startsWith('/login')

    const cookies = cookie.parse(event.request.headers.get('cookie') || '');
    const token = cookies['token']

    if (!token) {
        
        if (!isLogin) {
            
            return Response.redirect(`${event.url.origin}/login`)
        }
        return await resolve(event)
    } else {
        
        try {
            
            await verifyToken(token)

            if (isLogin) {
                
                return Response.redirect(`${event.url.origin}/about`)
            }
        } catch (err) {
            return Response.redirect(`${event.url.origin}/login`)
        }
    }

    return await resolve(event)

};

This does not work as expected. When I initiate the request to the api/login endpoint, the request does not seem to make it there. I have console.logs all over the endpoint but no messages were outputted to the terminal & when I check the application storage, no new cookie was added.

What am I missing about hooks? Why is it not passing the request off to the endpoint?

Any idea how I can fix this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The handle hook runs for every request—including endpoints.

When you fetch /api/login without a token, your hook will redirect the request to /login since isLogin === false. You need to allow through every route that should be accessible without a login, for example:

const isLogin = /^\/(api\/)?login$/.test(event.url.pathname)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!