客户与会话
创建与更新 Customer,签发 Customer Token,处理访客与登录后认领。
API v1
创建或查找 Customer#
用你系统里已验证的用户 ID 作为 externalUserId。同一个 ID 再次调用会返回同一个 Customer(created: false),已有资料不会被覆盖。
1import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server";2 3const longplay = createLongplayServer({4 baseUrl: process.env.LONGPLAY_API_URL!,5 apiKey: process.env.LONGPLAY_SERVER_KEY!,6 scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },7});8 9async function main() {10 const { customer, created } = await longplay.getOrCreateCustomer({11 externalUserId: "user-42",12 });13 console.log(customer.id, created);14}15 16main().catch((error) => {17 console.error(error);18 process.exitCode = 1;19});
更新 Customer#
只发送要修改的字段。每次更新生成一个 requestId 并保存;重试时使用同一个值,不会重复修改。
1import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server";2 3const longplay = createLongplayServer({4 baseUrl: process.env.LONGPLAY_API_URL!,5 apiKey: process.env.LONGPLAY_SERVER_KEY!,6 scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },7});8 9export async function syncProfile(input: {10 verifiedUserId: string;11 requestId: string; // persisted before the first attempt12 plan: string;13}) {14 const { customer, changed } = await longplay.updateCustomer({15 lookup: { type: "externalUserId", value: input.verifiedUserId },16 requestId: input.requestId,17 patch: {18 attributes: { plan: input.plan },19 tagChanges: { add: ["beta"], remove: [] },20 },21 });22 return { customerId: customer.id, changed };23}
让浏览器直接调用#
- 后端验证用户登录后,调用
createCustomerSession,把完整会话凭证对返回给该用户。 - 浏览器调用
identify(session),SDK 使用 Access Token 请求业务接口,并使用 Refresh Token 自动换证。 - 退出或切换账号时,浏览器调用
reset(),后端调用 revokeCustomerSessions。
server/session.tsTypeScript 1import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server";2 3const longplay = createLongplayServer({4 baseUrl: process.env.LONGPLAY_API_URL!,5 apiKey: process.env.LONGPLAY_SERVER_KEY!,6 scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },7});8 9// Call only after your own login check succeeds.10export async function issueCustomerSession(verifiedUserId: string) {11 const { customer } = await longplay.getOrCreateCustomer({12 externalUserId: verifiedUserId,13 });14 return longplay.createCustomerSession(customer.id);15}
- 在 Console 的 接入 → 网站来源 中登记你的网站地址。
- 签发 Token 的 Key 需要
sessions:create。Token 只会带上该 Key 中的这些权限:entitlements:read、credits:read、billing:read、billing:write、offers:resolve、funnels:run、features:read、events:write、content:read。 - SDK 在 Access Token 即将到期时合并并发刷新,不会重放已发送的业务写请求。以
expiresAt 和 refreshExpiresAt 为准;两者受原会话和签发 Key 的有效期约束。刷新被明确拒绝后,重新验证用户登录再签发。
支持未登录用户#
为访客生成 guestToken 并保存在 HttpOnly cookie 中,通过 resumeGuestSession 换取完整会话凭证对,传给浏览器的 identify(session)。用户登录后调用 claimGuestCustomer 绑定到其账号,再签发普通 Customer 会话。
1import { createGuestToken, createLongplayServer } from "@tinkerlandai/longplay-sdk/server";2 3const longplay = createLongplayServer({4 baseUrl: process.env.LONGPLAY_API_URL!,5 apiKey: process.env.LONGPLAY_SERVER_KEY!,6 scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },7});8 9// 1. First visit: create the guest token once and keep it in an HttpOnly cookie.10export async function startGuest(savedGuestToken?: string) {11 const guestToken = savedGuestToken ?? createGuestToken();12 await longplay.createGuestCustomer({ guestToken });13 const session = await longplay.resumeGuestSession({ guestToken });14 return { guestToken, session };15}16 17// 2. After your own login succeeds: bind the guest to the verified account.18export async function claimGuest(input: {19 guestToken: string;20 verifiedUserId: string;21 requestId: string;22}) {23 const { customer, switched } = await longplay.claimGuestCustomer({24 guestToken: input.guestToken,25 externalUserId: input.verifiedUserId,26 requestId: input.requestId,27 });28 const session = await longplay.createCustomerSession(customer.id);29 return { session, switched };30}
- 访客 Token 可以读取独立内容、运行 Funnel、读取 Feature 和上报事件:它只带签发 Key 中的
funnels:run、features:read、events:write、content:read。检查权益、购买和兑换优惠需要用户先登录。 - 签发访客 Token 的 Key 需要
sessions:create,以及上面四个权限中的至少一个。 - 访客会话以返回的 expiresAt 为准,且不晚于 guestToken 的到期时间(
guestExpiresAt)。