Skip to content

Sign in with TikTok

For: an app developer whose users have TikTok accounts.

What it does: tiktokEntry adds a “Continue with TikTok” button. Core finds or makes the user by the TikTok open_id, and the user has no address.

  1. Register https://<your api host>/auth/social/tiktok/callback as the redirect URI of the TikTok app.
  2. Copy the client key and the client secret of the TikTok app to your secret store.
  3. Add tiktokEntry from @authtempo/core/social-entries to social.oauth2.
The TikTok entry in the config
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
import { tiktokEntry } from "@authtempo/core/social-entries";
export function createTiktokAuth(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: {
// TikTok calls the client id the client key. Read both from your secret store.
oauth2: [
tiktokEntry({ clientKey: "tiktok-client-key", clientSecret: "tiktok-client-secret" }),
],
},
});
}
  1. Link to /auth/social/tiktok/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 { createTiktokAuth } from "./tiktok.ts";
describe("sign in with TikTok", () => {
it("sends the person to TikTok with the client key", async () => {
const auth = createTiktokAuth(harness());
const res = await call(auth, "GET", "/auth/social/tiktok/start?next=/home");
expect(res.status).toBe(302);
const to = new URL(res.headers.get("location") ?? "");
expect(to.origin + to.pathname).toBe("https://www.tiktok.com/v2/auth/authorize/");
expect(to.searchParams.get("client_key")).toBe("tiktok-client-key");
expect(to.searchParams.get("scope")).toBe("user.info.basic");
});
});
  • TikTok names the client id client_key. The entry sends it in the authorize query and in the token form, through a tokenRequest hook.
  • Core spends the state and checks the binding cookie before the hook runs. The hook’s fetch calls only the TikTok token URL.
  • The subject is open_id, the key that Better Auth keeps. A Better Auth account row of the provider tiktok links with no change.
  • The TikTok user object has no address. So the user has no address, no magic link, and no mail notice. Offer a passkey or a second method in the account settings.
  • Core never links a TikTok sign-in to another user by address. The settings link of a TikTok account answers email_required, because the link needs a proved address.
  • The TikTok page of the web sign-in names no PKCE. The entry sends a PKCE challenge. Do one real sign-in before you go live, to see that TikTok accepts it.
  • Under signUp: "invited", a TikTok sign-in makes no user, because an invitation names an address.
  • oauth_failed: the token call or the user info call failed, answered an error, or named another open_id. Check the client key, the secret, and the redirect URI.
  • oauth_denied: the person refused at TikTok.
  • 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 TikTok account.