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

226
Vistas
how to get data from a middleware in Nextjs?

I am trying to create an authentication system with jwt in nextjs. So I built a middleware that could verify the jwt token and use it to set up an authentication system like this:

export default function middleware(request, event) {
    const token = request.cookies.token
    let authenticated = false
    if (pathname === '/dashboard') {
        if (token) {
            try {
                jwt.verify(token, 'HvpkPMarcwoZFAo')
                authenticated = true
            } catch {
                return NextResponse.redirect('/signin')
            }
        } else {
            return NextResponse.redirect('/signin')
        }
    }
}

According to the above code if the user is not logged in, he will be redirected to the login page.

But the problem is that I want some components to change according to authentication (for example, do not show the registration button for logged in users). Can I get data from this middleware (for example a boolean value) to be used in different components? I used response.locals in Expressjs. Is there a similar solution in Nextjs?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should look at https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering

getServerSideProps receives context in params ;)

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