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

Commit 7604907

Browse files
#153 [ANDROID-BUG] Getting firebase token?
1 parent f51f1ce commit 7604907

File tree

5 files changed

+120
-41
lines changed

5 files changed

+120
-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: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,43 @@ firebase.logout = function (arg) {
473473
});
474474
};
475475

476+
firebase.getAuthToken = function (arg) {
477+
return new Promise(function (resolve, reject) {
478+
try {
479+
if (firebase.instance === null) {
480+
reject("Run init() first!");
481+
return;
482+
}
483+
484+
var firebaseAuth = com.google.firebase.auth.FirebaseAuth.getInstance();
485+
var user = firebaseAuth.getCurrentUser();
486+
if (user !== null) {
487+
var onSuccessListener = new com.google.android.gms.tasks.OnSuccessListener({
488+
onSuccess: function(getTokenResult) {
489+
resolve(getTokenResult.getToken());
490+
}
491+
});
492+
493+
var onFailureListener = new com.google.android.gms.tasks.OnFailureListener({
494+
onFailure: function (exception) {
495+
reject(exception);
496+
}
497+
});
498+
499+
user.getToken(arg.forceRefresh)
500+
.addOnSuccessListener(onSuccessListener)
501+
.addOnFailureListener(onFailureListener);
502+
503+
} else {
504+
reject("Log in first");
505+
}
506+
} catch (ex) {
507+
console.log("Error in firebase.getAuthToken: " + ex);
508+
reject(ex);
509+
}
510+
});
511+
};
512+
476513
function toLoginResult(user) {
477514
if (user === null) {
478515
return false;
@@ -484,7 +521,6 @@ function toLoginResult(user) {
484521
email: user.getEmail(),
485522
// expiresAtUnixEpochSeconds: authData.getExpires(),
486523
profileImageURL: user.getPhotoUrl() ? user.getPhotoUrl().toString() : null
487-
// token: user.getToken() // can be used to auth with a backend server
488524
};
489525
}
490526

firebase.d.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ declare module "nativescript-firebase" {
5959

6060
export interface ServerValue {
6161
/**
62-
* When for instance using setValue you can set a timestamp property to this placeholder value.
63-
* Example:
64-
* updateTs: firebase.ServerValue.TIMESTAMP
62+
* TODO
6563
*/
6664
TIMESTAMP: any;
6765
}
@@ -142,6 +140,13 @@ declare module "nativescript-firebase" {
142140
singleEvent?: boolean;
143141
}
144142

143+
export interface GetAuthTokenOptions {
144+
/**
145+
* Default false.
146+
*/
147+
forceRefresh?: boolean;
148+
}
149+
145150
/**
146151
* The options object passed into the login function.
147152
*/
@@ -476,6 +481,7 @@ declare module "nativescript-firebase" {
476481

477482
// Auth
478483
export function login(options: LoginOptions): Promise<User>;
484+
export function getAuthToken(option: GetAuthTokenOptions): Promise<string>;
479485
export function logout(): Promise<any>;
480486
export function createUser(options: CreateUserOptions): Promise<CreateUserResult>;
481487
export function deleteUser(): Promise<any>;

firebase.ios.js

Lines changed: 54 additions & 36 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) {

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

0 commit comments

Comments
 (0)