Skip to content

Commit

Permalink
[Android] Add direct support to sign in and link with credentials obt…
Browse files Browse the repository at this point in the history
…ained via verifyPhoneNumber()
  • Loading branch information
dpa99c committed Oct 24, 2019
1 parent 5f7b945 commit cfb8548
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 14 deletions.
79 changes: 78 additions & 1 deletion src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@
import android.media.AudioAttributes;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.util.Base64;
import android.util.Log;

import com.crashlytics.android.Crashlytics;
import java.lang.reflect.Field;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
Expand Down Expand Up @@ -191,6 +197,12 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
} else if (action.equals("verifyPhoneNumber")) {
this.verifyPhoneNumber(callbackContext, args.getString(0), args.getInt(1));
return true;
} else if (action.equals("signInWithCredential")) {
this.signInWithCredential(callbackContext, args);
return true;
} else if (action.equals("linkUserWithCredential")) {
this.linkUserWithCredential(callbackContext, args);
return true;
} else if (action.equals("startTrace")) {
this.startTrace(callbackContext, args.getString(0));
return true;
Expand Down Expand Up @@ -889,6 +901,71 @@ private static String getPrivateField(PhoneAuthCredential credential, Field fiel
}
}

public void signInWithCredential(final CallbackContext callbackContext, JSONArray args){
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
String verificationId = args.getString(0);
String code = args.getString(1);
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);

FirebaseAuth.getInstance().signInWithCredential(credential).addOnCompleteListener(cordova.getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
PluginResult pluginresult = new PluginResult(PluginResult.Status.OK);
if (!task.isSuccessful()) {
String errMessage;
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
errMessage = "Invalid verification code";
}else{
errMessage = task.getException().toString();
}
pluginresult = new PluginResult(PluginResult.Status.ERROR, errMessage);
}
callbackContext.sendPluginResult(pluginresult);
}
}
);
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
}
}
});
}

public void linkUserWithCredential(final CallbackContext callbackContext, JSONArray args){
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
String verificationId = args.getString(0);
String code = args.getString(1);
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);

FirebaseAuth.getInstance().getCurrentUser().linkWithCredential(credential).addOnCompleteListener(cordova.getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
PluginResult pluginresult = new PluginResult(PluginResult.Status.OK);
if (!task.isSuccessful()) {
String errMessage;
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
errMessage = "Invalid verification code";
}else{
errMessage = task.getException().toString();
}
pluginresult = new PluginResult(PluginResult.Status.ERROR, errMessage);
}
callbackContext.sendPluginResult(pluginresult);
}
}
);
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
}
}
});
}


//
// Firebase Performace
//
Expand Down
33 changes: 20 additions & 13 deletions www/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,6 @@ exports.setPerformanceCollectionEnabled = function (enabled, success, error) {
};



exports.verifyPhoneNumber = function (number, timeOutDuration, success, error) {
if (typeof timeOutDuration === 'function') {
// method being called with old signature: function(number, success, error)
// timeOutDuration is the success callback, success is the error callback
exec(timeOutDuration, success, "FirebasePlugin", "verifyPhoneNumber", [number]);
} else {
// method being called with new signature: function(number, timeOutDuration, success, error)
// callbacks are correctly named
exec(success, error, "FirebasePlugin", "verifyPhoneNumber", [number, timeOutDuration]);
}
};

exports.clearAllNotifications = function (success, error) {
exec(success, error, "FirebasePlugin", "clearAllNotifications", []);
};
Expand Down Expand Up @@ -202,3 +189,23 @@ exports.setCrashlyticsUserId = function (userId, success, error) {
};


// Authentication
exports.verifyPhoneNumber = function (number, timeOutDuration, success, error) {
if (typeof timeOutDuration === 'function') {
// method being called with old signature: function(number, success, error)
// timeOutDuration is the success callback, success is the error callback
exec(timeOutDuration, success, "FirebasePlugin", "verifyPhoneNumber", [number]);
} else {
// method being called with new signature: function(number, timeOutDuration, success, error)
// callbacks are correctly named
exec(success, error, "FirebasePlugin", "verifyPhoneNumber", [number, timeOutDuration]);
}
};

exports.signInWithCredential = function (verificationId, code, success, error) {
exec(success, error, "FirebasePlugin", "signInWithCredential", [verificationId, code]);
};

exports.linkUserWithCredential = function (verificationId, code, success, error) {
exec(success, error, "FirebasePlugin", "linkUserWithCredential", [verificationId, code]);
};

0 comments on commit cfb8548

Please sign in to comment.