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

Commit df8a79d

Browse files
#157 Cannot decode refreshToken using JWT
2 parents 45c75d0 + aebae70 commit df8a79d

File tree

4 files changed

+114
-41
lines changed

4 files changed

+114
-41
lines changed

docs/AUTHENTICATION.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,25 @@ Then add the following lines to your code and check for setup instructions for y
279279
1. If you didn't choose this feature during installation you can uncomment `google-services-auth` in `node_modules\nativescript-plugin-firebase\platforms\android\include.gradle`
280280
2. Google Sign-In requires an SHA1 fingerprint: see [Authenticating Your Client for details](https://developers.google.com/android/guides/client-auth). If you don't do this you will see the account selection popup, but you won't be able to actually sign in.
281281

282+
### getAuthToken
283+
If you want to authenticate your user from your backend server you can obtain
284+
a Firebase auth token for the currently logged in user.
285+
286+
```js
287+
firebase.getAuthToken({
288+
// default false, not recommended to set to true by Firebase but exposed for {N} devs nonetheless :)
289+
forceRefresh: false
290+
}).then(
291+
function (token) {
292+
console.log("Auth token retrieved: " + token);
293+
},
294+
function (errorMessage) {
295+
console.log("Auth token retrieval error: " + errorMessage);
296+
}
297+
);
298+
299+
```
300+
282301
### logout
283302
Shouldn't be more complicated than:
284303

firebase.android.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ firebase.init = function (arg) {
209209
}
210210

211211
// Facebook
212-
if (typeof(com.facebook) !== "undefined") {
212+
if (typeof(com.facebook) !== "undefined" && typeof(com.facebook.FacebookSdk) !== "undefined") {
213213
com.facebook.FacebookSdk.sdkInitialize(com.tns.NativeScriptApplication.getInstance());
214214
fbCallbackManager = com.facebook.CallbackManager.Factory.create();
215215
appModule.android.on(appModule.AndroidApplication.activityResultEvent, function(eventData){
@@ -497,6 +497,43 @@ firebase.logout = function (arg) {
497497
});
498498
};
499499

500+
firebase.getAuthToken = function (arg) {
501+
return new Promise(function (resolve, reject) {
502+
try {
503+
if (firebase.instance === null) {
504+
reject("Run init() first!");
505+
return;
506+
}
507+
508+
var firebaseAuth = com.google.firebase.auth.FirebaseAuth.getInstance();
509+
var user = firebaseAuth.getCurrentUser();
510+
if (user !== null) {
511+
var onSuccessListener = new com.google.android.gms.tasks.OnSuccessListener({
512+
onSuccess: function(getTokenResult) {
513+
resolve(getTokenResult.getToken());
514+
}
515+
});
516+
517+
var onFailureListener = new com.google.android.gms.tasks.OnFailureListener({
518+
onFailure: function (exception) {
519+
reject(exception);
520+
}
521+
});
522+
523+
user.getToken(arg.forceRefresh)
524+
.addOnSuccessListener(onSuccessListener)
525+
.addOnFailureListener(onFailureListener);
526+
527+
} else {
528+
reject("Log in first");
529+
}
530+
} catch (ex) {
531+
console.log("Error in firebase.getAuthToken: " + ex);
532+
reject(ex);
533+
}
534+
});
535+
};
536+
500537
function toLoginResult(user) {
501538
if (user === null) {
502539
return false;
@@ -508,7 +545,6 @@ function toLoginResult(user) {
508545
email: user.getEmail(),
509546
// expiresAtUnixEpochSeconds: authData.getExpires(),
510547
profileImageURL: user.getPhotoUrl() ? user.getPhotoUrl().toString() : null
511-
// token: user.getToken() // can be used to auth with a backend server
512548
};
513549
}
514550

@@ -769,7 +805,7 @@ firebase.createUser = function (arg) {
769805
reject("Creating a user failed. " + (task.getException() && task.getException().getReason ? task.getException().getReason() : task.getException()));
770806
} else {
771807
var user = task.getResult().getUser();
772-
resolve(toLoginResult(user));
808+
resolve({key: user.getUid()});
773809
}
774810
}
775811
});

firebase.ios.js

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,18 @@ firebase.addAppDelegateMethods = function(appDelegate) {
4242
var result = false;
4343
if (typeof(FBSDKApplicationDelegate) !== "undefined") {
4444
result = FBSDKApplicationDelegate.sharedInstance().applicationOpenURLSourceApplicationAnnotation(
45-
application,
46-
url,
47-
options.valueForKey(UIApplicationOpenURLOptionsSourceApplicationKey),
48-
options.valueForKey(UIApplicationOpenURLOptionsAnnotationKey));
45+
application,
46+
url,
47+
options.valueForKey(UIApplicationOpenURLOptionsSourceApplicationKey),
48+
options.valueForKey(UIApplicationOpenURLOptionsAnnotationKey));
4949
}
5050
// for iOS >= 9
5151
result = result || GIDSignIn.sharedInstance().handleURLSourceApplicationAnnotation(
52-
url,
53-
options.valueForKey(UIApplicationOpenURLOptionsSourceApplicationKey),
54-
options.valueForKey(UIApplicationOpenURLOptionsAnnotationKey));
52+
url,
53+
options.valueForKey(UIApplicationOpenURLOptionsSourceApplicationKey),
54+
options.valueForKey(UIApplicationOpenURLOptionsAnnotationKey));
5555
return result;
5656
};
57-
58-
// Untested, so commented out
59-
/*
60-
appDelegate.prototype.signDidDisconnectWithUserWithError = function (signIn, user, error) {
61-
if (error === null) {
62-
console.log("--- OK in signDidDisconnectWithUserWithError");
63-
} else {
64-
console.log("--- error in signDidDisconnectWithUserWithError: " + error.localizedDescription);
65-
}
66-
};
67-
*/
6857
}
6958

7059
// making this conditional to avoid http://stackoverflow.com/questions/37428539/firebase-causes-issue-missing-push-notification-entitlement-after-delivery-to ?
@@ -219,16 +208,16 @@ firebase._onTokenRefreshNotification = function (notification) {
219208
firebase.addAppDelegateMethods(application.ios.delegate);
220209
} else {
221210
var __extends = this.__extends || function (d, b) {
222-
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
223-
function __() { this.constructor = d; }
224-
__.prototype = b.prototype;
225-
d.prototype = new __();
226-
};
211+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
212+
function __() { this.constructor = d; }
213+
__.prototype = b.prototype;
214+
d.prototype = new __();
215+
};
227216

228217
var appDelegate = (function (_super) {
229218
__extends(appDelegate, _super);
230219
function appDelegate() {
231-
_super.apply(this, arguments);
220+
_super.apply(this, arguments);
232221
}
233222
firebase.addAppDelegateMethods(appDelegate);
234223
appDelegate.ObjCProtocols = [UIApplicationDelegate];
@@ -330,8 +319,8 @@ firebase.init = function (arg) {
330319
if (arg.onAuthStateChanged) {
331320
firebase.authStateListener = function(auth, user) {
332321
arg.onAuthStateChanged({
333-
loggedIn: user !== null,
334-
user: toLoginResult(user)
322+
loggedIn: user !== null,
323+
user: toLoginResult(user)
335324
});
336325
};
337326
FIRAuth.auth().addAuthStateDidChangeListener(firebase.authStateListener);
@@ -341,8 +330,8 @@ firebase.init = function (arg) {
341330
if (!firebase.authStateListener) {
342331
firebase.authStateListener = function(auth, user) {
343332
firebase.notifyAuthStateListeners({
344-
loggedIn: user !== null,
345-
user: toLoginResult(user)
333+
loggedIn: user !== null,
334+
user: toLoginResult(user)
346335
});
347336
};
348337
FIRAuth.auth().addAuthStateDidChangeListener(firebase.authStateListener);
@@ -560,6 +549,35 @@ function toLoginResult(user) {
560549
};
561550
}
562551

552+
firebase.getAuthToken = function (arg) {
553+
return new Promise(function (resolve, reject) {
554+
try {
555+
var fAuth = FIRAuth.auth();
556+
if (fAuth === null) {
557+
reject("Run init() first!");
558+
return;
559+
}
560+
561+
var user = fAuth.currentUser;
562+
if (user) {
563+
var onCompletion = function(token, error) {
564+
if (error) {
565+
reject(error.localizedDescription);
566+
} else {
567+
resolve(token);
568+
}
569+
};
570+
user.getTokenForcingRefreshCompletion(arg.forceRefresh, onCompletion);
571+
} else {
572+
reject("Log in first");
573+
}
574+
} catch (ex) {
575+
console.log("Error in firebase.getAuthToken: " + ex);
576+
reject(ex);
577+
}
578+
});
579+
};
580+
563581
firebase.login = function (arg) {
564582
return new Promise(function (resolve, reject) {
565583

@@ -600,14 +618,14 @@ firebase.login = function (arg) {
600618
fAuth.signInWithCustomToken(arg.token, onCompletion);
601619
} else if (arg.tokenProviderFn) {
602620
arg.tokenProviderFn()
603-
.then(
604-
function (token) {
605-
firebaseAuth.signInWithCustomToken(arg.token, onCompletion);
606-
},
607-
function (error) {
608-
reject(error);
609-
}
610-
);
621+
.then(
622+
function (token) {
623+
firebaseAuth.signInWithCustomToken(arg.token, onCompletion);
624+
},
625+
function (error) {
626+
reject(error);
627+
}
628+
);
611629
}
612630

613631
} else if (arg.type === firebase.LoginType.FACEBOOK) {
@@ -791,7 +809,7 @@ firebase.createUser = function (arg) {
791809
reject(error.localizedDescription);
792810
} else {
793811
resolve({
794-
key: user.uid // firebase.toJsObject(authData).uid
812+
key: user.uid
795813
});
796814
}
797815
};

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.6.4",
3+
"version": "3.7.0",
44
"description" : "Fire. Base. Firebase!",
55
"main" : "firebase.js",
66
"typings": "firebase.d.ts",

0 commit comments

Comments
 (0)