revenuecat-identify-user

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

revenuecat-identify-user: connect RevenueCat to your auth system

revenuecat-identify-user:将RevenueCat与您的认证系统关联

Use this skill when the user wants to call
logIn
/
logOut
on the RevenueCat SDK so that their app users line up with RevenueCat subscribers. This skill does not cover initial SDK setup (see
integrate-revenuecat
), purchases (
revenuecat-purchase-flow
), or gating (
revenuecat-entitlements-gate
).
当用户希望在RevenueCat SDK上调用
logIn
/
logOut
,使应用用户与RevenueCat订阅者保持一致时,可使用此技能。本技能不涵盖SDK初始设置(详见
integrate-revenuecat
)、购买流程(
revenuecat-purchase-flow
)或权限管控(
revenuecat-entitlements-gate
)。

1. Detect the platform

1. 检测平台

Inspect the working directory and pick the first match, from top to bottom:
  1. React Native:
    package.json
    has a
    react-native-purchases
    entry, or
    react-native
    as a dependency → read
    platforms/react-native.md
    . If
    expo
    is also a dependency, note it as an Expo project.
  2. Flutter:
    pubspec.yaml
    exists at the project root → read
    platforms/flutter.md
    .
  3. Kotlin Multiplatform:
    build.gradle.kts
    contains a
    kotlin { … }
    multiplatform source sets block, or depends on
    com.revenuecat.purchases:purchases-kmp*
    → read
    platforms/kmp.md
    .
  4. Android (native):
    build.gradle(.kts)
    applies
    com.android.application
    (and is not KMP) → read
    platforms/android.md
    .
  5. iOS (native):
    Package.swift
    ,
    *.xcodeproj
    ,
    *.xcworkspace
    , or
    Podfile
    at the project root → read
    platforms/ios.md
    .
If several match (e.g. an
ios/
folder inside a Flutter project), pick the outermost project, the one that owns the build. If still ambiguous, ask the user which platform they want to configure.
检查工作目录,从上到下选择第一个匹配项:
  1. React Native
    package.json
    中包含
    react-native-purchases
    条目,或依赖
    react-native
    → 阅读
    platforms/react-native.md
    。如果同时依赖
    expo
    ,则标记为Expo项目。
  2. Flutter:项目根目录存在
    pubspec.yaml
    → 阅读
    platforms/flutter.md
  3. Kotlin Multiplatform
    build.gradle.kts
    包含
    kotlin { … }
    多平台源码集块,或依赖
    com.revenuecat.purchases:purchases-kmp*
    → 阅读
    platforms/kmp.md
  4. Android(原生)
    build.gradle(.kts)
    应用了
    com.android.application
    (且非KMP项目)→ 阅读
    platforms/android.md
  5. iOS(原生):项目根目录存在
    Package.swift
    *.xcodeproj
    *.xcworkspace
    Podfile
    → 阅读
    platforms/ios.md
如果有多个匹配项(例如Flutter项目中包含
ios/
文件夹),选择最外层的项目,即拥有构建权限的项目。若仍存在歧义,请询问用户想要配置哪个平台。

2. Shared concepts (all platforms)

2. 通用概念(全平台)

  • Anonymous by default. Before
    logIn
    is called, RevenueCat assigns a stable anonymous ID prefixed
    $RCAnonymousID:
    . Purchases made while anonymous are aliased onto the real
    appUserID
    the first time
    logIn
    is called with it, so there is no "lost purchase" risk from letting users buy before signing in.
  • Never use email, phone number, or a sequential database id as the appUserID. Use a stable opaque value such as your backend's user UUID, or a hash of the user id. RevenueCat treats the ID as an opaque string and it is difficult to change later.
  • Call
    logIn
    after your auth system confirms the session.
    Do not call
    logIn
    speculatively. The typical trigger is your auth state listener firing with a signed in user.
    logIn
    returns both the user's current
    CustomerInfo
    and a
    created: Boolean
    that tells you whether this is a brand new RevenueCat customer.
  • logOut
    only works on identified users.
    Calling
    logOut
    while the SDK is on an anonymous ID throws an error in every SDK (
    PurchasesErrorCode.LogOutWithAnonymousUserError
    or the iOS equivalent). Gate it behind your own "is signed in" flag.
  • Restore is not login.
    restorePurchases()
    asks the store for the current receipt and attaches it to the current RevenueCat user. It does not switch identities. If the user signs in on a new device, call
    logIn(appUserID)
    first, then
    restorePurchases()
    only if they also expect to pull a receipt from the current store account.
  • Account switching is
    logOut
    then
    logIn
    .
    If your app lets a user sign out and sign back in as someone else, call
    logOut()
    first, wait for it, then
    logIn(newId)
    . Do not try to swap directly with a second
    logIn
    , since that will alias the two IDs together.
  • Configure first.
    Purchases.configure(…)
    must have run before
    logIn
    /
    logOut
    . If it has not, the SDK throws.
  • 默认匿名状态。 在调用
    logIn
    之前,RevenueCat会分配一个以
    $RCAnonymousID:
    为前缀的稳定匿名ID。匿名状态下进行的购买,会在首次使用真实
    appUserID
    调用
    logIn
    时关联到该ID上,因此允许用户在登录前购买不会有“购买丢失”的风险。
  • 切勿使用邮箱、电话号码或连续数据库ID作为appUserID。 使用稳定的不透明值,例如后端的用户UUID或用户ID的哈希值。RevenueCat将该ID视为不透明字符串,后续难以修改。
  • 在认证系统确认会话后调用
    logIn
    请勿随意调用
    logIn
    。典型触发时机是认证状态监听器检测到用户已登录。
    logIn
    会返回用户当前的
    CustomerInfo
    以及一个
    created: Boolean
    值,用于告知您该用户是否是全新的RevenueCat客户。
  • logOut
    仅对已识别用户生效。
    当SDK处于匿名ID状态时调用
    logOut
    ,所有SDK都会抛出错误(
    PurchasesErrorCode.LogOutWithAnonymousUserError
    或iOS端等效错误)。请在您自己的“已登录”标记为真时才允许调用该方法。
  • 恢复购买不等于登录。
    restorePurchases()
    会向应用商店请求当前收据,并将其附加到当前RevenueCat用户上。它不会切换身份。如果用户在新设备上登录,请先调用
    logIn(appUserID)
    ,仅当用户需要从当前商店账户获取收据时,再调用
    restorePurchases()
  • 账户切换需先
    logOut
    logIn
    如果您的应用允许用户退出并以其他身份重新登录,请先调用
    logOut()
    ,等待完成后再调用
    logIn(newId)
    。请勿尝试直接通过第二次调用
    logIn
    来切换,因为这会将两个ID关联在一起。
  • 先配置再调用。
    Purchases.configure(…)
    必须在
    logIn
    /
    logOut
    之前执行。如果未执行,SDK会抛出错误。

3. Implementation

3. 实现步骤

Read the platform file that matches detection:
  • platforms/ios.md
  • platforms/android.md
  • platforms/kmp.md
  • platforms/flutter.md
  • platforms/react-native.md
Each platform file shows the
logIn
and
logOut
calls wired into a typical auth state observer.
阅读与检测结果匹配的平台文件:
  • platforms/ios.md
  • platforms/android.md
  • platforms/kmp.md
  • platforms/flutter.md
  • platforms/react-native.md
每个平台文件都展示了如何将
logIn
logOut
调用接入典型的认证状态监听器。

4. Verify

4. 验证步骤

Do not claim identity sync works until:
  1. In the RevenueCat dashboard, the Customer page for your test user shows the same appUserID your backend uses, not the
    $RCAnonymousID:
    placeholder.
  2. Signing out clears the ID back to a fresh anonymous user; signing in as a different account switches to that account's purchases (or shows none if it is a new account).
  3. A purchase made while anonymous, followed by
    logIn
    , remains attached to the signed in user (aliased, not lost).
  4. Calling
    logOut
    while already anonymous is handled, not treated as a crash or a silent success.
在满足以下条件之前,请勿宣称身份同步功能正常:
  1. 在RevenueCat控制台中,测试用户的客户页面显示的appUserID与您后端使用的完全一致,而非
    $RCAnonymousID:
    占位符。
  2. 退出登录后,ID会重置为新的匿名用户;以其他账户登录时,会切换到该账户的购买记录(如果是新账户则显示无记录)。
  3. 匿名状态下进行的购买,在调用
    logIn
    后仍会关联到已登录用户(已关联,未丢失)。
  4. 在已匿名状态下调用
    logOut
    时能被正确处理,不会导致崩溃或静默成功。