【问题标题】:How to get firebase userID from inside a firebase cloud function如何从 Firebase 云功能中获取 Firebase 用户 ID
【发布时间】:2020-02-28 10:44:21
【问题描述】:

我的用户从我的应用程序被定向到一个网站,该网站最终返回对重定向 uri 的响应。我在 firebase 中有一个云功能,可以监听该链接并获得响应。现在我想将该信息与触发该过程的 Firebase 用户的详细信息一起保存?例如,我想将 Firestore 中的响应保存在文档名称中作为用户 ID。我怎样才能实现它??

我添加了我的流程图片

这是我的功能

exports.connectStripeStandardAccount = functions.https.onRequest((req, res) => {

  let authCode = req.query.code;
  //here I would like to get the uid of the user who triggered the website
  return res.send(authCode);

  });
});

这是第 nr 1 步的代码(我的应用是用 Flutter 编写的)

链接是这种格式 链接=https://connect.stripe.com/oauth/authorize?response_type=code&client_id=xxxxxxxxxxxx&scope=read_write

_launchURLWebsite(String link) async {
    if (await canLaunch(link)) {
      await launch(link);
    } else {
      throw 'Could not launch $link';
    }
  }

谢谢

【问题讨论】:

  • 您究竟是从哪里调用云函数的?从您的流程来看,它似乎来自网站。它是否正确?如果来自应用程序,是否通过 Firebase 身份验证识别用户?您使用哪种云功能?一个“简单”的 HTTPS 还是 Callable 的?
  • 该功能由网站触发。我已经添加了上面的功能供参考
  • “网站”到底是什么?我猜你可以完全控制它。不能和app合并吗?它通过Java Script 调用HTTPS Cloud Function?也许该网站是 Stripe API 端点(来自您的 CF 名称),它是一个调用您的 CF 的 Stripe webhook。我们需要更多详细信息才能全面了解您的情况。
  • 是的,网站是条纹的,我无法控制它
  • 好的!您是否还可以共享用户单击的按钮的代码(即如何调用 Stripe API)。它是什么类型的应用程序?安卓?iOS?网络?

标签: javascript firebase google-cloud-firestore google-cloud-functions stripe-payments


【解决方案1】:

我认为这应该适合你。通过使用此方法,您将能够从任何地方调用用户的uid

通过将提供程序包 (https://pub.dev/packages/provider#-installing-tab-) 添加到您的 pubspec.yaml 依赖项来安装它。

pubspec.yaml

dependencies:
  provider: ^3.1.0+1

接下来,创建一个名为 auth.dart 的新 dart 文件(你可以随意命名它。

在该文件中创建一个名为 Auth 的新类,如下所示:

import 'package:firebase_auth/firebase_auth.dart';

class Auth {

String userId;

final FirebaseAuth _auth = FirebaseAuth.instance;

String get userId {
    return _userId;
  }

void getUserID() async{
    FirebaseUser user = await _auth.currentUser();
    _userId = user.uid;
  }

}

然后在您的main.dart 文件中

通过添加此导入来导入 Provider 包:

import 'package:provider/provider.dart';

同样在 main.dart 文件中,在您的 MaterialApp 返回之前 (return MaterialApp...)

用这样的新小部件包装它:

Consumer<Auth>(
 builder: (ctx, auth, _) => MaterialApp(...),),

现在,在您想要调用用户 ID 的任何小部件中,您都可以执行此操作..

@override
  Widget build(BuildContext context) {

final auth = Provider.of<Auth>(context);

return Container(
 child: Text(auth.uid);
 );
}


【讨论】:

  • 嗨,是的,这是一个用 dart/flutter 编写的应用程序。你会怎么回答?
  • 嗨@Cris 感谢您的反馈。这是一个很好的解决方案,但仅限于客户端。我一直在寻找类似的东西,但要获得云功能。我意识到除了将此信息传递给我的 https 请求之外,没有其他方法可以获取 userId
【解决方案2】:

从不同的 cmets 到您的问题,您似乎正在使用 Stripe Connect OAuth Reference

文档(上面的链接)解释说,您可以在请求中添加state 参数,这是“我们将传递回给您的任意字符串值”。

因此,通过在您的 URL 中添加 state 参数,如下所示

https://connect.stripe.com/oauth/authorize?response_type=code&client_id=xxxxxxxxxxxx&scope=read_write&state=theuidofyouruser

您将在 Cloud Function 中收到 state 的值作为查询字符串参数,如下所述:https://stripe.com/docs/connect/oauth-reference#get-authorize-response

在云函数中,你可以通过req.query.state得到state的值,这点见云函数documentation

【讨论】:

    猜你喜欢
    • 2018-01-18
    • 2020-07-26
    • 2019-10-17
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    相关资源
    最近更新 更多