繁体   English   中英

如何从 firebase 云 function 内部获取 firebase 用户 ID

[英]How to get firebase userID from inside a firebase cloud function

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

我添加了我的流程的图片在此处输入图像描述

这是我的 function

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 的代码(我的应用程序是用颤振编写的)

链接采用这种格式链接 = 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';
    }
  }

谢谢

从对您的问题的不同评论来看,您似乎正在使用 Stripe Connect OAuth Reference

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

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

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

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

在 Cloud Function 中,您可以通过执行req.query.state来获取state的值,有关这一点,请参阅 Cloud Functions 文档

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

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

pubspec.yaml

dependencies:
  provider: ^3.1.0+1

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

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

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文件中

通过添加此导入来导入提供程序 package:

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);
 );
}


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM