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

155
Views
No se puede obtener la cookie durante la inicialización de _app con getInitialProps

Estoy usando next-cookies para almacenar las credenciales de autenticación de la sesión del usuario, sin embargo, no puedo obtenerlas durante la inicialización de la aplicación, digamos, el usuario actualiza la página o regresa más tarde, sin embargo, después de que la aplicación se ha inicializado, o el usuario ha iniciado sesión-, lo hago navegar en la aplicación.

Esto es importante, porque quiero obtener algunos datos iniciales para que estén disponibles en la tienda redux desde el principio.

 // pages/_app.js import { useStore } from '../store/store'; import nextCookie from 'next-cookies'; function MyApp({ Component, pageProps }) { const Layout = Component.layout || MainLayout; const store = useStore(pageProps.initialReduxState); // custom useStore method to init store const pageTitle = Component.title || pageProps.title || 'Título de la página'; return ( <Provider store={store}> <Layout pageTitle={pageTitle}> <Component {...pageProps} /> </Layout> </Provider> ); } export default MyApp; MyApp.getInitialProps = async (ctx) => { const { token, user } = nextCookie(ctx); /* TO DO the idea is to get cookie from the server side and pass it to the client side, if the cookie is active, initial data will be triggered from the initializers */ return { pageProps: { token, user } }; };

Échale un vistazo:

ingrese la descripción de la imagen aquí

¿Es una mejor manera o una opción nativa para obtener la cookie sin tener que depender de una cookie diferente?

Por cierto, ya tengo un middleware que protege las rutas de usuarios no autenticados que funciona bien:

 // pages/_middleware.js export function middleware(req) { const activeSession = req.headers.get('cookie'); const url = req.nextUrl.clone(); if (activeSession) { if (req.nextUrl.pathname === '/login') { url.pathname = '/'; return NextResponse.redirect(url); } return NextResponse.next(); } url.pathname = '/login'; return NextResponse.rewrite(url); }
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!