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

337
Views
No obtener la URL solicitada real en el lado del servidor en Next.js

Tengo un middleware para auth.

Básicamente, el middleware maneja la autenticación.

ctx.pathname devolviendo el valor incorrecto . Por ejemplo, si solicito http://localhost:4001/student/courses/bd478029-2493-4602-9047-ab4ed83f4538 esta URL en el navegador , entonces devuelve /login en lugar /student/courses/[id] . Pero, para http://localhost:4001/login , regresa como el valor esperado, es decir: /login

¿Que esta pasando?


Aquí está mi código authMiddleware.js

 import { studentHome, studentLogin, teacherHome, teacherLogin } from "../utils/staticFields"; import redirectTo from "./redirectTo" import UserManager from "./UserManager"; const authMiddleware = ctx => { try { console.log('@@@@@', ctx.pathname, Date.now()) const user = UserManager.getCookie('user',ctx) const userObj = user ? JSON.parse(user) : null; let redirectUrl = "" // const prevUrl = ctx.asPath if(userObj){ if(userObj.type === "teacher" && (!ctx.pathname.startsWith("/teacher") || ctx.pathname === "/teacher/login")) redirectUrl = teacherHome; // user is logged in, redirect to teacher portal else if(userObj.type === "student" && !ctx.pathname.startsWith("/student")) redirectUrl = studentHome; // user is logged in, redirect to student portal } else if(!userObj && ctx.pathname.startsWith("/teacher") && ctx.pathname !== "/teacher/login"){ redirectUrl = teacherLogin; // user is not logged in, redirect to login (teacher) } else if(!userObj && ctx.pathname.startsWith("/student")){ // UserManager.setIntendedUrl(ctx,prevUrl) redirectUrl = studentLogin; // user is not logged in, redirect to login (student) } if(redirectUrl){ console.log("REDIRECTING FROM", ctx.pathname, "TO", redirectUrl) redirectTo(redirectUrl, { res: ctx.res, status: 301 }); } } catch (error) { console.error("Error in authMiddleware", error) } } export default authMiddleware

_app.js

 ... MyApp.getInitialProps = async ({ Component, ctx }) => { const pageProps = Component.getInitialProps ? await Component.getInitialProps(ctx) : {}; authMiddleware(ctx); return { pageProps } }
about 4 years ago · Juan Pablo Isaza
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!