Skip to content

Commit 72d4c5b

Browse files
feat(providers): refactor Azure B2C provider (#2862)
* fix(providers): refactor AzureB2C provider * chore(dev): add Azure B2C to the dev app * chore(providers): remove unnecessary config
1 parent f635035 commit 72d4c5b

File tree

3 files changed

+48
-44
lines changed

3 files changed

+48
-44
lines changed

app/pages/api/auth/[...nextauth].ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import SpotifyProvider from "next-auth/providers/spotify"
2222
import CognitoProvider from "next-auth/providers/cognito"
2323
import SlackProvider from "next-auth/providers/slack"
2424
import Okta from "next-auth/providers/okta"
25+
import AzureB2C from "next-auth/providers/azure-ad-b2c"
2526

2627
// import { PrismaAdapter } from "@next-auth/prisma-adapter"
2728
// import { PrismaClient } from "@prisma/client"
@@ -160,6 +161,12 @@ export default NextAuth({
160161
clientId: process.env.SLACK_ID,
161162
clientSecret: process.env.SLACK_SECRET,
162163
}),
164+
AzureB2C({
165+
clientId: process.env.AZURE_B2C_ID,
166+
clientSecret: process.env.AZURE_B2C_SECRET,
167+
tenantId: process.env.AZURE_B2C_TENANT_ID,
168+
primaryUserFlow: process.env.AZURE_B2C_PRIMARY_USER_FLOW,
169+
}),
163170
],
164171
jwt: {
165172
encryption: true,

src/providers/azure-ad-b2c.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/providers/azure-ad-b2c.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { OAuthConfig, OAuthUserConfig } from "."
2+
3+
export interface AzureB2CProfile {
4+
exp: number
5+
nbf: number
6+
ver: string
7+
iss: string
8+
sub: string
9+
aud: string
10+
iat: number
11+
auth_time: number
12+
oid: string
13+
country: string
14+
name: string
15+
postalCode: string
16+
emails: string[]
17+
tfp: string
18+
}
19+
20+
export default function AzureADB2C<
21+
P extends Record<string, any> = AzureB2CProfile
22+
>(options: OAuthUserConfig<P> & { primaryUserFlow: string }): OAuthConfig<P> {
23+
const { tenantId, primaryUserFlow } = options
24+
return {
25+
id: "azure-ad-b2c",
26+
name: "Azure Active Directory B2C",
27+
type: "oauth",
28+
wellKnown: `https://${tenantId}.b2clogin.com/${tenantId}.onmicrosoft.com/${primaryUserFlow}/v2.0/.well-known/openid-configuration`,
29+
idToken: true,
30+
profile(profile) {
31+
return {
32+
id: profile.sub,
33+
name: profile.name,
34+
email: profile.emails[0],
35+
// TODO: Find out how to retrieve the profile picture
36+
image: null,
37+
}
38+
},
39+
options,
40+
}
41+
}

0 commit comments

Comments
 (0)