跳转至

添加匿名用户

概述

您可以使用「匿名用户」功能,为应用中的访客创建匿名用户账号,让他们能够像普通用户一样进行操作。例如,在社交平台中,访客可以在设置邮箱和密码之前就发表评论、保存偏好设置。即使应用被关闭,用户会话仍会保留。

这提升了应用体验,因为用户无需在用户旅程的早期就设置验证方式,同时仍可享受大部分应用功能。对应用开发者而言,创建和分配匿名用户的能力也让关联用户注册前后活动变得更容易。

说明

注意:匿名用户功能仅适用于以下类型的 Authany 客户端应用:原生应用、单页应用和传统 Web 应用。

OIDC/SAML 客户端应用类型不支持匿名用户。因此,如果您的应用使用此客户端应用类型,尝试创建匿名用户时会出现错误。

为您的项目启用匿名用户

  1. 在管理台中,进入身份验证 > 匿名用户。
  2. 打开启用匿名用户。
  3. 保存设置。

使用 SDK

以匿名用户身份注册

这会为该会话创建一个匿名用户。同一会话中终端用户后续的请求可以通过相同的 sub 来识别

authgear
    .authenticateAnonymously()
    .then(({userInfo}) => {
        // Logged in as anonymous user successfully
    })
    .catch((err) => {
        // Handle the error
    });
try {
    final userInfo = await authgear.authenticateAnonymously();
    // Logged in as anonymous user successfully
} catch (e) {
    // Handle the error
}
authgear.authenticateAnonymously { result in
    switch result {
    case let .success(userInfo):
        // Logged in as anonymous user successfully
    case let .failure(error):
        // Handle the error
    }
}
mAuthgear.authenticateAnonymously(new OnAuthenticateAnonymouslyListener() {
    @Override
    public void onAuthenticated(@NonNull UserInfo userInfo) {
        // Logged in as anonymous user successfully
    }

    @Override
    public void onAuthenticationFailed(@NonNull Throwable throwable) {
        // Handle the error
    }
});
authgear
    .authenticateAnonymously()
    .then(({userInfo}) => {
        // Logged in as anonymous user successfully
    })
    .catch((err) => {
        // Handle the error
    });

检查 UserInfo 对象

以匿名用户「注册」后,您可以获取「UserInfo」对象,查看终端用户的 sub。

UserInfo

{
  "sub": "...",
  "isVerified": false,
  "isAnonymous": true
}

匿名用户的升级

可以调用 promoteAnonymousUser 函数将匿名用户升级为正式用户。您需要用登录 ID(例如邮箱、手机号)和验证方式(例如密码)来调用该函数。终端用户会看到一个注册页面来完成升级。升级后终端用户的 sub 保持不变。

authgear
    .promoteAnonymousUser({
        redirectURI: THE_REDIRECT_URI,
    })
    .then(({userInfo}) => {
        // Promote anonymous user successfully
    })
    .catch((e) => {
        // Handle the error
    });
try {
    final userInfo = await authgear.promoteAnonymousUser(redirectURI: THE_REDIRECT_URI);
    // Promote anonymous user successfully
} catch (e) {
    // Handle the error
}
authgear.promoteAnonymousUser(
    redirectURI: THE_REDIRECT_URI
) { result in
    switch result {
    case let .success(userInfo):
        // Promote anonymous user successfully
    case let .failure(error):
        // Handle the error
    }
}
PromoteOptions options = new PromoteOptions(THE_REDIRECT_URI);
authgear.promoteAnonymousUser(options, new OnPromoteAnonymousUserListener() {
    @Override
    public void onPromoted(@NonNull UserInfo userInfo) {
        // Promote anonymous user successfully
    }
    @Override
    public void onPromotionFailed(@NonNull Throwable throwable) {
        // Handle the error
    }
});

第一步:启动升级流程

当用户在您的网站上点击升级时,发起一个开始升级调用,将其重定向到升级页面。

authgear
    .startPromoteAnonymousUser({
        // Configure redirectURI which users will be redirected to
        // after they have promoted with Authgear.
        // You can use any path in your website.
        // Make sure it is in the "Redirect URIs" list of the Application.
        // The redirect uri for anonymous user promotion should be
        // different from the one for normal user authentication.
        // e.g. "https://yourdomain.com/promote-redirect"
        redirectURI: THE_REDIRECT_URI,
    })
    .then(({userInfo}) => {
        // Started the promotion flow
    })
    .catch((err) => {
        // Failed to start the promotion flow
    });

第二步:处理升级结果

用户在升级页面完成升级后,会被重定向到 redirectURL,URL 查询参数中带有 code。在您应用的 redirectURI 中,发起一个完成升级调用来处理升级结果。

authgear
    .finishPromoteAnonymousUser()
    .then(({userInfo}) => {
        // Promoted successfully
        // You should redirect the user to another path
    })
    .catch((err) => {
        // Failed to finish promotion
    });
);

用户存续时间

移动应用

在移动端 SDK 上,创建匿名用户时会生成一对密钥。该密钥对存储在移动设备的原生加密存储中。终端用户始终可以用该密钥对重新登录到同一个匿名用户。当加密存储被移除后,该匿名用户将无法再访问。

Web 应用和网站

在 Web SDK 上,不会生成密钥对。因此,一旦会话失效,终端用户将无法再登录到同一个匿名用户。对于基于 Cookie 的身份验证,这由 Cookie 的「空闲超时」和「会话存续时间」控制。对于基于令牌的身份验证,这由刷新令牌的「空闲超时」和「令牌存续时间」控制。

换句话说,匿名用户账号的存续时间与已登录会话的存续时间相同。

要调整存续时间设置,请在管理台 > 应用中相应地修改超时时间和存续时间。

高流量网站的注意事项

您应仅在用户旅程中确有必要时才创建匿名用户,以避免在租户中产生过多孤立账号。