Next.js Discord

Discord Forum

Next-auth azure ad spa not working

Unanswered
Rock Dove posted this in #help-forum
Open in Discord
Rock DoveOP
Im creating this new azure-ad integration with next-auth

import { NextAuthOptions, TokenSet } from 'next-auth';
import AzureADProvider, { AzureADProfile } from 'next-auth/providers/azure-ad';

export const authOptions: NextAuthOptions = {
  session: {
    strategy: 'jwt',
  },
  pages: {
    signIn: '/login',
    signOut: '/logout',
  },
  providers: [
    AzureADProvider({
      clientId: getAzureCredentials().clientId,
      clientSecret: getAzureCredentials().clientSecret,
      tenantId: getAzureCredentials().tenantId,
      checks: ['state', 'nonce'],
      wellKnown:
        'https://login.microsoftonline.com/organizations/v2.0/.well-known/openid-configuration',
      authorization: {
        params: {
          scope: 'openid email profile',
        },
      },
      idToken: true,
      profile(profile: AzureADProfile, tokens: TokenSet) {
        console.log('all azure tokens ', tokens);
        return {
          ...profile,
          azureToken: tokens.access_token ?? 'azure access token tok',
          role: profile.role ?? 'azure user',
          id: profile.id ?? 'azure defualt id',
          image: profile.image ?? 'azure default image',
        };
      },
    }),
  ],
  callbacks: {
    async session({ token, session }) {
      if (session?.user) {
        session.user.role = token.role;
        session.user.azureToken = token.azureToken;
      }
      return session;
    },
    async jwt({ token, user }) {
      if (user) {
        token.role = user.role;
        token.azureToken = user.azureToken;
      }
      return token;
    },
    redirect() {
      return '/dashboard';
    },
  },
};


in my main page I call the login like this

    <button
      onClick={async () => {
        await signIn('azure-ad');
      }}
    >
      button
  


BUT WHEN LOGGING IT THROWS THIS ERROR

from azure ad ive configured it as a SPA, I could not found what is the issue with this

0 Replies