Skip to content

Commit

Permalink
Add authenticateUserWithEmailAndPassword(). Resolves dpa99c#486.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpa99c committed Sep 14, 2020
1 parent 86a0898 commit dda8b57
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 9 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ To help ensure this plugin is kept updated, new features are added and bugfixes
- [verifyPhoneNumber](#verifyphonenumber)
- [Android](#android-2)
- [iOS](#ios-2)
- [authenticateUserWithEmailAndPassword](#authenticateuserwithemailandpassword)
- [authenticateUserWithGoogle](#authenticateuserwithgoogle)
- [Android](#android-3)
- [authenticateUserWithApple](#authenticateuserwithapple)
Expand All @@ -154,9 +155,9 @@ To help ensure this plugin is kept updated, new features are added and bugfixes
- [resetRemoteConfig](#resetremoteconfig)
- [getValue](#getvalue)
- [getInfo](#getinfo)
- [getAll](#getall)
- [setConfigSettings](#setconfigsettings)
- [setDefaults](#setdefaults)
- [getAll](#getall)
- [Performance](#performance)
- [setPerformanceCollectionEnabled](#setperformancecollectionenabled)
- [isPerformanceCollectionEnabled](#isperformancecollectionenabled)
Expand Down Expand Up @@ -2542,6 +2543,33 @@ You can [set up reCAPTCHA verification for iOS](https://firebase.google.com/docs

This adds the `REVERSED_CLIENT_ID` from the `GoogleService-Info.plist` to the list of custom URL schemes in your Xcode project, so you don't need to do this manually.


### authenticateUserWithEmailAndPassword
Authenticates the user with email/password-based user account to obtain a credential that can be used to sign the user in/link to an existing user account/reauthenticate the user.

**Parameters**:
- {string} email - user email address
- {string} password - user password
- {function} success - callback function to pass {object} credentials to as an argument. The credential object has the following properties:
- {string} id - the identifier of a native credential object which can be used for signing in the user.
- {function} error - callback function which will be passed a {string} error message as an argument

Example usage:

```javascript
FirebasePlugin.authenticateUserWithEmailAndPassword(email, password, function(credential) {
console.log("Successfully authenticated with email/password");
FirebasePlugin.reauthenticateWithCredential(credential, function() {
console.log("Successfully re-authenticated");
}, function(error) {
console.error("Failed to re-authenticate", error);
});
// User is now signed in
}, function(error) {
console.error("Failed to authenticate with email/password", error);
});
```

### authenticateUserWithGoogle
Authenticates the user with a Google account to obtain a credential that can be used to sign the user in/link to an existing user account/reauthenticate the user.

Expand Down
35 changes: 35 additions & 0 deletions src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import android.util.Base64;
import android.util.Log;

import com.google.firebase.auth.EmailAuthCredential;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.crashlytics.FirebaseCrashlytics;

import com.google.android.gms.auth.api.Auth;
Expand Down Expand Up @@ -265,6 +267,8 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
this.createUserWithEmailAndPassword(callbackContext, args);
} else if (action.equals("signInUserWithEmailAndPassword")) {
this.signInUserWithEmailAndPassword(callbackContext, args);
} else if (action.equals("authenticateUserWithEmailAndPassword")) {
this.authenticateUserWithEmailAndPassword(callbackContext, args);
} else if (action.equals("signInUserWithCustomToken")) {
this.signInUserWithCustomToken(callbackContext, args);
} else if (action.equals("signInUserAnonymously")) {
Expand Down Expand Up @@ -1502,6 +1506,37 @@ public void run() {
});
}

public void authenticateUserWithEmailAndPassword(final CallbackContext callbackContext, final JSONArray args){
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
String email = args.getString(0);
String password = args.getString(1);

if(email == null || email.equals("")){
callbackContext.error("User email address must be specified");
return;
}

if(password == null || password.equals("")){
callbackContext.error("User password must be specified");
return;
}

AuthCredential authCredential = EmailAuthProvider.getCredential(email, password);
String id = FirebasePlugin.instance.saveAuthCredential(authCredential);

JSONObject returnResults = new JSONObject();
returnResults.put("instantVerification", true);
returnResults.put("id", id);
callbackContext.success(returnResults);
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
}
}
});
}


public void authenticateUserWithGoogle(final CallbackContext callbackContext, final JSONArray args){
cordova.getThreadPool().execute(new Runnable() {
Expand Down
8 changes: 4 additions & 4 deletions src/ios/AppDelegate+FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ - (void)signIn:(GIDSignIn *)signIn
[FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken
accessToken:authentication.accessToken];

int key = [[FirebasePlugin firebasePlugin] saveAuthCredential:credential];
NSNumber* key = [[FirebasePlugin firebasePlugin] saveAuthCredential:credential];
NSMutableDictionary* result = [[NSMutableDictionary alloc] init];
[result setValue:@"true" forKey:@"instantVerification"];
[result setValue:[NSNumber numberWithInt:key] forKey:@"id"];
[result setValue:key forKey:@"id"];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description];
Expand Down Expand Up @@ -520,10 +520,10 @@ - (void)authorizationController:(ASAuthorizationController *)controller
IDToken:idToken
rawNonce:rawNonce];

int key = [[FirebasePlugin firebasePlugin] saveAuthCredential:credential];
NSNumber* key = [[FirebasePlugin firebasePlugin] saveAuthCredential:credential];
NSMutableDictionary* result = [[NSMutableDictionary alloc] init];
[result setValue:@"true" forKey:@"instantVerification"];
[result setValue:[NSNumber numberWithInt:key] forKey:@"id"];
[result setValue:key forKey:@"id"];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/ios/FirebasePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- (void)verifyPhoneNumber:(CDVInvokedUrlCommand*)command;
- (void)createUserWithEmailAndPassword:(CDVInvokedUrlCommand*)command;
- (void)signInUserWithEmailAndPassword:(CDVInvokedUrlCommand*)command;
- (void)authenticateUserWithEmailAndPassword:(CDVInvokedUrlCommand*)command;
- (void)signInUserWithCustomToken:(CDVInvokedUrlCommand*)command;
- (void)signInUserAnonymously:(CDVInvokedUrlCommand*)command;
- (void)authenticateUserWithGoogle:(CDVInvokedUrlCommand*)command;
Expand Down Expand Up @@ -110,7 +111,7 @@
- (void) _logInfo: (NSString*)msg;
- (void) _logMessage: (NSString*)msg;
- (BOOL) _shouldEnableCrashlytics;
- (int) saveAuthCredential: (FIRAuthCredential *) authCredential;
- (NSNumber*) saveAuthCredential: (FIRAuthCredential *) authCredential;
- (void)executeGlobalJavascript: (NSString*)jsString;

- (void)createChannel:(CDVInvokedUrlCommand *)command;
Expand Down
24 changes: 21 additions & 3 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,23 @@ - (void)signInUserWithEmailAndPassword:(CDVInvokedUrlCommand*)command {
}
}

- (void)authenticateUserWithEmailAndPassword:(CDVInvokedUrlCommand*)command {
@try {
NSString* email = [command.arguments objectAtIndex:0];
NSString* password = [command.arguments objectAtIndex:1];
FIRAuthCredential* authCredential = [FIREmailAuthProvider credentialWithEmail:email password:password];
NSNumber* key = [self saveAuthCredential:authCredential];

NSMutableDictionary* result = [[NSMutableDictionary alloc] init];
[result setValue:@"true" forKey:@"instantVerification"];
[result setValue:key forKey:@"id"];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}@catch (NSException *exception) {
[self handlePluginExceptionWithContext:exception :command];
}
}

- (void)signInUserWithCustomToken:(CDVInvokedUrlCommand*)command {
@try {
NSString* customToken = [command.arguments objectAtIndex:0];
Expand Down Expand Up @@ -1968,9 +1985,10 @@ - (void) handleAuthResult:(FIRAuthDataResult*) authResult error:(NSError*) error
}
}

- (int) saveAuthCredential: (FIRAuthCredential*) authCredential {
int key = [self generateId];
[authCredentials setObject:authCredential forKey:[NSNumber numberWithInt:key]];
- (NSNumber*) saveAuthCredential: (FIRAuthCredential*) authCredential {
int id = [self generateId];
NSNumber* key = [NSNumber numberWithInt:id];
[authCredentials setObject:authCredential forKey:key];
return key;
}

Expand Down
4 changes: 4 additions & 0 deletions www/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ exports.signInUserWithEmailAndPassword = function (email, password, success, err
exec(success, error, "FirebasePlugin", "signInUserWithEmailAndPassword", [email, password]);
};

exports.authenticateUserWithEmailAndPassword = function (email, password, success, error) {
exec(success, error, "FirebasePlugin", "authenticateUserWithEmailAndPassword", [email, password]);
};

exports.signInUserWithCustomToken = function (customToken, success, error) {
exec(success, error, "FirebasePlugin", "signInUserWithCustomToken", [customToken]);
};
Expand Down

0 comments on commit dda8b57

Please sign in to comment.