Docs

Microsoft

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.

Get your Microsoft credentials

To use Microsoft as a social provider, you need to get your Microsoft credentials. Which involves generating your own Client ID and Client Secret using your Microsoft Entra ID dashboard account.

see the Microsoft Entra ID documentation for more information.

Configure the provider

To configure the provider, you need to pass the clientId and clientSecret to socialProviders.microsoft in your auth configuration.

auth.ts
import { betterAuth } from "better-auth"
 
export const auth = betterAuth({
    socialProviders: { 
        microsoft: { 
            clientId: process.env.MICROSOFT_CLIENT_ID as string, 
            clientSecret: process.env.MICROSOFT_CLIENT_SECRET as string, 
        }, 
    }, 
})

Signin with Microsoft

To signin with Microsoft, you can use the signIn.social function provided by the client. The signIn function takes an object with the following properties:

  • provider: The provider to use. It should be set to microsoft.
client.ts
import { createAuthClient } from "better-auth/client"
 
const client = createAuthClient()
 
const signin = async () => {
    const data = await client.signIn.social({
        provider: "microsoft",
        callbackURL: "/dashboard" //the url to redirect to after the signin
    })
}

On this page

Edit on GitHub