Skip to content

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.

  1. Copy the AppID and the AppSecret to your secret store.
  2. Add wechatEntry from @authtempo/core/social-entries to social.oauth2.
The WeChat entry in the config
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" })],
},
});
}
  1. Link to /auth/social/wechat/start?next=/home from the sign-in page.
The start as a test
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);
});
});
  • The authorize URL takes appid and 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 is oauth_failed, and no user or session is made.
  • The subject is the unionid, else the openid, the key that Better Auth keeps. A unionid names one person across the apps of one Open Platform account. An openid names 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 by openid gets 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.
  • oauth_failed: the token call or the user info call failed or answered an errcode. 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.