Sign in with WeChat
For: an app developer whose users sign in with WeChat on a desktop browser.
What it does: wechatEntry adds a “Continue with WeChat” button. The person scans a QR code, and core finds or makes the user by the WeChat identity, with no address.
Before you start
Section titled “Before you start”- A working social sign-in from Sign in with Google and Apple.
- A website app on the WeChat Open Platform with WeChat sign-in, for the WeChat entry. Set the domain of your API host as its callback domain.
- Copy the AppID and the AppSecret to your secret store.
- Add
wechatEntryfrom@authtempo/core/social-entriestosocial.oauth2.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";import { wechatEntry } from "@authtempo/core/social-entries";
export function createWechatAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string;}) { return createAuth({ baseURL: "https://api.example.com", appURL: "https://app.example.com", trustedOrigins: ["https://app.example.com"], secret: deps.secret, storage: deps.storage, mail: deps.mail, social: { // The AppID and the AppSecret of a website app on the WeChat Open Platform. oauth2: [wechatEntry({ appId: "wx-app-id", appSecret: "wechat-app-secret" })], }, });}- Link to
/auth/social/wechat/start?next=/homefrom the sign-in page.
import { describe, expect, it } from "vitest";import { call, harness } from "../support/harness.ts";import { createWechatAuth } from "./wechat.ts";
describe("sign in with WeChat", () => { it("sends the person to the WeChat QR page with the app id and the fragment", async () => { const auth = createWechatAuth(harness()); const res = await call(auth, "GET", "/auth/social/wechat/start?next=/home"); expect(res.status).toBe(302); const location = res.headers.get("location") ?? ""; expect(location.endsWith("#wechat_redirect")).toBe(true); const to = new URL(location); expect(to.origin + to.pathname).toBe("https://open.weixin.qq.com/connect/qrconnect"); expect(to.searchParams.get("appid")).toBe("wx-app-id"); expect(to.searchParams.get("scope")).toBe("snsapi_login"); // The WeChat docs name no PKCE, so the entry sends none. expect(to.searchParams.has("code_challenge")).toBe(false); });});How it behaves
Section titled “How it behaves”- The authorize URL takes
appidand ends with#wechat_redirect, as the WeChat docs give it. - The WeChat docs name no PKCE and no nonce, so the entry sets
pkce: false. The state and the binding cookie still tie the callback to the browser that started it. - The token call is a GET with the AppSecret and the code in the query. Core logs only the error class of a failure, never the URL.
- WeChat answers an error with status 200 and an
errcode. Each such answer isoauth_failed, and no user or session is made. - The subject is the
unionid, else theopenid, the key that Better Auth keeps. Aunionidnames one person across the apps of one Open Platform account. Anopenidnames the person in this app only. - A website app with no Open Platform account gets no
unionid. When the app gets an account later, a person who signed in byopenidgets a new subject. That person then gets a second user. Bind the app before you go live. - No WeChat answer holds an address. So the user has no address, no magic link, and no mail notice. Core never links a WeChat sign-in to another user by address.
- Under
signUp: "invited", a WeChat sign-in makes no user, because an invitation names an address.
If it fails
Section titled “If it fails”oauth_failed: the token call or the user info call failed or answered anerrcode. The two answers can also name other people. Check the AppID, the AppSecret, and the callback domain.oauth_denied: the person refused at WeChat.invalid_token: the state is spent or old, or the callback came to another browser. Start again.sign_up_closed: the sign-up rule refused a new user.email_required: a signed-in user tried to link a WeChat account.