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

766
Vistas
how to store and get access_token in NextJS SSR

I use NextJS with ServerSide Rendering

I use an authentication service with access_token (JWT)

I can't store access_token in the localStorage because it's not available in the getServerSideProps

so I put access_token in an httpOnly cookie to be available on the server.

I have some requests on the server and some on the client, so I need to get the access_token in two ways, on the server from req.headers.cookie and on the client from document.cookie

I want to wrtie axios interceptors to add access_token to the every request.

this works for the client-side, but what can I do for the server-side ?

import axios from 'axios';

const _axios = axios.create();
axiosInstance.interceptors.request.use(function (config) {
  let token;

  // check if it's on the client-side
  if(typeof document !== 'undefined')
     token = getToken(document.cookie);

  // how to check for server-side and how to get cookie ?

  return {
    ...config,
      headers: {
      'Authorization': `Bearer ${token}`
    }
  };
}, function (error) {
  return Promise.reject(error);
});

export default axiosInstance;
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You may want to use nookies

From getServerSideProps

export async function getServerSideProps(ctx) {

    //Parse cookies from request header
    const cookies = nookies.get(ctx)

    //Set cookies on response header
    nookies.set(ctx, 'key', 'token', { path: '/' })

    ...

}

From API route

export default function handler(req, res) {

    //Parse cookies from request header
    const cookies = nookies.get({ req })

    //Set cookies on response header
    nookies.set({ res }, 'key', 'token', { path: '/' })

    ...
}
over 4 years ago · Santiago Trujillo 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