nithin_sikinam
nithin_sikinam

Reputation: 77

NextAuth Azure AD B2C Signin Error - Expected 200 OK, got: 404 Not Found

I am working on a Next.js project where I am integrating OAuth authentication using NextAuth. I have configured both Google and Azure AD B2C as authentication providers. While Google authentication is working fine, I am encountering a 404 Not Found error when attempting to sign in with Azure AD B2C.

Here's the relevant part of my code where I configure the authentication providers:

import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import AzureADB2CProvider from "next-auth/providers/azure-ad-b2c";

export const authOptions = {
    providers:[
        GoogleProvider({
            clientId: process.env.GOOGLE_ID ?? "",
            clientSecret: process.env.GOOGLE_SECRET ?? ""
        }),
        AzureADB2CProvider({
            tenantId: process.env.AZURE_AD_B2C_TENANT_NAME ?? "",
            clientId: process.env.AZURE_AD_B2C_CLIENT_ID ?? "",
            clientSecret: process.env.AZURE_AD_B2C_CLIENT_SECRET ?? "",
            authorization: {
                params: {
                  scope: "offline_access openid",
                },
            },
            checks: ["pkce"],
            client: {
                token_endpoint_auth_method: "none",
            }
        }),
    ],
    pages:{
        signIn : "/auth/Signin"
    },
    callbacks:{
        async redirect({ url, baseUrl }:any) {
            return baseUrl;
        },
        async signIn({ user, account, profile, email, credentials }:any) {
            return true;
        }
    }
};

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

I have set up the app registration in Azure AD B2C with All Microsoft account users as supported account types and have specified the redirect URL as

http://localhost:3000/api/auth/callback/azure-ad-b2c. The error stack trace suggests an issue during the OAuth process, specifically at the point where the Issuer.discover method is called, which leads to a 404 Not Found error.

I have double-checked my Azure AD B2C configuration and ensured that the tenant ID, client ID, and client secret are correct. I also verified the redirect URL in both Azure AD B2C and my NextAuth configuration.

Has anyone encountered a similar issue or have any insights on how to resolve this? Any help would be greatly appreciated!

Upvotes: 1

Views: 546

Answers (1)

Jiří Hroch
Jiří Hroch

Reputation: 1

I'm working on a similar issue.

What helped me was removing signIn from the pages section.

The result might look like this:

 ...
 export const authOptions = {
    providers:[
        GoogleProvider({
            clientId: process.env.GOOGLE_ID ?? "",
            clientSecret: process.env.GOOGLE_SECRET ?? ""
        }),
        AzureADB2CProvider({
            tenantId: process.env.AZURE_AD_B2C_TENANT_NAME ?? "",
            clientId: process.env.AZURE_AD_B2C_CLIENT_ID ?? "",
            clientSecret: process.env.AZURE_AD_B2C_CLIENT_SECRET ?? "",
            authorization: {
                params: {
                  scope: "offline_access openid",
                },
            },
            checks: ["pkce"],
            client: {
                token_endpoint_auth_method: "none",
            }
        }),
    ],
    callbacks:{
        async redirect({ url, baseUrl }:any) {
            return baseUrl;
        },
        async signIn({ user, account, profile, email, credentials }:any) {
            return true;
        }
    }
};

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

Another potential issue could be related to cookies, but I don’t see that in this example.

Upvotes: 0

Related Questions