dart 如何使用Flutter创建具有自定义UID的FireBase新用户

bfhwhh0e  于 2024-01-04  发布在  Flutter
关注(0)|答案(3)|浏览(170)

我一直在尝试在flutter中使用自定义UID在firebase中创建一个新用户,但似乎只能使用FireBaseAdmin包来完成,而且在flutter中还没有实现。有没有可能的方法可以使用自定义UID创建一个新用户?
我没有原生开发的背景,所以目前我不可能用原生java或Kotlin实现admin方法。有人知道如何实现吗?

jucafojl

jucafojl1#

用户的UID由创建用户的身份验证提供程序确定。当调用客户端Firebase SDK中的现有提供程序(如FlutterFire库)时,无法指定自己的UID。
如果为用户指定自己的UID是一个硬性要求,您可以implement a custom provider。这 * 确实 * 涉及到将代码写入create a custom ID token,该代码在可信环境(例如您的开发机器,您控制的服务器或Cloud Functions)中运行。
或者,您也可以考虑存储从Firebase提供的UID到您想要用来引用同一用户的ID的Map。这是一个经常用于给予用户唯一名称的场景,所以我建议检查questions related to unique user names

nwsw7zdq

nwsw7zdq2#

我是这样做的。我使用firebase云函数和pub.dev的普通firebase auth包。
在应用程序中,在登录时,我通过http触发器firebase函数和http包(也可以在pub.dev上获得)将用户的电子邮件地址和uid提交给服务器。

  1. Future<String?> createAccountWithSpecificUserID(
  2. String? email,
  3. String? userName,
  4. String? password,
  5. String uid,
  6. String? phone,
  7. ) async {
  8. final response = await http.post(
  9. Uri.parse(
  10. '${cloudFunctionStart}createFirebaseUser',
  11. ),
  12. headers: {'Content-Type': 'application/json'},
  13. body: jsonEncode(
  14. {
  15. if (email != null) 'email': email,
  16. if (userName != null) "username": userName,
  17. if (password != null) 'password': password,
  18. 'uid': uid,
  19. if (phone != null) "phone": phone,
  20. },
  21. ),
  22. );
  23. if (response.statusCode == 200) {
  24. return null;
  25. } else {
  26. return response.body;
  27. }
  28. }

字符串
这个函数将数据发送到云函数,云函数的代码如下所示(它是 typescript 。请根据需要自由使用):

  1. export const createFirebaseUser = functions.https.onRequest(async (req, res) => {
  2. corsMiddleware(req, res, async () => {
  3. try {
  4. const email = req.body.email;
  5. const password = req.body.password;
  6. const name = req.body.name;
  7. const uid = req.body.uid;
  8. const userRecord = await admin.auth().createUser({
  9. email: email,
  10. displayName: name,
  11. uid: uid,
  12. emailVerified: false,
  13. password: password,
  14. disabled: false,
  15. });
  16. res.status(200).send(`User created with email: ${userRecord.email}`);
  17. } catch (error) {
  18. res.status(500).send(`Error creating user: ${error}`);
  19. logToConsole("Error creating user", error!.toString(), error);
  20. }
  21. });
  22. });


这个函数返回一个可以为空的字符串。如果字符串为空,那么我们很好。登录。如果不是,那么有一个错误,字符串是错误消息。下面是一个代码示例

  1. setState(() {
  2. processing = true;
  3. nameController = TextEditingController(
  4. text: accts[0].name,
  5. );
  6. processingText = translation(context)!.creatingAccount;
  7. });
  8. if (nameController.text.trim().isEmpty) {
  9. nameController = TextEditingController(
  10. text: accts[0].name,
  11. );
  12. }
  13. String? cc = await createAccountWithSpecificUserID(
  14. emailController.text.trim().toLowerCase(),
  15. nameController.text.trim(),
  16. passwordController.text.trim(),
  17. accts[0].id,
  18. null,
  19. );
  20. if (cc == null) {
  21. signIn();
  22. } else {
  23. setState(() {
  24. processing = false;
  25. });
  26. showDialog(
  27. context: context,
  28. builder: (context) {
  29. return CustomDialogBox(
  30. bodyText:
  31. "$cc.\n\n${translation(context)!.ifYouNeedHelp}",
  32. buttonText: translation(context)!.pressHereToCallUs,
  33. onButtonTap: () {
  34. context.pushNamed(
  35. RouteConstants.contactUs,
  36. );
  37. },
  38. showButton: true,
  39. );
  40. },
  41. );
  42. }


我希望这能帮上忙。哦,还有,那个translation(context). blah blah的东西只是我翻译的字符串。我不得不在代码中添加本地化,因为这个应用程序是由说不同语言的人使用的。

展开查看全部
lokaqttq

lokaqttq3#

您可以使用FirebaseAuthentication。
当一个用户创建一个帐户时,它将创建一个具有随机UID的新用户,该UID将是唯一的。
链接:https://firebase.flutter.dev/docs/auth/usage/
它非常容易用途:

相关问题