How to get Firebase UID knowing email user?

霸气de小男生 提交于 2021-02-11 13:17:35

问题


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.

It's my code...

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

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


回答1:


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. 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.

  2. 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.

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.



来源:https://stackoverflow.com/questions/63316778/how-to-get-firebase-uid-knowing-email-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!