【问题标题】:How to get the token from firebase_auth如何从 firebase_auth 获取令牌
【发布时间】:2019-10-05 16:03:56
【问题描述】:

我想从 firebase 获取身份验证令牌(电子邮件和密码身份验证)以在我的 firebase 云功能中进行身份验证。似乎 getIdToken() 和 getToken() 函数都不适用于 firebase_auth 包。

是否有其他功能,或者是否有更好的方法来确保只有经过身份验证的用户才能触发云功能?

var token = await FirebaseAuth.instance.currentUser.getIdToken();
var response = await httpClient.get(url,headers: {'Authorization':"Bearer $token"});

【问题讨论】:

    标签: flutter firebase-authentication google-cloud-functions


    【解决方案1】:

    我同意@Doug 在这一点上的看法——可调用为你包装了它,并且会更容易——但我的用例要求我进行 HTTPS 调用(函数中的onRequest)。另外,我认为您只是在正确的道路上 - 但您可能没有在您的 Cloud Functions 中检查它。

    在您的应用中,您将调用:

    _httpsCall() async {
      // Fetch the currentUser, and then get its id token 
      final user = await FirebaseAuth.instance.currentUser();
      final idToken = await user.getIdToken();
      final token = idToken.token;
    
      // Create authorization header
      final header = { "authorization": 'Bearer $token' };
    
      get("http://YOUR_PROJECT_BASE_URL/httpsFunction", headers: header)
      .then((response) {
        final status = response.statusCode;
        print('STATUS CODE: $status');
      })
      .catchError((e) {
        print(e);
      });
    }
    

    在您的函数中,您将检查令牌:

    export const httpsFunction = functions.https.onRequest((request, response) => {
      const authorization = request.header("authorization")
    
      if (authorization) {
        const idToken = authorization.split('Bearer ')[1]
        if (!idToken) {
          response.status(400).send({ response: "Unauthenticated request!" })
          return
        }
    
        return admin.auth().verifyIdToken(idToken)
        .then(decodedToken => {
          // You can check for your custom claims here as well
          response.status(200).send({ response: "Authenticated request!" })
        })
        .catch(err => {
          response.status(400).send({ response: "Unauthenticated request!" })
        })
      }
    
      response.status(400).send({ response: "Unauthenticated request!" })
    })
    

    记住:

    如果我没记错的话,这些令牌的有效期为 1 小时,如果您要将它们存储在某个地方,请注意这一点。我已经在本地进行了测试,我每次都需要大约 200~500 毫秒才能仅获取 id 令牌,这在大多数情况下并没有那么大的开销 - 但很重要。

    【讨论】:

      【解决方案2】:

      使用callable function 对您来说是最简单的,因为它可以让您:

      1. 在请求中自动发送当前用户的uid。
      2. 在功能方面很容易知道请求中是否提供了 UID,如果没有提供则拒绝服务。

      flutter 插件是here

      不过,您应该能够自己完成等效的工作,因为可调用函数只是普通 HTTP 连接的包装器。您可以获取已登录用户的 ID 令牌。

      【讨论】:

      • 虽然我同意你的观点,但我的团队选择将我们的 CF 封装在 Express 应用程序中,因此我们最终陷入了 onRequest 调用 - 我们不可避免地不得不自己操作令牌。
      【解决方案3】:
      import 'package:firebase_messaging/firebase_messaging.dart';
      .
      .
      .
      
      final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
      
      @override
      Future<void> initState() {
        super.initState();
      
        _firebaseMessaging.getToken().then((token) {
          assert(token != null);
          print("teken is: " + token);
        });
      }
      

      【讨论】:

        猜你喜欢
        • 2017-08-04
        • 1970-01-01
        • 2021-09-07
        • 2019-09-09
        • 2020-06-24
        • 2018-09-05
        • 2018-01-15
        • 2017-02-17
        • 2017-05-26
        相关资源
        最近更新 更多