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

224
Views
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 answers
Answer question

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 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!