Next.js Discord

Discord Forum

Microsoft Entra Id + External Ids

Unanswered
Mucuchies posted this in #help-forum
Open in Discord
MucuchiesOP
Hey all, I'm trying to get a basic example done using Microsoft Entra Id External Id's.

I'd like to start by saying I believe the application is registered and configured properly in Azure Entra already. Testing the sign up & sign in appear to work.

I have tried a few different things and I feel like I'm kind of heading in circles at this point so any help would be greatly appreciated.

If we start with the very familiar nextjs-dashboard example: or the final version w/e you prefer: npx create-next-app@latest nextjs-dashboard --example "https://github.com/vercel/next-learn/tree/main/dashboard/final-example" --use-pnpm

On the landing page at '/' there is a familiar 'Log in -> ' button.

- When click the Login it initiates the user flow with the sign in page
- When I enter my email address it sends me a code
- When I enter the code it would appear to sign in successfully but fails to process the response.

During the failure I receive the following:
 GET / 200 in 1987ms
 ✓ Compiled /api/auth/[...nextauth] in 336ms
 ⨯ [TypeError: Function.prototype.apply was called on #<Object>, which is an object and not a function]
 ⨯ [TypeError: Function.prototype.apply was called on #<Object>, which is an object and not a function]
 GET /api/auth/callback/microsoft-entra-id 500 in 996ms


The URL that is sent in the browser is: http://localhost:3000/api/auth/callback/microsoft-entra-id#id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1N...removed...RbqtPZr3XWmG...&session_state=00301899-xxxx-de67-1a4a-41f9caxd3ex5

Any ideas on what I'm missing?

2 Replies

MucuchiesOP
I have done a bit more research into this. There is some odd behavior when the home page loads it appears to be trying to execute next-auth routes. In particular: GET /api/auth/session 500 in 120ms I get several of these when the page loads.
export const authConfig = {
  pages: {
    signIn: loginUrl,
    signOut: '/auth/signout',
    
  },
  session: {
    strategy: 'jwt',
  },
  providers: [
    MicrosoftEntraID({
      clientId: process.env.AUTH_MICROSOFT_ENTRA_ID_ID,
      clientSecret: process.env.AUTH_MICROSOFT_ENTRA_ID_SECRET,
      issuer: process.env.AUTH_MICROSOFT_ENTRA_ID_ISSUER,
    }),
  ],
  callbacks: {  
    async signIn(user, account, profile) {
      //throw new Error('signIn() not implemented');
      return user || {};
    },
    async authorized({ request, auth }) {
      return !!auth.user;
    },
    async redirect({ url, baseUrl }) {
      return url.startsWith(baseUrl) ? url : `${baseUrl}/dashboard`;
    },
    async session({ session, token, user }) {
      session.user.claims = token.claims;
      return session;
    },
    async jwt({ token, user, account, profile, isNewUser }) {
      if (user) {
        token.claims = (user as User).claims;
      }
      return token;
    },
  },
  logger: {
    error(code, ...message) {
      log.error(code, message)
    },
    warn(code, ...message) {
      log.warn(code, message)
    },
    debug(code, ...message) {
      log.debug(code, message)
    }
  }
} satisfies NextAuthConfig;