0
votes

I'm trying to get the user id of the user through email at the backend. The code I'm using is this.

val user:UserRecord?
try {
  user=FirebaseAuth.getInstance().getUserByEmail(email)
}catch(e:Exception){
  map["msg"]=e.localizedMessage
  return map
    }

map["msg"]=FirebaseAuth.getInstance().createCustomTokenAsync(user.uid).get()
return map

Line 3 produces an exception.

com.google.firebase.auth.FirebaseAuthException: No user record found for the 
provided email: id
at com.google.firebase.auth.FirebaseUserManager.getUserByEmail(FirebaseUserManager.java:138)
at com.google.firebase.auth.FirebaseAuth$7.execute(FirebaseAuth.java:613)
at com.google.firebase.auth.FirebaseAuth$7.execute(FirebaseAuth.java:610)
at com.google.firebase.internal.CallableOperation.call(CallableOperation.java:36)
at com.google.firebase.auth.FirebaseAuth.getUserByEmail(FirebaseAuth.java:590)
at in.ac.krishnacollege.CheckTeacherResource.checkTeacherEmail(CheckTeacherResource.kt:27)

The function getUserByEmail() should return null but it throws an exception.

1
"If the provided email does not belong to an existing user or the user cannot be fetched for any other reason, the Admin SDK throws an error." firebase.google.com/docs/auth/admin/… - user1531971
Sounds like an answer @jdv! :-) - Frank van Puffelen
@jdv They should have made it return a null if the user is not found - Varun Sharma
@varunsharma you need to read the docs. It clearly says otherwise. - user1531971

1 Answers

2
votes

I'm not expert with Firebase. (I literally dropped "Firebase getUserByEmail" into a web search.) But a cursory reading of the Firebase Guide for managing users has this to say about getUserByEmail(email):

This method returns a UserRecord object for the user corresponding to the email provided.

If the provided email does not belong to an existing user or the user cannot be fetched for any other reason, the Admin SDK throws an error. For a full list of error codes, including descriptions and resolution steps, see Admin Authentication API Errors.

There is no mention of returning null for any of the user lookup methods described. All examples show error handling in a catch block (at least for the Javascript examples), not null handling. At least for the version and release describe here.

There are no Kotlin examples. The Java example does not show exception handling, but it does not change the explanation of how errors are to be handled, and you are clearly seeing the error exception in your own code.

There may be circumstances under which this method returns null under a JVM language, but that is not the case here.