Skip to content

Commit

Permalink
feat(authentication): provide error code (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Apr 30, 2023
1 parent 6951f3b commit 3c8b54f
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 115 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-drinks-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/authentication': minor
---

feat: provide error code
19 changes: 19 additions & 0 deletions packages/authentication/BREAKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ If you want to use this plugin with Capacitor 4, please install version `1.4.0`:
npm i @capacitor-firebase/authentication@1.4.0
```

### Error handling

Errors generated by the Firebase SDK now contain a `code` property.
This can be used to identify the specific Firebase Authentication error.

```ts
try {
const result = await FirebaseAuthentication.signInWithEmailAndPassword({
email: 'mail@example.com',
password: '1234',
});
console.log(result.user);
} catch (error) {
if (error.code === 'wrong-password') {
alert('The password is invalid or the user does not have a password.');
}
}
```

### Localized error messages

On Android, error messages were previously generated with `getLocalizedMessage`. They are no longer localized and are generated with `getMessage` instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ public void createUserWithEmailAndPassword(PluginCall call) {
plugin.getActivity(),
task -> {
if (task.isSuccessful()) {
Logger.debug(TAG, "createUserWithEmailAndPassword succeeded.");
FirebaseUser user = getCurrentUser();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null, null);
call.resolve(signInResult);
} else {
Logger.error(TAG, "createUserWithEmailAndPassword failed.", task.getException());
call.reject(FirebaseAuthenticationPlugin.ERROR_CREATE_USER_WITH_EMAIL_AND_PASSWORD_FAILED);
Exception exception = task.getException();
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
}
);
Expand Down Expand Up @@ -215,12 +216,13 @@ public void linkWithEmailAndPassword(final PluginCall call) {
plugin.getActivity(),
task -> {
if (task.isSuccessful()) {
Logger.debug(TAG, "linkWithEmailAndPassword succeeded.");
JSObject linkResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null, null);
call.resolve(linkResult);
} else {
Logger.error(TAG, "linkWithEmailAndPassword failed.", task.getException());
call.reject(FirebaseAuthenticationPlugin.ERROR_LINK_FAILED);
Exception exception = task.getException();
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
}
);
Expand Down Expand Up @@ -249,12 +251,13 @@ public void linkWithEmailLink(final PluginCall call) {
plugin.getActivity(),
task -> {
if (task.isSuccessful()) {
Logger.debug(TAG, "linkWithEmailLink succeeded.");
JSObject linkResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null, null);
call.resolve(linkResult);
} else {
Logger.error(TAG, "linkWithEmailLink failed.", task.getException());
call.reject(FirebaseAuthenticationPlugin.ERROR_LINK_FAILED);
Exception exception = task.getException();
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
}
);
Expand Down Expand Up @@ -352,7 +355,6 @@ public void signInAnonymously(final PluginCall call) {
plugin.getActivity(),
task -> {
if (task.isSuccessful()) {
Logger.debug(TAG, "signInAnonymously succeeded.");
AuthResult authResult = task.getResult();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(
authResult.getUser(),
Expand All @@ -364,8 +366,10 @@ public void signInAnonymously(final PluginCall call) {
);
call.resolve(signInResult);
} else {
Logger.error(TAG, "signInAnonymously failed.", task.getException());
call.reject(task.getException().getMessage());
Exception exception = task.getException();
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
}
);
Expand All @@ -391,13 +395,14 @@ public void signInWithEmailAndPassword(final PluginCall call) {
plugin.getActivity(),
task -> {
if (task.isSuccessful()) {
Logger.debug(TAG, "signInWithEmailAndPassword succeeded.");
FirebaseUser user = getCurrentUser();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null, null);
call.resolve(signInResult);
} else {
Logger.error(TAG, "signInWithEmailAndPassword failed.", task.getException());
call.reject(FirebaseAuthenticationPlugin.ERROR_SIGN_IN_FAILED);
Exception exception = task.getException();
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
}
);
Expand All @@ -419,7 +424,6 @@ public void signInWithEmailLink(final PluginCall call) {
plugin.getActivity(),
task -> {
if (task.isSuccessful()) {
Logger.debug(TAG, "signInWithEmailLink succeeded.");
AuthResult authResult = task.getResult();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(
authResult.getUser(),
Expand All @@ -431,8 +435,10 @@ public void signInWithEmailLink(final PluginCall call) {
);
call.resolve(signInResult);
} else {
Logger.error(TAG, "signInWithEmailLink failed.", task.getException());
call.reject(FirebaseAuthenticationPlugin.ERROR_SIGN_IN_FAILED);
Exception exception = task.getException();
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
}
);
Expand Down Expand Up @@ -485,13 +491,14 @@ public void signInWithCustomToken(final PluginCall call) {
plugin.getActivity(),
task -> {
if (task.isSuccessful()) {
Logger.debug(TAG, "signInWithCustomToken succeeded.");
FirebaseUser user = getCurrentUser();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null, null);
call.resolve(signInResult);
} else {
Logger.error(TAG, "signInWithCustomToken failed.", task.getException());
call.reject(FirebaseAuthenticationPlugin.ERROR_SIGN_IN_FAILED);
Exception exception = task.getException();
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
}
);
Expand All @@ -517,15 +524,16 @@ public void unlink(final PluginCall call, FirebaseUser user, @NonNull String pro
.addOnCompleteListener(
task -> {
if (task.isSuccessful()) {
Logger.debug(TAG, "unlink succeeded.");
AuthResult authResult = task.getResult();
JSObject userResult = FirebaseAuthenticationHelper.createUserResult(authResult.getUser());
JSObject result = new JSObject();
result.put("user", userResult);
call.resolve(result);
} else {
Logger.error(TAG, "unlink failed.", task.getException());
call.reject(FirebaseAuthenticationPlugin.ERROR_UNLINK_FAILED);
Exception exception = task.getException();
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
}
);
Expand Down Expand Up @@ -680,12 +688,13 @@ public void handleSuccessfulSignIn(
plugin.getActivity(),
task -> {
if (task.isSuccessful()) {
Logger.debug(TAG, "signInWithCredential succeeded.");
final AuthResult authResult = task.getResult();
handleSuccessfulSignIn(call, authResult, idToken, nonce, accessToken);
} else {
Logger.error(TAG, "signInWithCredential failed.", task.getException());
call.reject(FirebaseAuthenticationPlugin.ERROR_SIGN_IN_FAILED);
Exception exception = task.getException();
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
}
);
Expand All @@ -709,12 +718,13 @@ public void handleSuccessfulSignIn(
call.resolve(signInResult);
}

public void handleFailedSignIn(final PluginCall call, String message, Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
public void handleFailedSignIn(@NonNull final PluginCall call, @Nullable String message, @Nullable Exception exception) {
if (message == null && exception != null) {
message = exception.getMessage();
}
call.reject(message, exception);
Logger.error(TAG, message, exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(message, code);
}

public void handleSuccessfulLink(
Expand All @@ -735,12 +745,13 @@ public void handleSuccessfulLink(
plugin.getActivity(),
task -> {
if (task.isSuccessful()) {
Logger.debug(TAG, "linkWithCredential succeeded.");
final AuthResult authResult = task.getResult();
handleSuccessfulLink(call, authResult, idToken, nonce, accessToken);
} else {
Logger.error(TAG, "linkWithCredential failed.", task.getException());
call.reject(FirebaseAuthenticationPlugin.ERROR_LINK_FAILED);
Exception exception = task.getException();
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
}
);
Expand All @@ -765,11 +776,12 @@ public void handleSuccessfulLink(
}

public void handleFailedLink(final PluginCall call, String message, Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
if (message == null && exception != null) {
message = exception.getMessage();
}
call.reject(message, exception);
Logger.error(TAG, message, exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(message, code);
}

public void handlePhoneVerificationCompleted(@NonNull final PhoneVerificationCompletedEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.getcapacitor.JSObject;
import com.google.firebase.auth.AdditionalUserInfo;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.OAuthCredential;
import java.util.Map;
Expand All @@ -24,6 +25,18 @@ public static class ProviderId {
public static final String PHONE = "phone";
}

@Nullable
public static String createErrorCode(@Nullable Exception exception) {
if (exception == null) {
return null;
} else if (exception instanceof FirebaseAuthException) {
String errorCode = ((FirebaseAuthException) exception).getErrorCode();
errorCode = errorCode.replaceFirst("ERROR_", "");
return snakeToKebabCase(errorCode);
}
return null;
}

public static JSObject createSignInResult(
@Nullable FirebaseUser user,
@Nullable AuthCredential credential,
Expand Down Expand Up @@ -122,4 +135,8 @@ public static JSObject createAdditionalUserInfoResult(@Nullable AdditionalUserIn
}
return result;
}

private static String snakeToKebabCase(String snakeCase) {
return snakeCase.replaceAll("_+", "-").toLowerCase();
}
}
Loading

0 comments on commit 3c8b54f

Please sign in to comment.