Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 18d6ab0

Browse files
Re-Authenticate a user #245
1 parent e1bb815 commit 18d6ab0

File tree

10 files changed

+212
-56
lines changed

10 files changed

+212
-56
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ You can also change the configuration by deleting the `firebase.nativescript.jso
5252
Install the latest packages 'Google Play Services' and 'Google Repository' in your [Android SDK Manager](http://stackoverflow.com/a/37310513)
5353

5454
#### Google Play Services Version
55-
The plugin will default to version 9.8.0+ of the Android `play-services-base` SDK.
55+
The plugin will default to version 10.0+ of the Android `play-services-base` SDK.
5656
If you need to change the version (to for instance the latest version), you can add a project ext property `googlePlayServicesVersion` like so:
5757

5858
```

docs/AUTHENTICATION.md

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ Once the user is logged in you can retrieve the currently logged in user.
9696
});
9797
```
9898

99+
### Updating a profile
100+
Pass in at least one of `displayName` and `photoURL`.
101+
The logged in user will be updated, but for `getCurrentUser` to reflect the change you'll need to do a logout-login.
102+
103+
```js
104+
firebase.updateProfile({
105+
displayName: 'Eddy Verbruggen',
106+
photoURL: 'http://provider.com/profiles/eddyverbruggen.png'
107+
}).then(
108+
function () {
109+
// called when update profile was successful
110+
},
111+
function (errorMessage) {
112+
console.log(errorMessage);
113+
}
114+
);
115+
```
116+
99117
### Anonymous login
100118
Don't forget to enable anonymous login in your firebase instance.
101119

@@ -166,24 +184,6 @@ Don't forget to enable email-password login in your firebase instance.
166184
);
167185
```
168186

169-
#### Updating a profile
170-
Pass in at least one of `displayName` and `photoURL`.
171-
The logged in user will be updated, but for `getCurrentUser` to reflect the change you'll need to do a logout-login.
172-
173-
```js
174-
firebase.updateProfile({
175-
displayName: 'Eddy Verbruggen',
176-
photoURL: 'http://provider.com/profiles/eddyverbruggen.png'
177-
}).then(
178-
function () {
179-
// called when update profile was successful
180-
},
181-
function (errorMessage) {
182-
console.log(errorMessage);
183-
}
184-
);
185-
```
186-
187187
#### Resetting a password
188188
```js
189189
firebase.resetPassword({
@@ -357,6 +357,34 @@ Shouldn't be more complicated than:
357357
firebase.logout();
358358
```
359359

360+
### reauthenticate
361+
Some security-sensitive actions (deleting an account, changing a password) require that the user has recently signed in.
362+
If you perform one of these actions, and the user signed in too long ago, the action fails.
363+
When this happens (or to prevent it from happening), re-authenticate the user.
364+
365+
```js
366+
firebase.reauthenticate({
367+
type: firebase.LoginType.PASSWORD, // or GOOGLE / FACEBOOK
368+
// these are only required in type = PASSWORD
369+
370+
password: 'thePassword'
371+
}).then(
372+
function () {
373+
// you can now safely delete the account / change the password
374+
dialogs.alert({
375+
title: "Re-authenticated user",
376+
okButtonText: "OK"
377+
});
378+
},
379+
function (error) {
380+
dialogs.alert({
381+
title: "Re-authenticate error",
382+
message: error,
383+
okButtonText: "OK"
384+
});
385+
}
386+
);
387+
```
360388

361389
### sendEmailVerification
362390
Sending an "email confirmation" email can be done after the user logged in:

firebase.android.js

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ firebase._launchNotification = null;
77

88
// we need to cache and restore the context, otherwise the next invocation is broken
99
firebase._rememberedContext = null;
10+
firebase._googleSignInIdToken = null;
11+
firebase._facebookAccessToken = null;
1012

1113
var fbCallbackManager = null;
1214
var GOOGLE_SIGNIN_INTENT_ID = 123;
@@ -654,8 +656,8 @@ firebase.login = function (arg) {
654656
fbCallbackManager,
655657
new com.facebook.FacebookCallback({
656658
onSuccess: function (loginResult) {
657-
var token = loginResult.getAccessToken().getToken();
658-
var authCredential = com.google.firebase.auth.FacebookAuthProvider.getCredential(token);
659+
firebase._facebookAccessToken = loginResult.getAccessToken().getToken();
660+
var authCredential = com.google.firebase.auth.FacebookAuthProvider.getCredential(firebase._facebookAccessToken);
659661

660662
var user = com.google.firebase.auth.FirebaseAuth.getInstance().getCurrentUser();
661663
if (user) {
@@ -728,9 +730,9 @@ firebase.login = function (arg) {
728730
var success = googleSignInResult.isSuccess();
729731
if (success) {
730732
var googleSignInAccount = googleSignInResult.getSignInAccount();
731-
var idToken = googleSignInAccount.getIdToken();
733+
firebase._googleSignInIdToken = googleSignInAccount.getIdToken();
732734
var accessToken = null;
733-
var authCredential = com.google.firebase.auth.GoogleAuthProvider.getCredential(idToken, accessToken);
735+
var authCredential = com.google.firebase.auth.GoogleAuthProvider.getCredential(firebase._googleSignInIdToken, accessToken);
734736

735737
firebase._mGoogleApiClient.connect();
736738

@@ -772,6 +774,62 @@ firebase._alreadyLinkedToAuthProvider = function (user, providerId) {
772774
return false;
773775
};
774776

777+
firebase.reauthenticate = function (arg) {
778+
return new Promise(function (resolve, reject) {
779+
try {
780+
var user = com.google.firebase.auth.FirebaseAuth.getInstance().getCurrentUser();
781+
if (user === null) {
782+
reject("no current user");
783+
return;
784+
}
785+
786+
var authCredential = null;
787+
if (arg.type === firebase.LoginType.PASSWORD) {
788+
if (!arg.email || !arg.password) {
789+
reject("Auth type emailandpassword requires an email and password argument");
790+
} else {
791+
authCredential = com.google.firebase.auth.EmailAuthProvider.getCredential(arg.email, arg.password);
792+
}
793+
794+
} else if (arg.type === firebase.LoginType.GOOGLE) {
795+
if (!firebase._googleSignInIdToken) {
796+
reject("Not currently logged in with Google");
797+
return;
798+
}
799+
authCredential = com.google.firebase.auth.GoogleAuthProvider.getCredential(firebase._googleSignInIdToken, null);
800+
801+
} else if (arg.type === firebase.LoginType.FACEBOOK) {
802+
if (!firebase._facebookAccessToken) {
803+
reject("Not currently logged in with Facebook");
804+
return;
805+
}
806+
authCredential = com.google.firebase.auth.FacebookAuthProvider.getCredential(firebase._facebookAccessToken);
807+
}
808+
809+
if (authCredential === null) {
810+
reject("arg.type should be one of LoginType.PASSWORD | LoginType.GOOGLE | LoginType.FACEBOOK");
811+
return;
812+
}
813+
814+
var onCompleteListener = new com.google.android.gms.tasks.OnCompleteListener({
815+
onComplete: function (task) {
816+
if (task.isSuccessful()) {
817+
resolve();
818+
} else {
819+
// TODO extract error
820+
reject("Reathentication failed");
821+
}
822+
}
823+
});
824+
user.reauthenticate(authCredential).addOnCompleteListener(onCompleteListener);
825+
826+
} catch (ex) {
827+
console.log("Error in firebase.reauthenticate: " + ex);
828+
reject(ex);
829+
}
830+
});
831+
};
832+
775833
firebase.resetPassword = function (arg) {
776834
return new Promise(function (resolve, reject) {
777835
try {

firebase.ios.js

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var platform = require("platform");
99
firebase._messagingConnected = null;
1010
firebase._pendingNotifications = [];
1111
firebase._receivedPushTokenCallback = null;
12+
firebase._gIDAuthentication = null;
1213

1314
firebase._addObserver = function (eventName, callback) {
1415
var queue = utils.ios.getter(NSOperationQueue, NSOperationQueue.mainQueue);
@@ -865,8 +866,8 @@ firebase.login = function (arg) {
865866
var delegate = GIDSignInDelegateImpl.new().initWithCallback(function (user, error) {
866867
if (error === null) {
867868
// Get a Google ID token and Google access token from the GIDAuthentication object and exchange them for a Firebase credential
868-
var authentication = user.authentication;
869-
var fIRAuthCredential = FIRGoogleAuthProvider.credentialWithIDTokenAccessToken(authentication.idToken, authentication.accessToken);
869+
firebase._gIDAuthentication = user.authentication;
870+
var fIRAuthCredential = FIRGoogleAuthProvider.credentialWithIDTokenAccessToken(firebase._gIDAuthentication.idToken, firebase._gIDAuthentication.accessToken);
870871

871872
// Finally, authenticate with Firebase using the credential
872873
if (fAuth.currentUser) {
@@ -903,6 +904,67 @@ firebase.login = function (arg) {
903904
});
904905
};
905906

907+
firebase.reauthenticate = function (arg) {
908+
return new Promise(function (resolve, reject) {
909+
try {
910+
var fAuth = FIRAuth.auth();
911+
if (fAuth === null) {
912+
reject("Run init() first!");
913+
return;
914+
}
915+
916+
var user = fAuth.currentUser;
917+
if (user === null) {
918+
reject("no current user");
919+
return;
920+
}
921+
922+
var authCredential = null;
923+
if (arg.type === firebase.LoginType.PASSWORD) {
924+
if (!arg.email || !arg.password) {
925+
reject("Auth type emailandpassword requires an email and password argument");
926+
} else {
927+
authCredential = FIREmailPasswordAuthProvider.credentialWithEmailPassword(arg.email, arg.password);
928+
}
929+
930+
} else if (arg.type === firebase.LoginType.GOOGLE) {
931+
if (!firebase._gIDAuthentication) {
932+
reject("Not currently logged in with Google");
933+
return;
934+
}
935+
authCredential = FIRGoogleAuthProvider.credentialWithIDTokenAccessToken(firebase._gIDAuthentication.idToken, firebase._gIDAuthentication.accessToken);
936+
937+
} else if (arg.type === firebase.LoginType.FACEBOOK) {
938+
var currentAccessToken = FBSDKAccessToken.currentAccessToken();
939+
if (!currentAccessToken) {
940+
reject("Not currently logged in with Facebook");
941+
return;
942+
}
943+
authCredential = FIRFacebookAuthProvider.credentialWithAccessToken(currentAccessToken.tokenString);
944+
}
945+
946+
if (authCredential === null) {
947+
reject("arg.type should be one of LoginType.PASSWORD | LoginType.GOOGLE | LoginType.FACEBOOK");
948+
return;
949+
}
950+
951+
var onCompletion = function(error) {
952+
if (error) {
953+
reject(error.localizedDescription);
954+
955+
} else {
956+
resolve();
957+
}
958+
};
959+
user.reauthenticateWithCredentialCompletion(authCredential, onCompletion);
960+
961+
} catch (ex) {
962+
console.log("Error in firebase.reauthenticate: " + ex);
963+
reject(ex);
964+
}
965+
});
966+
};
967+
906968
firebase.resetPassword = function (arg) {
907969
return new Promise(function (resolve, reject) {
908970
try {

index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ export interface LoginOptions {
191191
scope?: string[];
192192
}
193193

194+
export interface ReauthenticateOptions {
195+
type: LoginType;
196+
email?: string;
197+
password?: string;
198+
}
199+
194200
/**
195201
* The returned object from the login function.
196202
*/
@@ -520,6 +526,8 @@ export module analytics {
520526
// Auth
521527
export function login(options: LoginOptions): Promise<User>;
522528

529+
export function reauthenticate(options: ReauthenticateOptions): Promise<any>;
530+
523531
export function getAuthToken(option: GetAuthTokenOptions): Promise<string>;
524532

525533
export function logout(): Promise<any>;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-plugin-firebase",
3-
"version": "3.8.5",
3+
"version": "3.9.0-dev",
44
"description": "Fire. Base. Firebase!",
55
"main": "firebase",
66
"typings": "index.d.ts",

platforms/android/include.gradle

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ repositories {
1414

1515
dependencies {
1616
// make sure you have these versions by updating your local Android SDK's (Android Support repo and Google repo)
17-
compile "com.google.firebase:firebase-core:9.8.+"
18-
compile "com.google.firebase:firebase-database:9.8.+"
19-
compile "com.google.firebase:firebase-auth:9.8.+"
20-
compile "com.google.firebase:firebase-crash:9.8.+"
17+
compile "com.google.firebase:firebase-core:10.0.+"
18+
compile "com.google.firebase:firebase-database:10.0.+"
19+
compile "com.google.firebase:firebase-auth:10.0.+"
20+
compile "com.google.firebase:firebase-crash:10.0.+"
2121

2222
// for reading google-services.json and configuration
23-
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : '9.8.+'
23+
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : '10.0.+'
2424
compile "com.google.android.gms:play-services-base:$googlePlayServicesVersion"
2525

2626
// Uncomment if you want to use 'Remote Config'
27-
// compile "com.google.firebase:firebase-config:9.8.+"
27+
// compile "com.google.firebase:firebase-config:10.0.+"
2828

2929
// Uncomment if you want FCM (Firebase Cloud Messaging)
30-
// compile "com.google.firebase:firebase-messaging:9.8.+"
30+
// compile "com.google.firebase:firebase-messaging:10.0.+"
3131

3232
// Uncomment if you want Google Cloud Storage
33-
// compile 'com.google.firebase:firebase-storage:9.8.+'
33+
// compile 'com.google.firebase:firebase-storage:10.0.+'
3434

3535
// Uncomment if you need Facebook Authentication
3636
// compile "com.facebook.android:facebook-android-sdk:4.+"
3737

3838
// Uncomment if you need Google Sign-In Authentication
39-
// compile "com.google.android.gms:play-services-auth:9.8.+"
39+
// compile "com.google.android.gms:play-services-auth:10.0.+"
4040

4141
}
4242

platforms/ios/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
pod 'Firebase', '~> 3.9.0'
2+
pod 'Firebase', '~> 3.11.0'
33
pod 'Firebase/Database'
44
pod 'Firebase/Auth'
55
pod 'Firebase/Crash'

scripts/installer.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ function writePodFile(result) {
191191
}
192192
try {
193193
fs.writeFileSync(directories.ios + '/Podfile',
194-
`pod 'Firebase', '~> 3.9.0'
194+
`pod 'Firebase', '~> 3.11.0'
195195
pod 'Firebase/Database'
196196
pod 'Firebase/Auth'
197197
pod 'Firebase/Crash'
@@ -245,29 +245,29 @@ repositories {
245245
246246
dependencies {
247247
// make sure you have these versions by updating your local Android SDK's (Android Support repo and Google repo)
248-
compile "com.google.firebase:firebase-core:9.8.+"
249-
compile "com.google.firebase:firebase-database:9.8.+"
250-
compile "com.google.firebase:firebase-auth:9.8.+"
251-
compile "com.google.firebase:firebase-crash:9.8.+"
248+
compile "com.google.firebase:firebase-core:10.0.+"
249+
compile "com.google.firebase:firebase-database:10.0.+"
250+
compile "com.google.firebase:firebase-auth:10.0.+"
251+
compile "com.google.firebase:firebase-crash:10.0.+"
252252
253253
// for reading google-services.json and configuration
254-
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : '9.8.+'
254+
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : '10.0.+'
255255
compile "com.google.android.gms:play-services-base:$googlePlayServicesVersion"
256256
257257
// Uncomment if you want to use 'Remote Config'
258-
` + (isSelected(result.remote_config) ? `` : `//`) + ` compile "com.google.firebase:firebase-config:9.8.+"
258+
` + (isSelected(result.remote_config) ? `` : `//`) + ` compile "com.google.firebase:firebase-config:10.0.+"
259259
260260
// Uncomment if you want FCM (Firebase Cloud Messaging)
261-
` + (isSelected(result.messaging) ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:9.8.+"
261+
` + (isSelected(result.messaging) ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:10.0.+"
262262
263263
// Uncomment if you want Google Cloud Storage
264-
` + (isSelected(result.storage) ? `` : `//`) + ` compile 'com.google.firebase:firebase-storage:9.8.+'
264+
` + (isSelected(result.storage) ? `` : `//`) + ` compile 'com.google.firebase:firebase-storage:10.0.+'
265265
266266
// Uncomment if you need Facebook Authentication
267267
` + (isSelected(result.facebook_auth) ? `` : `//`) + ` compile "com.facebook.android:facebook-android-sdk:4.+"
268268
269269
// Uncomment if you need Google Sign-In Authentication
270-
` + (isSelected(result.google_auth) ? `` : `//`) + ` compile "com.google.android.gms:play-services-auth:9.8.+"
270+
` + (isSelected(result.google_auth) ? `` : `//`) + ` compile "com.google.android.gms:play-services-auth:10.0.+"
271271
272272
}
273273

0 commit comments

Comments
 (0)