mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-10 04:19:32 +00:00
feat: support Microsoft Entra ID select_account prompt (#1219)
* feat: add optional prompt to create auth url * feat: add prompt option to microsoft config * docs: add requireSelectAccount option
This commit is contained in:
@@ -5,7 +5,6 @@ description: Microsoft provider setup and usage.
|
|||||||
|
|
||||||
Enabling OAuth with Microsoft Azure Entra ID (formerly Active Directory) allows your users to sign in and sign up to your application with their Microsoft account.
|
Enabling OAuth with Microsoft Azure Entra ID (formerly Active Directory) allows your users to sign in and sign up to your application with their Microsoft account.
|
||||||
|
|
||||||
|
|
||||||
<Steps>
|
<Steps>
|
||||||
<Step>
|
<Step>
|
||||||
### Get your Microsoft credentials
|
### Get your Microsoft credentials
|
||||||
@@ -18,21 +17,23 @@ Enabling OAuth with Microsoft Azure Entra ID (formerly Active Directory) allows
|
|||||||
### Configure the provider
|
### Configure the provider
|
||||||
To configure the provider, you need to pass the `clientId` and `clientSecret` to `socialProviders.microsoft` in your auth configuration.
|
To configure the provider, you need to pass the `clientId` and `clientSecret` to `socialProviders.microsoft` in your auth configuration.
|
||||||
|
|
||||||
```ts title="auth.ts"
|
```ts title="auth.ts"
|
||||||
import { betterAuth } from "better-auth"
|
import { betterAuth } from "better-auth"
|
||||||
|
|
||||||
export const auth = betterAuth({
|
export const auth = betterAuth({
|
||||||
socialProviders: {
|
socialProviders: {
|
||||||
microsoft: { // [!code highlight]
|
microsoft: { // [!code highlight]
|
||||||
clientId: process.env.MICROSOFT_CLIENT_ID as string, // [!code highlight]
|
clientId: process.env.MICROSOFT_CLIENT_ID as string, // [!code highlight]
|
||||||
clientSecret: process.env.MICROSOFT_CLIENT_SECRET as string, // [!code highlight]
|
clientSecret: process.env.MICROSOFT_CLIENT_SECRET as string, // [!code highlight]
|
||||||
|
// Optional
|
||||||
|
requireSelectAccount: true // [!code highlight]
|
||||||
}, // [!code highlight]
|
}, // [!code highlight]
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
</Step>
|
</Step>
|
||||||
</Steps>
|
|
||||||
|
|
||||||
|
</Steps>
|
||||||
|
|
||||||
## Sign In with Microsoft
|
## Sign In with Microsoft
|
||||||
|
|
||||||
@@ -41,14 +42,14 @@ To sign in with Microsoft, you can use the `signIn.social` function provided by
|
|||||||
- `provider`: The provider to use. It should be set to `microsoft`.
|
- `provider`: The provider to use. It should be set to `microsoft`.
|
||||||
|
|
||||||
```ts title="auth-client.ts" /
|
```ts title="auth-client.ts" /
|
||||||
import { createAuthClient } from "better-auth/client"
|
import { createAuthClient } from "better-auth/client";
|
||||||
|
|
||||||
const authClient = createAuthClient()
|
const authClient = createAuthClient();
|
||||||
|
|
||||||
const signIn = async () => {
|
const signIn = async () => {
|
||||||
const data = await authClient.signIn.social({
|
const data = await authClient.signIn.social({
|
||||||
provider: "microsoft",
|
provider: "microsoft",
|
||||||
callbackURL: "/dashboard" //the url to redirect to after the sign in
|
callbackURL: "/dashboard", //the url to redirect to after the sign in
|
||||||
})
|
});
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ export async function createAuthorizationURL({
|
|||||||
claims,
|
claims,
|
||||||
redirectURI,
|
redirectURI,
|
||||||
duration,
|
duration,
|
||||||
|
prompt,
|
||||||
}: {
|
}: {
|
||||||
id: string;
|
id: string;
|
||||||
options: ProviderOptions;
|
options: ProviderOptions;
|
||||||
@@ -21,6 +22,7 @@ export async function createAuthorizationURL({
|
|||||||
scopes: string[];
|
scopes: string[];
|
||||||
claims?: string[];
|
claims?: string[];
|
||||||
duration?: string;
|
duration?: string;
|
||||||
|
prompt?: boolean;
|
||||||
}) {
|
}) {
|
||||||
const url = new URL(authorizationEndpoint);
|
const url = new URL(authorizationEndpoint);
|
||||||
url.searchParams.set("response_type", "code");
|
url.searchParams.set("response_type", "code");
|
||||||
@@ -52,6 +54,9 @@ export async function createAuthorizationURL({
|
|||||||
if (duration) {
|
if (duration) {
|
||||||
url.searchParams.set("duration", duration);
|
url.searchParams.set("duration", duration);
|
||||||
}
|
}
|
||||||
|
if (prompt) {
|
||||||
|
url.searchParams.set("prompt", "select_account");
|
||||||
|
}
|
||||||
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,12 @@ export interface MicrosoftOptions
|
|||||||
* Disable profile photo
|
* Disable profile photo
|
||||||
*/
|
*/
|
||||||
disableProfilePhoto?: boolean;
|
disableProfilePhoto?: boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Require user to select their account even if only one account is logged in
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
requireSelectAccount?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const microsoft = (options: MicrosoftOptions) => {
|
export const microsoft = (options: MicrosoftOptions) => {
|
||||||
@@ -49,6 +55,7 @@ export const microsoft = (options: MicrosoftOptions) => {
|
|||||||
codeVerifier: data.codeVerifier,
|
codeVerifier: data.codeVerifier,
|
||||||
scopes,
|
scopes,
|
||||||
redirectURI: data.redirectURI,
|
redirectURI: data.redirectURI,
|
||||||
|
prompt: options.requireSelectAccount || false,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
validateAuthorizationCode({ code, codeVerifier, redirectURI }) {
|
validateAuthorizationCode({ code, codeVerifier, redirectURI }) {
|
||||||
|
|||||||
Reference in New Issue
Block a user