将社交提供程序与两个设备上的匿名用户链接

问题描述 投票:0回答:1

[当用户开始使用我们的应用程序时,我们使用Firebase匿名登录名登录。稍后,我们允许他们使用Apple等社交服务提供商登录。我们使用“ auth()。currentUser?.linkWithCredential”将社交凭据与匿名用户ID关联起来。

我们遇到了不确定的解决方法:

  1. 用户将应用程序安装在设备上,并使用“使用Apple登录”进行登录。我们将匿名帐户链接到Apple登录,一切正常。

  2. 但是现在用户购买了新设备。他安装了该应用程序并启动它。他得到了一个新的匿名uid。然后,他尝试使用Apple登录。现在,如果我们尝试调用linkWithCredential,则会得到一个错误:“”身份验证/凭据已在使用中]此凭据已与其他用户帐户关联“

这当然是正确的,因为Apple凭据与旧设备上的匿名用户相关联。那么,我们如何允许用户从新设备再次登录?我们认为要捕获该错误,然后调用signInWithCredential而不是linkWithCredential。但是然后我们得到一个错误:收到重复的凭证。请使用新的凭据重试。

似乎您不能使用Apple凭据进行多个通话。

同样,我们无法让用户登录两个设备。

let appleAuthRequestResponse = null;
try {
  appleAuthRequestResponse = await appleAuth.performRequest({
    requestedOperation: AppleAuthRequestOperation.LOGIN,
    requestedScopes: [
      AppleAuthRequestScope.EMAIL,
      AppleAuthRequestScope.FULL_NAME,
    ],
  });
} catch (err) {
  // TODO: decide what to do
  return;
}

const {
  identityToken,
  nonce
} = appleAuthRequestResponse;
const appleCredential = auth.AppleAuthProvider.credential(
  identityToken,
  nonce
);

let userCredentials = null;

try {
  userCredentials = await auth().currentUser ? .linkWithCredential(
    appleCredential
  );
  // This will work on the first device but fail on the second one
  console.log(userCredentials);
} catch (err) {
  // This will fail as well with error: Duplicate credential received
  await auth().signInWithCredential(appleCredential)
}
firebase react-native firebase-authentication react-native-firebase
1个回答
0
投票

如果对linkWithCredential的调用失败,则您的应用应回退到登录用户后,再使用其首选的身份验证提供程序直接登录,就好像根本没有匿名帐户一样。这将有效地将用户连接到他们现有的凭据。这将放弃以前创建的任何现有匿名帐户。

您可能会选择完全提示用户登录,然后再创建一个匿名帐户。

© www.soinside.com 2019 - 2024. All rights reserved.