Skip to content

Commit

Permalink
fix(authentication): NullPointerException on Android (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Jul 15, 2022
1 parent d78f6ae commit 4a0e4df
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-seahorses-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@capacitor-firebase/authentication": patch
---

fix(android): `NullPointerException`
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
import androidx.annotation.Nullable;
import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GetTokenResult;
Expand Down Expand Up @@ -172,43 +169,43 @@ public void setLanguageCode(String languageCode) {
firebaseAuthInstance.setLanguageCode(languageCode);
}

public void signInWithApple(PluginCall call) {
public void signInWithApple(final PluginCall call) {
appleAuthProviderHandler.signIn(call);
}

public void signInWithFacebook(PluginCall call) {
public void signInWithFacebook(final PluginCall call) {
facebookAuthProviderHandler.signIn(call);
}

public void signInWithGithub(PluginCall call) {
public void signInWithGithub(final PluginCall call) {
oAuthProviderHandler.signIn(call, "github.com");
}

public void signInWithGoogle(PluginCall call) {
public void signInWithGoogle(final PluginCall call) {
googleAuthProviderHandler.signIn(call);
}

public void signInWithMicrosoft(PluginCall call) {
public void signInWithMicrosoft(final PluginCall call) {
oAuthProviderHandler.signIn(call, "microsoft.com");
}

public void signInWithPhoneNumber(PluginCall call) {
public void signInWithPhoneNumber(final PluginCall call) {
phoneAuthProviderHandler.signIn(call);
}

public void signInWithPlayGames(PluginCall call) {
public void signInWithPlayGames(final PluginCall call) {
playGamesAuthProviderHandler.signIn(call);
}

public void signInWithTwitter(PluginCall call) {
public void signInWithTwitter(final PluginCall call) {
oAuthProviderHandler.signIn(call, "twitter.com");
}

public void signInWithYahoo(PluginCall call) {
public void signInWithYahoo(final PluginCall call) {
oAuthProviderHandler.signIn(call, "yahoo.com");
}

public void signInWithCustomToken(PluginCall call) {
public void signInWithCustomToken(final PluginCall call) {
boolean skipNativeAuth = this.config.getSkipNativeAuth();
if (skipNativeAuth) {
call.reject(FirebaseAuthenticationPlugin.ERROR_CUSTOM_TOKEN_SKIP_NATIVE_AUTH);
Expand All @@ -235,7 +232,7 @@ public void signInWithCustomToken(PluginCall call) {
);
}

public void signInWithEmailAndPassword(PluginCall call) {
public void signInWithEmailAndPassword(final PluginCall call) {
boolean skipNativeAuth = this.config.getSkipNativeAuth();
if (skipNativeAuth) {
call.reject(FirebaseAuthenticationPlugin.ERROR_EMAIL_SIGN_IN_SKIP_NATIVE_AUTH);
Expand Down Expand Up @@ -263,7 +260,7 @@ public void signInWithEmailAndPassword(PluginCall call) {
);
}

public void signOut(PluginCall call) {
public void signOut(final PluginCall call) {
FirebaseAuth.getInstance().signOut();
if (googleAuthProviderHandler != null) {
googleAuthProviderHandler.signOut();
Expand Down Expand Up @@ -305,15 +302,15 @@ public void useEmulator(@NonNull String host, int port) {
firebaseAuthInstance.useEmulator(host, port);
}

public void startActivityForResult(PluginCall call, Intent intent, String callbackName) {
public void startActivityForResult(final PluginCall call, Intent intent, String callbackName) {
plugin.startActivityForResult(call, intent, callbackName);
}

public void handleGoogleAuthProviderActivityResult(PluginCall call, ActivityResult result) {
public void handleGoogleAuthProviderActivityResult(final PluginCall call, ActivityResult result) {
googleAuthProviderHandler.handleOnActivityResult(call, result);
}

public void handlePlayGamesAuthProviderActivityResult(PluginCall call, ActivityResult result) {
public void handlePlayGamesAuthProviderActivityResult(final PluginCall call, ActivityResult result) {
playGamesAuthProviderHandler.handleOnActivityResult(call, result);
}

Expand Down Expand Up @@ -364,7 +361,7 @@ public void handleSuccessfulSignIn(
);
}

public void handleFailedSignIn(PluginCall call, String message, Exception exception) {
public void handleFailedSignIn(final PluginCall call, String message, Exception exception) {
if (message == null && exception != null) {
message = exception.getLocalizedMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Intent;
import android.util.Log;
import androidx.annotation.Nullable;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
Expand All @@ -26,6 +27,8 @@ public class FacebookAuthProviderHandler {
private FirebaseAuthentication pluginImplementation;
private CallbackManager mCallbackManager;
private LoginButton loginButton;

@Nullable
private PluginCall savedCall;

public FacebookAuthProviderHandler(FirebaseAuthentication pluginImplementation) {
Expand Down Expand Up @@ -91,14 +94,23 @@ private void handleSuccessCallback(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
String accessTokenString = accessToken.getToken();
AuthCredential credential = FacebookAuthProvider.getCredential(accessTokenString);
if (savedCall == null) {
return;
}
pluginImplementation.handleSuccessfulSignIn(savedCall, credential, null, null, accessTokenString);
}

private void handleCancelCallback() {
if (savedCall == null) {
return;
}
pluginImplementation.handleFailedSignIn(savedCall, ERROR_SIGN_IN_CANCELED, null);
}

private void handleErrorCallback(FacebookException exception) {
if (savedCall == null) {
return;
}
pluginImplementation.handleFailedSignIn(savedCall, null, exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void signOut() {
mGoogleSignInClient.signOut();
}

public void handleOnActivityResult(PluginCall call, ActivityResult result) {
public void handleOnActivityResult(final PluginCall call, ActivityResult result) {
Intent data = result.getData();
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public PhoneAuthProviderHandler(FirebaseAuthentication pluginImplementation) {
this.pluginImplementation = pluginImplementation;
}

public void signIn(PluginCall call) {
public void signIn(final PluginCall call) {
String phoneNumber = call.getString("phoneNumber");
String verificationId = call.getString("verificationId");
String verificationCode = call.getString("verificationCode");
Expand All @@ -31,7 +31,7 @@ public void signIn(PluginCall call) {
}
}

private void verifyPhoneNumber(PluginCall call, String phoneNumber) {
private void verifyPhoneNumber(final PluginCall call, String phoneNumber) {
PhoneAuthOptions.Builder builder = PhoneAuthOptions
.newBuilder(pluginImplementation.getFirebaseAuthInstance())
.setPhoneNumber(phoneNumber)
Expand All @@ -42,12 +42,12 @@ private void verifyPhoneNumber(PluginCall call, String phoneNumber) {
PhoneAuthProvider.verifyPhoneNumber(options);
}

private void handleVerificationCode(PluginCall call, String verificationId, String verificationCode) {
private void handleVerificationCode(final PluginCall call, String verificationId, String verificationCode) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, verificationCode);
pluginImplementation.handleSuccessfulSignIn(call, credential, null);
}

private PhoneAuthProvider.OnVerificationStateChangedCallbacks createCallbacks(PluginCall call) {
private PhoneAuthProvider.OnVerificationStateChangedCallbacks createCallbacks(final PluginCall call) {
return new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void signOut() {
mGoogleSignInClient.signOut();
}

public void handleOnActivityResult(PluginCall call, ActivityResult result) {
public void handleOnActivityResult(final PluginCall call, ActivityResult result) {
Intent data = result.getData();
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
Expand All @@ -59,7 +59,7 @@ private GoogleSignInClient buildGoogleSignInClient() {
return buildGoogleSignInClient(null);
}

private GoogleSignInClient buildGoogleSignInClient(@Nullable PluginCall call) {
private GoogleSignInClient buildGoogleSignInClient(@Nullable final PluginCall call) {
GoogleSignInOptions.Builder gsob = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(pluginImplementation.getPlugin().getContext().getString(R.string.default_web_client_id))
.requestEmail();
Expand Down

0 comments on commit 4a0e4df

Please sign in to comment.