如何获得Firebase UID认识的电子邮件用户? [英] How to get Firebase UID knowing email user?

查看:59
本文介绍了如何获得Firebase UID认识的电子邮件用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Flutter应用程序,管理员个人资料可以在其中创建用户以访问其公司.该代码可以正常工作,除非先前为另一家公司创建了新用户.在这种情况下,FIREBASE AUTH中将出现类型为ERROR_EMAIL_ALREADY_IN_USE的错误.我要做的只是从FIREBASE AUTH中检索分配的UID,这对于将数据库中的用户分配给其他公司是必要的.

I am building a Flutter app where the administrator profile can create users to access their company. The code works right, unless the new user was previously created for another company. In this case an error of type ERROR_EMAIL_ALREADY_IN_USE appears from FIREBASE AUTH. What I want to do is simply retrieve the assigned UID from FIREBASE AUTH, which is necessary to assign the user within my database to an additional company.

这是我的代码...

 _register(LoginBloc bloc, BuildContext context) async{

    final usuarioBloc = Provider.usuarioBloc(context);
    if (!formKey.currentState.validate() ) return;

    final info = await usuarioProvider.crearUsuarioFirebase(bloc.email, bloc.password, true);

    if (info['ok']) {
      final keyUserId = info['localId'];
      usuarioProvider.crearUsuarioRaiz(keyUserId, _prefs.idEmpresa, bloc.email);

      usuario.idUsuario = info['localId'];
      usuario.correo    = bloc.email;

      usuarioBloc.crearUsuarioEmpresa(usuario, usuario.idUsuario, usuario.idEmpresa); //to create user in the Company
      print('******* User was Created *************');

    } else { //info['ok'] is false
      switch (info['mensaje'].code) {
        case 'ERROR_EMAIL_ALREADY_IN_USE':
          usuario.correo = bloc.email;
          // usuario.idUsuario = ????????
          // Here I would like to retrieve the UID to assign it to their additional Company
          usuarioBloc.crearUsuarioEmpresa(usuario, usuario.idUsuario, usuario.idEmpresa); //to create user in the Company
          print('*** User already in use, the user can use his/her usual password ***');
          break;
        default:
          print(info['mensaje'].message); //If it was a different error
      }
    }
  }

在提供者中,我有...

In Provider, I have...

  Future <Map<String, dynamic>> crearUsuarioFirebase(String email, String password, [bool desdeAdmin = false]) async {
  try {
    AuthResult result =  await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
    FirebaseUser user = result.user;
    return {'ok' : true, 'localId':user.uid, 'email' : user.email};
  } catch (e) {
    print(e);
    return {'ok': false, 'mensaje': e}; 
  }
 }

如何知道自己的用户电子邮件以编程方式获取UID?

How can I programmatically obtain the UID knowing its user email?

推荐答案

无法使用Firebase身份验证客户端API从用户的电子邮件地址中查找用户的UID.由于此查找被视为受信任的操作,因此仅在用于Firebase身份验证的Admin SDK中可用.

There is no way to look up a user's UID from their email address using the Firebase Authentication client-side APIs. Since this lookup is considered a trusted operations, it is only available in the Admin SDK for Firebase Authentication.

两个最常见的解决方案是:

The two most common solutions are:

  1. 在受信任的环境(例如Cloud Functions)中创建自定义服务器端API,以执行查找,然后从客户端应用程序调用该API.您必须确保只有授权用户才能执行此查找.

  1. Create a custom server-side API in a trusted environment (such as Cloud Functions) that performs the lookup, and then call that API from your client-side application. You will have to make sure that only authorized users can perform this lookup.

在创建他们的帐户时或每次登录时,将有关每个用户的信息存储到数据库中(例如,您为问题添加了标签的实时数据库).然后,您可以从电子邮件中查找UID数据库.同样,在这里,您将必须确保仅以适合您应用程序的数据隐私要求的方式提供数据.

Store the information about each user into a database (like the Realtime Database that you tagged your question with) when their account is created, or whenever they sign in. Then you can look up the UID from the email in the database. Here too, you will have to ensure that the data is only available in ways that fit with your application's data privacy requirements.

请注意,如果您只需要知道是否正在使用电子邮件地址(而不是使用该电子邮件地址的特定UID),则可以调用

Note that if you just need to know whether an email address is in use (and not the specific UID that uses it), you can call the fetchSignInMethodsForEmail method.

这篇关于如何获得Firebase UID认识的电子邮件用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆