Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda