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

251
Views
Dónde almacenar el token JWT de una API en next-auth

Implementé esto en next-auth siguiendo un tutorial en línea

 import NextAuth from "next-auth" import Providers from "next-auth/providers"; const https = require('https'); export default NextAuth({ providers: [ Providers.Credentials({ name: 'Credentials', credentials: { email: { label: "Email", type: "email" }, password: { label: "Password", type: "password" } }, async authorize(credentials) { const url = 'https://localhost/auth'; const httpsAgent = new https.Agent({ rejectUnauthorized: false, }); const res = await fetch(url, { method: 'POST', body: JSON.stringify(credentials), agent: httpsAgent, headers: { "Content-Type": "application/json" } }) const user = await res.json(); if (res.ok && user) { return user; } else { return null; } } }), // ...add more providers here ], callbacks: { async jwt(token, user, account, profile, isNewUser) { if (user?.type) { token.status = user.type } if (user?.username) { token.username = user.username; } return token }, async session(session, token) { session.type = token.type; session.username = token.username; return session } } })

bastante estándar. https://localhost/auth devuelve un objeto como este (lo llamé usuario por ahora)

 { token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE2MzY0MTE4NjEsImV4cCI6MTYzNjQxNTQ2MSwicm9sZXMiOlsiUk9MRV9VU0VSIl0sInVzZXJuYW1lIjoiZXJuYTM5QHdlYmVyLmNvbSJ9.Abenx1GhB-_d9LVpLfa2NYp62Lbw6U65EUQowA0jA_aykx1m-BlBR_YBcL4XIJsknJ99NN8Ees4Zxdsphfhjs7du4TR2MgTITHYy-BYjBX9CsluVSBpm-L7c-oK5vu70eumAy1ixy5MKOTN2EQYCm65RszSheIwZ4LN8vSuzxzZuLszRG9nbpauiHDpYCeLrNeNkz4lhTicfWkdPafR8vhqt4MIeCl-kxbMqc35UNmglzE7n-b9zVh4OhU7bSCoPKZySL5c4GSf7UFFD-mXIe6s9b4qYSXJuLpdspFJSgP7UoEGP1gh8fTb5MDZREYyZOpK3BMU8EdwokngVR9zrbw' }

Me gustaría saber cómo almacenar este token para usarlo en futuras llamadas a mi API. Puedo ver que el objeto token en la devolución de llamada de la sesión es

 { iat: 1636411862, exp: 1639003862 }

entonces next-aut no está haciendo esto por mí. ¿Debo establecer una cookie httpOnly en la devolución de llamada de la sesión? o justo después

 if (res.ok && user) {

justo antes de devolver al usuario?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Encontré una manera simplemente actualizando las devoluciones de llamada:

 callbacks: { async jwt(token, user, account, profile, isNewUser) { if (user?.token) { token.token = user.token; } return token; }, async session(session, token) { return session; } }

de esta manera, el token de la API ahora se almacena en una cookie httpOnly llamada __Secure-next-auth.session-token (suponiendo que el token de la API tenga el formato anterior).

about 4 years ago · Juan Pablo Isaza Report

0

en dart, guarde el token jwt en el almacenamiento local. luego puede acceder al almacenamiento local y conservar el token entre sesiones.

de lo contrario, almacene el token jwt en un singleton o un proveedor y evalúelo durante la sesión.

about 4 years ago · Juan Pablo Isaza Report

0

Si almacena el JWT en las cookies, cada vez que llama a su API puede verificar el encabezado de la cookie para ver si lo tiene.

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!