I need to take information from the nextAuth service profile function and forward it to the browser

Asked

Viewed 7 times

-1

Hello I am making an app that uses as authentication a backend of its own, and in the authentication with google I am using next-auth, it sends the tokenid to the api via post and with that the backend registers or logs the user. Then after registration it returns a jwt token as a way if user session:

{
  status: 200,
  message: 'Usuário logado com sucesso',
  data: {
    accessToken: 'tokenjwt'
  }
}

The problem is: I need to take this accessToken and save it as a cookie, but as the function occurs on the server side of nextauth, I do not know how to proceed, all attempts so far have been flawed, my code:

import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';
import { api } from '../../../services/api';
import nookies from 'nookies';
import { GetServerSideProps } from 'next';

export default NextAuth({
  providers: [
    Providers.Google({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      idToken: true,
      authorizationUrl:
        'https://accounts.google.com/o/oauth2/v2/auth?prompt=consent&access_type=offline&response_type=code',
      profile: async (profile, tokens) => {
        await api
          .post('/auth/google', {
            token: tokens.id_token
          })
          .then(({ data }) => {
            console.log(data);
            nookies.set(undefined, 'abstrakt.token', data.accessToken, {
              maxAge: 60 * 60 * 1 // 1 hour
            });
          })
          .catch((res) => {
            console.warn(res);
          });

        return {
          id: tokens.idToken,
          name: profile.name,
          image: profile.image
        };
      }
    })
  ],

  pages: {
    error: '/fronterror/signerror'
  }
});

In short, I want to save the accessToken coming from my cookie api.

  • Please clarify your specific problem or provide Additional Details to Highlight Exactly what you need. As it’s Currently Written, it’s hard to Tell Exactly what you’re asking.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.