BETTER-AUTH.

WeChat

WeChat provider setup and usage.

Get your WeChat Credentials

To use WeChat sign in, you need to register a website application on the WeChat Open Platform, set the Authorization Callback Domain to the better auth domain and get your App ID and App Secret.

Configure the provider

To configure the provider, you need to import the provider and pass it to the socialProviders option of the auth instance.

auth.ts
import { betterAuth } from "better-auth"

export const auth = betterAuth({
    socialProviders: {
        wechat: { 
            clientId: process.env.WECHAT_CLIENT_ID, 
            clientSecret: process.env.WECHAT_CLIENT_SECRET, 
        }, 
    },
})

Optional Configuration

You can customize the WeChat provider with additional options:

auth.ts
export const auth = betterAuth({
    socialProviders: {
        wechat: {
            clientId: process.env.WECHAT_CLIENT_ID,
            clientSecret: process.env.WECHAT_CLIENT_SECRET,
            // Optional: Set UI language for the WeChat login page
            lang: "cn", // or "en" for English
            // Optional: Use custom scopes
            scope: [], // "snsapi_login" for web QR code login.
        },
    },
})

Sign In with WeChat

To sign in with WeChat, 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 wechat.
auth-client.ts
import { createAuthClient } from "better-auth/client"
const authClient = createAuthClient()

const signIn = async () => {
    const data = await authClient.signIn.social({
        provider: "wechat"
    })
}

Usage

Platform Support

WeChat provider currently supports the Website Application platform type, which enables WeChat QR code login for web applications.

Development Notes

  • The redirect URL domain must match the domain configured in the WeChat Open Platform