Next.js Discord

Discord Forum

Next Auth - How send JWT to custom API with Facebook Provider

Unanswered
Oak Titmouse posted this in #help-forum
Open in Discord
Oak TitmouseOP
Hello everyone! I hope you're ok =)

Actually I'm stuck right now. When i'm done connecting to my Facebook business account. I want to send the access token to my back-end (NestJS). But i'm stuck because when I send to the back-end I need to get my companyId which is store in my Redux store. Do you have any idea how can I do this?

import axios from 'axios';
import NextAuth, { NextAuthOptions } from 'next-auth';
import FacebookProvider from 'next-auth/providers/facebook';

import { API_BASE_URL, apiConfig } from '@/lib/api';
import { Callback } from '@/lib/interfaces/callback';

export const authOptions: NextAuthOptions = {
  providers: [
    FacebookProvider({
      clientId: process.env.FACEBOOK_CLIENT_ID ?? '',
      clientSecret: process.env.FACEBOOK_CLIENT_SECRET ?? '',
    }),
  ],
  callbacks: {
    async signIn({ account }) {
      if (account) {
        const body: Callback = {
          companyId: '',
          providerAccountId: account.providerAccountId,
          provider: account.provider,
          access_token: account.access_token ?? '',
          expires_in: account.expires_at ?? 0,
        };
        await axios.post(`${API_BASE_URL}/oauth/callback`, body, apiConfig);
      }
      return true;
    },
  },
  secret: process.env.NEXTAUTH_SECRET,
};

const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };

0 Replies