Store and authenticate FB/Google users from NextAuth to another backend api in Nextjs

Viewed 561

I'm using Next.js(v.10.0.4) and NextAuth(v.3.23.3) for a project which requires social authentication. I'm able to login/logout via a custom login page including a session-based social authentication system provided by NextAuth.

Now I would like to store the user's information in an external API and later on, I'd like to fetch the user's data via JWT.

I'm not sure how should I approach this. Any suggestion/example/solution idea will be highly appreciated. Thanks :)

api/[...nextauth].js]

import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
import axios from 'axiosConfig'

const options = {
  providers: [
    Providers.Facebook({
      clientId: process.env.FACEBOOK_ID,
      clientSecret: process.env.FACEBOOK_SECRET,
    }),
    Providers.Google({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET,
    }),
  ],
  pages: {
    signIn: '/',
    signOut: '/',
    error: '/auth/error', // Error code passed in query string as ?error=
    verifyRequest: '/auth/verify-request', // (used for check email message)
    newUser: null, // If set, new users will be directed here on first sign in
  },
  session: {
    jwt: true,
  },
  callbacks: {
    session: async (session, user) => {
      session.jwt = user.jwt
      session.id = user.id
      return Promise.resolve(session)
    },
    jwt: async (token, user, account) => {
      const isSignIn = user ? true : false
      if (isSignIn) {
        // 
      }
      return Promise.resolve(token)
    },
  },
}

export default (req, res) => NextAuth(req, res, options)
0 Answers
Related