Skip to content

Commit cdb46be

Browse files
fix(auth): adds missing EMAIL_NOT_FOUND error code (#436)
* fix(auth): adds missing EMAIL_NOT_FOUND error code Catch EMAIL_NOT_FOUND when /accounts:sendOobCode is called for password reset on a user that does not exist. * Adds additional test. Co-authored-by: Google Open Source Bot <26440463+google-oss-bot@users.noreply.github.com>
1 parent 18f3a65 commit cdb46be

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

auth/email_action_links_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"net/http"
2222
"reflect"
2323
"testing"
24+
25+
"firebase.google.com/go/v4/errorutils"
2426
)
2527

2628
const (
@@ -177,6 +179,27 @@ func TestPasswordResetLinkWithSettings(t *testing.T) {
177179
}
178180
}
179181

182+
func TestPasswordResetLinkWithSettingsNonExistingUser(t *testing.T) {
183+
resp := `{
184+
"error": {
185+
"message": "EMAIL_NOT_FOUND"
186+
}
187+
}`
188+
s := echoServer([]byte(resp), t)
189+
defer s.Close()
190+
s.Status = http.StatusBadRequest
191+
192+
link, err := s.Client.PasswordResetLinkWithSettings(context.Background(), testEmail, testActionCodeSettings)
193+
if link != "" || err == nil {
194+
t.Errorf("PasswordResetLinkWithSettings() = (%q, %v); want = (%q, error)", link, err, "")
195+
}
196+
197+
want := "no user record found for the given email"
198+
if err.Error() != want || !IsEmailNotFound(err) || !errorutils.IsNotFound(err) {
199+
t.Errorf("PasswordResetLinkWithSettings() error = %v; want = %q", err, want)
200+
}
201+
}
202+
180203
func TestEmailSignInLink(t *testing.T) {
181204
s := echoServer(testActionLinkResponse, t)
182205
defer s.Close()

auth/user_mgt.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ const (
450450
// Backend-generated error codes
451451
configurationNotFound = "CONFIGURATION_NOT_FOUND"
452452
emailAlreadyExists = "EMAIL_ALREADY_EXISTS"
453+
emailNotFound = "EMAIL_NOT_FOUND"
453454
invalidDynamicLinkDomain = "INVALID_DYNAMIC_LINK_DOMAIN"
454455
phoneNumberAlreadyExists = "PHONE_NUMBER_ALREADY_EXISTS"
455456
tenantNotFound = "TENANT_NOT_FOUND"
@@ -468,6 +469,11 @@ func IsEmailAlreadyExists(err error) bool {
468469
return hasAuthErrorCode(err, emailAlreadyExists)
469470
}
470471

472+
// IsEmailNotFound checks if the given error was due to the user record corresponding to the email not being found.
473+
func IsEmailNotFound(err error) bool {
474+
return hasAuthErrorCode(err, emailNotFound)
475+
}
476+
471477
// IsInsufficientPermission checks if the given error was due to insufficient permissions.
472478
//
473479
// Deprecated. Always returns false.
@@ -1298,6 +1304,11 @@ var serverError = map[string]*authError{
12981304
message: "user with the provided email already exists",
12991305
authCode: emailAlreadyExists,
13001306
},
1307+
"EMAIL_NOT_FOUND": {
1308+
code: internal.NotFound,
1309+
message: "no user record found for the given email",
1310+
authCode: emailNotFound,
1311+
},
13011312
"INVALID_DYNAMIC_LINK_DOMAIN": {
13021313
code: internal.InvalidArgument,
13031314
message: "the provided dynamic link domain is not configured or authorized for the current project",

0 commit comments

Comments
 (0)