Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda