【问题标题】:Firebase authentication with custom backend带有自定义后端的 Firebase 身份验证
【发布时间】:2019-05-17 06:56:00
【问题描述】:

我计划使用 Firebase 对我的 iOS 应用进行身份验证。但我想为其余的 REST API 使用自定义后端。如何在我的自定义后端为通过 Firebase 身份验证的用户添加授权?我们可以同时使用自定义后端和 firebase 身份验证吗?如何同时使用 Firebase 和自定义后端来维护会话?

【问题讨论】:

    标签: ios firebase authentication oauth-2.0 firebase-authentication


    【解决方案1】:

    您可以使用 firebase Admin SDK 在后端服务器上验证令牌。

    因此,在应用程序上,您会获得一个 firebase 访问令牌并将其发送到您的服务器。 在IOS上你这样做:

    FIRUser *currentUser = [FIRAuth auth].currentUser;
    [currentUser getIDTokenForcingRefresh:YES
                           completion:^(NSString *_Nullable idToken,
                                        NSError *_Nullable error) {
          if (error) {
            // Handle error
            return;
          }
    
          // Send token to your backend via HTTPS
          // ...
      }];
    

    更多信息在这里: https://firebase.google.com/docs/auth/admin/verify-id-tokens#retrieve_id_tokens_on_clients

    在你做的服务器上:

    admin.auth().verifyIdToken(idToken)
    .then(function(decodedToken) {
      var uid = decodedToken.uid;
      // ...
    }).catch(function(error) {
      // Handle error
    });
    

    如您所见,您甚至可以获得用户的“uid”。 更多信息在这里: https://firebase.google.com/docs/auth/admin/verify-id-tokens

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 2016-11-14
      相关资源
      最近更新 更多