Skip to content

Commit 3e36410

Browse files
Add reloadUser and sendEmailVerification
1 parent b88cf02 commit 3e36410

File tree

8 files changed

+181
-18
lines changed

8 files changed

+181
-18
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ cordova plugin add cordova-plugin-firebaseui-auth --variable ANDROID_FIREBASE_CO
3838
--variable FACEBOOK_DISPLAY_NAME="My application"
3939
--variable REVERSED_CLIENT_ID="com.googleusercontent.apps.9999999999-xxxxxxxxxxxxxxxxxxxxxxxxxx"
4040
--variable COLOR_PRIMARY="#ffffff"
41-
--variable COLOR_PRIMARY="#ffffff"
4241
--variable COLOR_DARK_PRIMARY="#555555"
4342
--variable COLOR_LIGHT_PRIMARY="#aaaaaa"
4443
--variable COLOR_ACCENT="#7C4DFF"
@@ -215,6 +214,26 @@ Sign out failed for some reason.
215214
## getToken()
216215
Get an access token, returning a Promise.
217216

217+
## deleteUser()
218+
219+
### deleteusersuccess
220+
The user was deleted successfully.
221+
222+
### deleteuserfailure
223+
The user was not deleted.
224+
225+
## sendEmailVerification()
226+
Sends a verification email for the current user.
227+
228+
### emailverificationsent
229+
The email verification was sent.
230+
231+
### emailverificationnotsent
232+
The email verification was not sent.
233+
234+
## reloadUser
235+
Reloads user details. Useful after a verification link has been used. A signinsuccess event will be raised.
236+
218237
# What platform configuration is carried out?
219238
For iOS and Android a number of platform files are added or updated based on the supplied configuration.
220239

@@ -292,6 +311,11 @@ In order to ensure the browser implementation works, it will be necessary to con
292311
```
293312

294313
# History
314+
## 0.0.9
315+
- Added missing documentation for deleteUser() method
316+
- Added sendEmailVerification() method
317+
- Added property newUser to user details. Only true when user us first created.
318+
295319
## 0.0.8
296320
- Update Android dependency versions
297321
- Update iOS dependency versions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cordova-plugin-firebaseui-auth",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "Adds support for Firebase UI authentication.",
55
"repository": {
66
"type": "git",

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-firebaseui-auth" version="0.0.8">
2+
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-firebaseui-auth" version="0.0.9">
33
<name>Firebase UI Authentication</name>
44
<description>Adds support for Firebase Authentication to your Cordova/PhoneGap apps.</description>
55
<license>Apache 2.0</license>

src/android/uk/co/reallysmall/cordova/plugin/firebase/ui/auth/FirebaseUIAuthPlugin.java

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.firebase.ui.auth.AuthUI;
99
import com.google.android.gms.tasks.OnCompleteListener;
1010
import com.google.android.gms.tasks.OnFailureListener;
11+
import com.google.android.gms.tasks.OnSuccessListener;
1112
import com.google.android.gms.tasks.Task;
1213
import com.google.firebase.auth.AuthResult;
1314
import com.google.firebase.auth.FirebaseAuth;
@@ -47,21 +48,63 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
4748
Log.d(TAG, "action : " + action);
4849

4950
if ("initialise".equals(action)) {
50-
return initialise(args, callbackContext);
51+
return initialise(args, callbackContext);
5152
} else if ("signIn".equals(action)) {
52-
return signIn(callbackContext);
53+
return signIn(callbackContext);
5354
} else if ("signOut".equals(action)) {
54-
return signOut(callbackContext);
55+
return signOut(callbackContext);
5556
} else if ("deleteUser".equals(action)) {
56-
return deleteUser(callbackContext);
57+
return deleteUser(callbackContext);
5758
} else if ("getToken".equals(action)) {
58-
return getToken(callbackContext);
59+
return getToken(callbackContext);
60+
} else if ("sendEmailVerification".equals(action)) {
61+
return sendEmailVerification(callbackContext);
62+
} else if ("reloadUser".equals(action)) {
63+
return reloadUser(callbackContext);
5964
} else {
60-
Log.w(TAG, "Unknown action : " + action);
61-
return false;
65+
Log.w(TAG, "Unknown action : " + action);
66+
return false;
6267
}
6368
}
6469

70+
private boolean reloadUser(CallbackContext callbackContext) {
71+
FirebaseUser user = firebaseAuth.getCurrentUser();
72+
73+
if (user != null) {
74+
user.reload().addOnSuccessListener(new OnSuccessListener<Void>() {
75+
76+
@Override
77+
public void onSuccess(Void aVoid) {
78+
FirebaseUser user = firebaseAuth.getCurrentUser();
79+
raiseEventForUser(user);
80+
}
81+
});
82+
}
83+
84+
return true;
85+
}
86+
87+
private boolean sendEmailVerification(final CallbackContext callbackContext) {
88+
89+
FirebaseUser user = firebaseAuth.getCurrentUser();
90+
91+
if (user != null && !anonymous) {
92+
93+
user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener() {
94+
@Override
95+
public void onComplete(@NonNull Task task) {
96+
if (task.isSuccessful()) {
97+
raiseEvent(callbackContext, "emailverificationsent", null);
98+
} else {
99+
raiseEvent(callbackContext, "emailverificationnotsent", null);
100+
}
101+
}
102+
});
103+
}
104+
105+
return true;
106+
}
107+
65108
private boolean initialise(JSONArray args, final CallbackContext callbackContext) {
66109

67110
Log.d(TAG, "initialise");
@@ -200,7 +243,7 @@ private AuthUI.SignInIntentBuilder buildCustomInstance() {
200243
}
201244
instance = instance.setIsSmartLockEnabled(smartLockEnabled, smartLockHints);
202245
} catch (JSONException ex) {
203-
Log.e(TAG, "Error in buildCustomInstance ", ex);
246+
Log.e(TAG, "Error in buildCustomInstance ", ex);
204247
}
205248

206249
return instance;
@@ -286,7 +329,7 @@ private void raiseErrorEvent(String event, int code, String message) {
286329
data.put("code", code);
287330
data.put("message", message);
288331
} catch (JSONException e) {
289-
Log.e(TAG, "Error in raiseErrorEvent ", e);
332+
Log.e(TAG, "Error in raiseErrorEvent ", e);
290333
}
291334
raiseEvent(callbackContext, event, data);
292335
}
@@ -297,15 +340,23 @@ private void raiseEventForUser(FirebaseUser user) {
297340
Log.d(TAG, "raiseEventForUser");
298341

299342
try {
343+
344+
anonymous = false;
345+
300346
resultData.put("name", user.getDisplayName());
301347
resultData.put("email", user.getEmail());
302348
resultData.put("emailVerified", user.isEmailVerified());
303349
resultData.put("id", user.getUid());
350+
if (user.getMetadata().getCreationTimestamp() == user.getMetadata().getLastSignInTimestamp()) {
351+
resultData.put("newUser", true);
352+
} else {
353+
resultData.put("newUser", false);
354+
}
304355
if (user.getPhotoUrl() != null) {
305356
resultData.put("photoUrl", user.getPhotoUrl().toString());
306357
}
307358
} catch (JSONException e) {
308-
Log.e(TAG, "Error in raiseEventForUser ", e);
359+
Log.e(TAG, "Error in raiseEventForUser ", e);
309360
}
310361

311362
raiseEvent(callbackContext, "signinsuccess", resultData);
@@ -344,7 +395,7 @@ private void raiseEvent(CallbackContext callbackContext, String type, Object dat
344395
event.put("data", data);
345396
}
346397
} catch (JSONException e) {
347-
Log.e(TAG, "Error in raiseEvent ", e);
398+
Log.e(TAG, "Error in raiseEvent ", e);
348399
}
349400

350401
PluginResult result = new PluginResult(PluginResult.Status.OK, event);
@@ -363,7 +414,7 @@ public void onComplete(@NonNull Task<AuthResult> task) {
363414
data.put("code", err.getClass().getSimpleName());
364415
data.put("message", err.getMessage());
365416
} catch (JSONException e) {
366-
Log.e(TAG, "Error in onComplete ", e);
417+
Log.e(TAG, "Error in onComplete ", e);
367418
}
368419
raiseEvent(callbackContext, "signinfailure", data);
369420
}

src/ios/FirebaseUIAuthPlugin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
- (void)signOut:(CDVInvokedUrlCommand *)command;
1111
- (void)getToken:(CDVInvokedUrlCommand *)command;
1212
- (void)deleteUser:(CDVInvokedUrlCommand *)command;
13+
- (void)sendEmailVerification:(CDVInvokedUrlCommand *)command;
14+
- (void)reloadUser:(CDVInvokedUrlCommand *)command;
1315

1416
@property(strong,nonatomic) FUIAuth *authUI;
1517
@property(strong,nonatomic) NSMutableArray<id<FUIAuthProvider>> *providers;

src/ios/FirebaseUIAuthPlugin.m

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,44 @@ - (void)deleteUser:(CDVInvokedUrlCommand *)command {
212212
}
213213
}
214214

215+
- (void)sendEmailVerification:(CDVInvokedUrlCommand *)command {
216+
217+
@try {
218+
219+
FIRUser *user = [[FIRAuth auth] currentUser];
220+
221+
[user sendEmailVerificationWithCompletion:^(NSError * _Nullable error) {
222+
if (error) {
223+
[self raiseEvent:@"emailverificationnotsent" withData:nil];
224+
} else {
225+
[self raiseEvent:@"emailverificationsent" withData:nil];
226+
[self signInAnonymous];
227+
}
228+
}];
229+
}
230+
@catch (NSException *exception) {
231+
NSLog(@"SignOut error %@", [exception reason]);
232+
@throw exception;
233+
}
234+
}
235+
236+
- (void)reloadUser:(CDVInvokedUrlCommand *)command {
237+
238+
@try {
239+
240+
FIRUser *user = [[FIRAuth auth] currentUser];
241+
242+
[user reload:^(NSError * _Nullable error);
243+
244+
[self raiseEventForUser:user];
245+
246+
}
247+
@catch (NSException *exception) {
248+
NSLog(@"SignOut error %@", [exception reason]);
249+
@throw exception;
250+
}
251+
}
252+
215253
- (void)authUI:(nonnull FUIAuth *)authUI didSignInWithUser:(nullable FIRUser *)user error:(nullable NSError *)error {
216254
if (error == nil) {
217255
[self raiseEventForUser:user];
@@ -242,24 +280,34 @@ - (void)raiseEventForUser:(FIRUser *)user {
242280

243281
NSNumber *isEmailVerified;
244282

283+
NSNumber *newUser;
284+
245285
if ([user isEmailVerified]) {
246286
isEmailVerified = @YES;
247287
} else {
248288
isEmailVerified = @NO;
249289
}
250290

291+
if ([user metaData]) {
292+
newUser = @YES;
293+
} else {
294+
newUser = @NO;
295+
}
296+
251297
if ([user photoURL] != nil) {
252298
result = @{@"email" : [self emptyIfNull:[user email]],
253299
@"emailVerified" : isEmailVerified,
254300
@"name" : [self emptyIfNull:[user displayName]],
255301
@"id" : [self emptyIfNull:[user uid]],
256-
@"photoUrl" : [[user photoURL] absoluteString]
302+
@"photoUrl" : [[user photoURL] absoluteString],
303+
@"newUser" : newUser
257304
};
258305
} else {
259306
result = @{@"email" : [self emptyIfNull:[user email]],
260307
@"emailVerified" : isEmailVerified,
261308
@"name" : [self emptyIfNull:[user displayName]],
262-
@"id" : [self emptyIfNull:[user uid]]
309+
@"id" : [self emptyIfNull:[user uid]],
310+
@"newUser" : newUser
263311
};
264312
}
265313

www/browser/firebaseUIAuthPlugin.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,30 @@ function FirebaseUIAuth(options, resolve) {
133133
return false;
134134
};
135135

136+
this.reloadUser = function() {
137+
138+
var self = this;
139+
140+
firebase.auth().currentUser.reload().then(function() {
141+
self.raiseEventForUser(firebase.auth().currentUser);
142+
});
143+
};
144+
136145
this.raiseEventForUser = function(user) {
146+
147+
var newUser = false;
148+
149+
if (user.metadata.creationTime === user.metadata.lastSignInTime) {
150+
newUser = true;
151+
}
152+
137153
var detail = {
138154
"detail": {
139155
"name": user.displayName,
140156
"email": user.email,
141157
"emailVerified": user.emailVerified,
142-
"id": user.uid
158+
"id": user.uid,
159+
"newUser": newUser
143160
}
144161
};
145162

@@ -185,6 +202,20 @@ function FirebaseUIAuth(options, resolve) {
185202
}
186203
};
187204

205+
this.sendEmailVerification = function() {
206+
207+
var currentUser = firebase.auth().currentUser;
208+
var customEvent;
209+
210+
currentUser.sendEmailVerification().then(function() {
211+
customEvent = new CustomEvent("emailverificationsent", {});
212+
window.dispatchEvent(customEvent);
213+
}, function(error) {
214+
customEvent = new CustomEvent("emailverificationnotsent", {});
215+
window.dispatchEvent(customEvent);
216+
});
217+
}
218+
188219
this.signOut = function() {
189220
firebase.auth().signOut();
190221
var customEvent = new CustomEvent("signoutsuccess", {});

www/firebaseUIAuthPlugin.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ function FirebaseUIAuth(options) {
3232
return exec(dispatchEvent, null, PLUGIN_NAME, 'deleteUser', []);
3333
};
3434

35+
this.sendEmailVerification = function() {
36+
return exec(dispatchEvent, null, PLUGIN_NAME, 'sendEmailVerification', []);
37+
}
38+
39+
this.reloadUser = function() {
40+
return exec(dispatchEvent, null, PLUGIN_NAME, 'reloadUser', []);
41+
}
3542
}
3643

3744
module.exports = {

0 commit comments

Comments
 (0)