Skip to content

Better-Auth

MC-ID is built using better-auth and is fully compatible with it. This guide shows how to add MC-ID as an identity provider in your better-auth application.

Terminal window
npm install better-auth

Use the genericOAuth plugin to add MC-ID as a provider:

import { betterAuth } from "better-auth";
import { genericOAuth } from "better-auth/plugins";
export const auth = betterAuth({
// ... your other config
plugins: [
genericOAuth({
config: [
{
providerId: "mc-id",
clientId: process.env.MCID_CLIENT_ID!,
clientSecret: process.env.MCID_CLIENT_SECRET!,
discoveryUrl: "https://mc-id.com/.well-known/openid-configuration",
scopes: ["openid", "profile", "email", "connections"],
}
]
})
]
});

See MC-ID Scopes

Once configured, you can trigger the MC-ID login flow:

import { signIn } from "./auth-client";
// Redirect to MC-ID login
signIn("mc-id", { callbackURL: "/dashboard" });

After authentication, the user object will include the claims from the userinfo endpoint based on the scopes you requested:

{
"sub": "AaltJ3XUoyDQiqDVk865CILljZBXrjZz",
"name": "Notch",
"email": "user@example.com",
"email_verified": true,
"accounts": [
{
"uuid": "069a79f444e94726a5befca90e38aaf5",
"primary": true,
"username": "Notch"
}
],
"connections": [
{
"providerId": "discord",
"accountId": "123456789012345678"
}
]
}