Skip to content

Commit

Permalink
Implemented setAuthStateChanged for iOS and added getCurrentUser (iOS…
Browse files Browse the repository at this point in the history
… and Android) (#54)

* - Added getCurrentUser (iOS and Android)
- Added setAuthStateChanged (iOS)

* Updated documentation getCurrentUser and fixed a bug in userToDictionary

* Added call setKeepCallbackAsBool for the authChanged event

* Changed getCurrentUser to return null instead of error if user is not logged in
  • Loading branch information
JavierPAYTEF authored and chemerisuk committed Mar 9, 2019
1 parent 7199ab3 commit d2d99e2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -17,6 +17,14 @@ To use phone number authentication on iOS, your app must be able to receive sile
## Methods
Every method call returns a promise which is optionally fulfilled with an appropriate value.

### getCurrentUser()
Returns the current user in the Firebase instance.
```js
cordova.plugins.firebase.auth.getCurrentUser().then(function(userInfo) {
// user information or null if not logged in
})
```

### getIdToken(_forceRefresh_)
Returns a JWT token used to identify the user to a Firebase service.
```js
Expand Down
10 changes: 10 additions & 0 deletions src/android/FirebaseAuthenticationPlugin.java
Expand Up @@ -44,6 +44,16 @@ protected void pluginInitialize() {
this.phoneAuthProvider = PhoneAuthProvider.getInstance();
}

@CordovaMethod
private void getCurrentUser(final CallbackContext callbackContext) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, (String)null));
} else {
callbackContext.success(getProfileData(user));
}
}

@CordovaMethod
private void getIdToken(boolean forceRefresh, final CallbackContext callbackContext) {
FirebaseUser user = firebaseAuth.getCurrentUser();
Expand Down
2 changes: 2 additions & 0 deletions src/ios/FirebaseAuthenticationPlugin.h
Expand Up @@ -2,6 +2,7 @@

@interface FirebaseAuthenticationPlugin : CDVPlugin

- (void)getCurrentUser:(CDVInvokedUrlCommand*)command;
- (void)getIdToken:(CDVInvokedUrlCommand*)command;
- (void)createUserWithEmailAndPassword:(CDVInvokedUrlCommand*)command;
- (void)sendEmailVerification:(CDVInvokedUrlCommand*)command;
Expand All @@ -15,5 +16,6 @@
- (void)verifyPhoneNumber:(CDVInvokedUrlCommand*)command;
- (void)signOut:(CDVInvokedUrlCommand*)command;
- (void)setLanguageCode:(CDVInvokedUrlCommand*)command;
- (void)setAuthStateChanged:(CDVInvokedUrlCommand*)command;

@end
32 changes: 32 additions & 0 deletions src/ios/FirebaseAuthenticationPlugin.m
@@ -1,6 +1,12 @@
#import "FirebaseAuthenticationPlugin.h"
@import Firebase;

@interface FirebaseAuthenticationPlugin() {
NSString* authChangedCallbackId;
}
@property(strong, nonatomic) FIRAuthStateDidChangeListenerHandle handle;
@end

@implementation FirebaseAuthenticationPlugin

- (void)pluginInitialize {
Expand All @@ -11,6 +17,12 @@ - (void)pluginInitialize {
}
}

- (void)getCurrentUser:(CDVInvokedUrlCommand *)command {
FIRUser *user = [FIRAuth auth].currentUser;
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self userToDictionary:user]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)getIdToken:(CDVInvokedUrlCommand *)command {
BOOL forceRefresh = [[command.arguments objectAtIndex:0] boolValue];
FIRUser *user = [FIRAuth auth].currentUser;
Expand Down Expand Up @@ -198,6 +210,23 @@ - (void)setLanguageCode:(CDVInvokedUrlCommand*)command {
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)setAuthStateChanged:(CDVInvokedUrlCommand*)command {
BOOL disable = [[command.arguments objectAtIndex:0] boolValue];
if (_handle) {
[[FIRAuth auth] removeAuthStateDidChangeListener:_handle];
self.handle = nil;
}
if (!disable) {
authChangedCallbackId = [command.callbackId copy];
self.handle = [[FIRAuth auth]
addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self userToDictionary:user]];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:authChangedCallbackId];
}];
}
}

- (CDVPluginResult*) createAuthResult:(FIRAuthDataResult*)result withError:(NSError*)error {
CDVPluginResult *pluginResult;
if (error) {
Expand All @@ -209,6 +238,9 @@ - (CDVPluginResult*) createAuthResult:(FIRAuthDataResult*)result withError:(NSEr
}

- (NSDictionary*)userToDictionary:(FIRUser *)user {
if (!user) {
return nil;
}
return @{
@"uid": user.uid,
@"providerId": user.providerID,
Expand Down
5 changes: 5 additions & 0 deletions www/FirebaseAuthentication.js
Expand Up @@ -2,6 +2,11 @@ var exec = require("cordova/exec");
var PLUGIN_NAME = "FirebaseAuthentication";

module.exports = {
getCurrentUser: function () {
return new Promise(function (resolve, reject) {
exec(resolve, reject, PLUGIN_NAME, "getCurrentUser", []);
});
},
getIdToken: function(forceRefresh) {
return new Promise(function(resolve, reject) {
if (forceRefresh == null) forceRefresh = false;
Expand Down

0 comments on commit d2d99e2

Please sign in to comment.