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.
Before you start
Section titled “Before you start”- A working social sign-in from Sign in with Google and Apple.
- A TikTok developer app with web sign-in and the scope
user.info.basic, for the TikTok entry.
- Register
https://<your api host>/auth/social/tiktok/callbackas the redirect URI of the TikTok app. - Copy the client key and the client secret of the TikTok app to your secret store.
- Add
tiktokEntryfrom@authtempo/core/social-entriestosocial.oauth2.
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" }), ], }, });}- Link to
/auth/social/tiktok/start?next=/homefrom the sign-in page.
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"); });});How it behaves
Section titled “How it behaves”- TikTok names the client id
client_key. The entry sends it in the authorize query and in the token form, through atokenRequesthook. - 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 Authaccountrow of the providertiktoklinks 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.
If it fails
Section titled “If it fails”oauth_failed: the token call or the user info call failed, answered an error, or named anotheropen_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.