簡體   English   中英

linkWithCredential 和 Flutter Web 與 Apple

[英]linkWithCredential and Flutter Web with Apple

我有一個用例,用戶在 Flutter Web 上需要將 Apple 授權與其現有帳戶相關聯,而 email 可能不匹配。

但是,Flutter Web Apple 身份驗證的唯一可用方法是signInWithPopUp 如果用戶的蘋果 email 與用戶 firebase 帳戶 email 不同,則會創建一個新的 firebase 帳戶,並返回一個用戶,從而縮短鏈接過程,這會在 firebase 中創建一個新帳戶,我無法linkWithCredential

我嘗試鏈接帳戶的方法如下:

  Future<String> linkWithAppleWeb() async {
    try {
      final User user = _auth.currentUser!;
      final provider = OAuthProvider("apple.com")
        ..addScope('email')
        ..addScope('name');
      await _auth.signInWithPopup(provider).then((appleCredential) async {
        final authCredential = appleCredential.credential;

        await user.linkWithCredential(authCredential!).then((result) {
          DatabaseService().updateUserSocialAuth(user.uid, 'apple');
          return 'Success';
        }).catchError((e) {
          return 'Error: $e';
        });
      });
    } catch (e) {
      return 'Error: $e';
    }
    return 'Success';
  }

如您所料,我的項目從 Streaming a User Object 開始, 當彈出窗口登錄時,它返回新用戶,從而重建整個項目。 有沒有辦法在不返回新用戶的情況下對蘋果用戶進行身份驗證? 我可以很好地鏈接谷歌或電話授權方法。 有問題的是蘋果。 我不完全理解為什么谷歌沒有以同樣的方式中斷,除了 Firebase 投入工作以確保GoogleSignIn().signIn()的鏈接功能我沒有使用其他社交身份驗證方法,我不要使用密碼/電子郵件。

此方法未記錄在Flutter Fire Docs中,但效果很好:

  Future<String> linkWithAppleWeb() async {
    try {
      final User user = _auth.currentUser!;
      final provider = OAuthProvider("apple.com")
        ..addScope('email')
        ..addScope('name');
      await user.linkWithPopup(provider).then((result) {
        DatabaseService().updateUserSocialAuth(user.uid, 'apple');
        return 'Success';
      }).catchError((e) {
        return 'Error: $e';
      });
    } catch (e) {
      debugPrint('auth linkWithGoogle error: ${e.toString()}');
      return 'Error: $e';
    }
    return 'Success';
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM