Skip to content

Commit 21d7c18

Browse files
Cedricmikehardy
Cedric
authored andcommitted
handle email error on android
1 parent 4d35fb3 commit 21d7c18

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

packages/app/android/src/reactnative/java/io/invertase/firebase/common/ReactNativeFirebaseModule.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ public static void rejectPromiseWithCodeAndMessage(
5454
promise.reject(code, message, userInfoMap);
5555
}
5656

57+
public static void rejectPromiseWithMap(
58+
Promise promise, String code, String message, ReadableMap map) {
59+
WritableMap userInfoMap = Arguments.createMap();
60+
userInfoMap.putString("code", code);
61+
userInfoMap.putString("message", message);
62+
userInfoMap.merge(map);
63+
promise.reject(code, message, userInfoMap);
64+
}
65+
5766
public static void rejectPromiseWithCodeAndMessage(Promise promise, String code, String message) {
5867
WritableMap userInfoMap = Arguments.createMap();
5968
userInfoMap.putString("code", code);

packages/app/lib/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export namespace ReactNativeFirebase {
4242
*/
4343
readonly message: string;
4444

45+
/**
46+
* The email address of the user's account used in the operation that triggered the error, if applicable
47+
*/
48+
readonly email?: string;
49+
4550
/**
4651
* The firebase module namespace that this error originated from, e.g. 'analytics'
4752
*/

packages/app/lib/internal/NativeFirebaseError.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ export default class NativeFirebaseError extends Error {
3939
value: `[${this.code}] ${userInfo.message || nativeError.message}`,
4040
});
4141

42+
if (typeof userInfo === 'object' && userInfo !== null) {
43+
if ('email' in userInfo) {
44+
Object.defineProperty(this, 'email', {
45+
enumerable: true,
46+
value: userInfo.email,
47+
});
48+
}
49+
}
50+
4251
Object.defineProperty(this, 'jsStack', {
4352
enumerable: false,
4453
value: jsStack,

packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.google.firebase.auth.FirebaseAuth;
4747
import com.google.firebase.auth.FirebaseAuthException;
4848
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
49+
import com.google.firebase.auth.FirebaseAuthUserCollisionException;
4950
import com.google.firebase.auth.FirebaseAuthMultiFactorException;
5051
import com.google.firebase.auth.FirebaseAuthProvider;
5152
import com.google.firebase.auth.FirebaseAuthSettings;
@@ -868,9 +869,12 @@ private void signInWithCredential(
868869
@ReactMethod
869870
public void signInWithProvider(String appName, String providerId, @Nullable String email, Promise promise){
870871
OAuthProvider.Builder provider = OAuthProvider.newBuilder(providerId);
872+
871873
if(email != null){
872874
provider.addCustomParameter("login_hint", email);
873875
}
876+
provider.addCustomParameter("prompt", "select_account");
877+
874878
Activity activity = getCurrentActivity();
875879
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
876880
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
@@ -908,9 +912,12 @@ public void onFailure(@NonNull Exception e) {
908912
@ReactMethod
909913
public void linkWithProvider(String appName, String providerId, @Nullable String email, Promise promise){
910914
OAuthProvider.Builder provider = OAuthProvider.newBuilder(providerId);
915+
911916
if(email != null){
912917
provider.addCustomParameter("login_hint", email);
913918
}
919+
provider.addCustomParameter("prompt", "select_account");
920+
914921
Activity activity = getCurrentActivity();
915922
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
916923
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
@@ -1977,7 +1984,7 @@ private void promiseWithAuthResult(AuthResult authResult, Promise promise) {
19771984
if(authResult.getCredential() instanceof OAuthCredential){
19781985
OAuthCredential creds = (OAuthCredential) authResult.getCredential();
19791986
WritableMap credentialMap = Arguments.createMap();
1980-
1987+
19811988
credentialMap.putString("providerId", creds.getProvider());
19821989
credentialMap.putString("signInMethod", creds.getSignInMethod());
19831990

@@ -2037,15 +2044,23 @@ private void promiseWithAuthResult(AuthResult authResult, Promise promise) {
20372044
*/
20382045
private void promiseRejectAuthException(Promise promise, Exception exception) {
20392046
WritableMap error = getJSError(exception);
2040-
2047+
20412048
final String sessionId = error.getString("sessionId");
20422049
final MultiFactorResolver multiFactorResolver = mCachedResolvers.get(sessionId);
20432050
WritableMap resolverAsMap = Arguments.createMap();
2051+
2052+
WritableMap map = Arguments.createMap();
20442053
if (multiFactorResolver != null) {
20452054
resolverAsMap = resolverToMap(sessionId, multiFactorResolver);
2055+
map.putMap("resolver", resolverAsMap);
20462056
}
2047-
rejectPromiseWithCodeAndMessage(
2048-
promise, error.getString("code"), error.getString("message"), resolverAsMap);
2057+
2058+
if(error.getString("email") != null){
2059+
map.putString("email", error.getString("email"));
2060+
}
2061+
2062+
rejectPromiseWithMap(
2063+
promise, error.getString("code"), error.getString("message"), map);
20492064
}
20502065

20512066
/**
@@ -2060,7 +2075,6 @@ private WritableMap getJSError(Exception exception) {
20602075
String invalidEmail = "The email address is badly formatted.";
20612076

20622077
System.out.print(exception);
2063-
20642078
try {
20652079
FirebaseAuthException authException = (FirebaseAuthException) exception;
20662080
code = authException.getErrorCode();
@@ -2096,7 +2110,7 @@ private WritableMap getJSError(Exception exception) {
20962110
+ " before retrying this request.";
20972111
break;
20982112
case "ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL":
2099-
message =
2113+
message =
21002114
"An account already exists with the same email address but different sign-in"
21012115
+ " credentials. Sign in using a provider associated with this email address.";
21022116
break;
@@ -2134,6 +2148,10 @@ private WritableMap getJSError(Exception exception) {
21342148
}
21352149
}
21362150

2151+
if(exception instanceof FirebaseAuthUserCollisionException) {
2152+
error.putString("email", ((FirebaseAuthUserCollisionException) exception).getEmail());
2153+
}
2154+
21372155
if (exception instanceof FirebaseAuthMultiFactorException) {
21382156
final FirebaseAuthMultiFactorException multiFactorException =
21392157
(FirebaseAuthMultiFactorException) exception;

0 commit comments

Comments
 (0)