Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

73
Visualizações
Correct way to use Credential authentication in next.js/next-auth.js

I am trying to properly implement an authentication with next.js/next-auth.js. Following the documentation I implemented the Providers authentication. The code below calls my api (in php), the api returns a token and the token is stored in a httpOnly cookie.

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 = process.env.NEXT_PUBLIC_API_PLATFORM_BASE_URL + '/auth';

        const httpsAgent = new https.Agent({
          rejectUnauthorized: false,
        });

        const response = await fetch(url, {
          method: 'POST',
          body: JSON.stringify(credentials),
          agent: httpsAgent,
          headers: {
            "Content-Type": "application/json"
          }
        })
        const user = await response.json();

        if (response.ok && user) {
          return user;
        } else {
          return null;
        }
      }
    }),
    // ...add more providers here
  ],
  callbacks: {
    async jwt(token, user, account, profile, isNewUser) {
      if (user?.token) {
        token = { accessToken: user.token };
      }
      return token;
    },

    async session(session, token) {
      session.accessToken = token.accessToken;
      return session;
    }
  }
})

I'd need to know how to retrieve the jwt token in the httpOnly cookie for future calls (tipically passing it to the api like Bearer <MYTOKEN>, using fetch), but I know JS does not have access to httpOnly cookies. In that case what is the best way to proceed?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda