authentication - Better-auth : I can't connect to Microsoft with generic OAuth provider - Stack Overflow

I'm currently trying to implement two Microsoft provider with the better-auth package but I keep g

I'm currently trying to implement two Microsoft provider with the better-auth package but I keep getting this error :

When I log with my personnal account

I verified if my callbackUrl is correctly setup in my Microsoft azure dashboard and it's the same between the dashboard callbackUrl and my app one.

There is the auth config with better-auth :

.....

const options = {
  baseURL: baseURL,
  database: prismaAdapter(adminPrisma, {
    provider: "postgresql",
  }),
  emailAndPassword: {
    enabled: true,
  },
  user: {
    additionalFields: {
      anizationId: {
        type: "string",
      },
      providerId: {
        type: "string",
      },
    },
  },
  session: {
    cookieCache: {
      enabled: true,
      maxAge: 5 * 60, // Cache duration in seconds
    },
    fields: {
      expiresAt: "expires", // e.g., "expires_at" or your existing field name
      token: "sessionToken", // e.g., "session_token" or your existing field name
    },
  },
  account: {
    accountLinking: {
      enabled: true,
    },
    fields: {
      accountId: "providerAccountId",
      providerId: "provider",
      refreshToken: "refresh_token",
      accessToken: "access_token",
      accessTokenExpiresAt: "expires_at",
      idToken: "id_token",
    },
  },
  verification: {
    // better auth not recognizing our model name, we have to override it
    modelName: "Verification",
  },
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
      accessType: "offline",
      prompt: "consent",
      mapProfileToUser(profile) {
        return {
          ...profile,
          providerId: "google",
        };
      },
    },
  },
  plugins: [
    adminPlugin({
      defaultRole: "client",
      ac: ac,
      roles: {
        admin,
        superadmin,
        client,
      },
      adminRoles: ["superadmin", "admin"],
    }),
    magicLink({
      sendMagicLink: async ({ email, token, url }, request) => {
        await sendVerificationRequest({
          identifier: email,
          url,
          provider: {
            from: process.env.NEXT_PUBLIC_RESEND_NO_REPLY || "",
            apiKey:
              process.env.RESEND_FULL_ACCESS_API_KEY || "api_key_placeholder",
          },
        });
      },
    }),
    genericOAuth({
      config: [
        {
          providerId: "microsoft-entra-id-fgta",
          clientId: process.env.FGTA_AUTH_MICROSOFT_ENTRA_ID_ID as string,
          clientSecret: process.env
            .FGTA_AUTH_MICROSOFT_ENTRA_ID_SECRET as string,
          discoveryUrl: `/${process.env.FGTA_AUTH_MICROSOFT_ENTRA_ID_TENANT_ID}/v2.0/.well-known/openid-configuration`,
          redirectURI: `${process.env.HOSTNAME_FGTA || "http://localhost:3000"}/api/auth/oauth2/callback/microsoft-entra-id-fgta`,
          responseType: "code",
          prompt: "select_account",
          scopes: ["offline_access", "openid", "profile", "email", "Mail.Send"],
          mapProfileToUser(profile) {
            return {
              ...profile,
              providerId: "microsoft-entra-id-fgta",
            };
          },
        },
        {
          providerId: "microsoft-entra-id",
          clientId: process.env.AUTH_MICROSOFT_ENTRA_ID_ID as string,
          clientSecret: process.env.AUTH_MICROSOFT_ENTRA_ID_SECRET as string,
          discoveryUrl: `.0/.well-known/openid-configuration`,
          redirectURI: `${baseURL}/api/auth/oauth2/callback/microsoft-entra-id`,
          responseType: "code",
          prompt: "select_account",
          scopes: ["offline_access", "openid", "profile", "email", "Mail.Send"],
          mapProfileToUser(profile) {
            return {
              ...profile,
              providerId: "microsoft-entra-id",
            };
          },
        },
        // Add more providers as needed
      ],
    }),
  ],
} satisfies BetterAuthOptions;

.....

Note : the microsoft-entra-id-fgta works but not the microsoft

How can I solve this ? Does anyone did get this error ?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744940650a4602300.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信