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

[ANDROID-BUG] Getting firebase token? #153

Closed
IvRRimum opened this issue Oct 6, 2016 · 18 comments
Closed

[ANDROID-BUG] Getting firebase token? #153

IvRRimum opened this issue Oct 6, 2016 · 18 comments
Assignees

Comments

@IvRRimum
Copy link

IvRRimum commented Oct 6, 2016

How can i get the auth token from:

 57     this.ngZone.run(() => {
 58       firebase.getCurrentUser().then((data) => {
 59         this.currentUserId.next(data.uid);
 60       });
 61     });

I need the token, so i can send it to my backend and upload resized photos to firebase. Is there a better way to do this?

@xerotolerant
Copy link

xerotolerant commented Oct 6, 2016

You can access it using data.refreshToken.

this.ngZone.run(() => {
 58       firebase.getCurrentUser().then((data) => {
 59         this.currentUserId.next(data.uid);
               this.token = data.refreshToken;
 60       });
 61     });

@IvRRimum
Copy link
Author

IvRRimum commented Oct 7, 2016

@xerotolerant That is exactly what i need, but:

 app/views/header/header.component.ts(60,26): error TS2339: Property 'refreshToken' does not exist on type 'User'.

Versions:

 1 {
  2   "name": "nativescript-plugin-firebase",
  3   "version": "3.6.3",
  4   "description": "Fire. Base. Firebase!",

@IvRRimum
Copy link
Author

IvRRimum commented Oct 7, 2016

Okey, this is a bit weird. But the property refreshToken is there only in IOS. Not android.

There is this method in user.

getToken()

but when called:

JS: Error in firebase.getCurrentUser: Error: java.lang.Exception: Failed resolving method getToken on class com.google.firebase.auth.FirebaseUser
JS:     com.tns.Runtime.resolveMethodOverload(Runtime.java:816)
JS:     com.tns.Runtime.callJSMethodNative(Native Method)
JS:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:865)
JS:     com.tns.Runtime.callJSMethodImpl(Runtime.java:730)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:716)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:697)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:687)
JS:     com.tns.gen.com.google.android.gms.tasks.OnCompleteListener.onComplete(com.google.android.gms.tasks.OnCompleteListener.java)
JS:     com.google.android.gms.tasks.zzc$1.run(Unknown Source)
JS:     android.os.Handler.handleCallback(Handler.java:739)
JS:     android.os.Handler.dispatchMessage(Handler.java:95)
JS:     android.os.Looper.loop(Looper.java:135)
JS:     android.app.ActivityThread.main(ActivityThread.java:5753)
JS:     java.lang.reflect.Method.invoke(Native Method)
JS:     java.lang.reflect.Method.invoke(Method.java:372)
JS:     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
JS:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

@IvRRimum IvRRimum changed the title Getting firebase token? [ANDROID-BUG] Getting firebase token? Oct 7, 2016
@IvRRimum
Copy link
Author

IvRRimum commented Oct 7, 2016

@EddyVerbruggen Maybe you have ideas?

@xerotolerant
Copy link

What happens if you stringify the user object?
console.log(JSON.stringify(user));

can you see the token under another name or something?

@IvRRimum
Copy link
Author

IvRRimum commented Oct 7, 2016

@xerotolerant The getToken() methid is inside the getCurrentUser method inside the plugin. I will try console stringify it.

@IvRRimum
Copy link
Author

IvRRimum commented Oct 7, 2016

 427 firebase.getCurrentUser = function (arg) {                                                                                                  │
 428   return new Promise(function (resolve, reject) {                                                                                           │
 429     try {                                                                                                                                   │
 430       if (firebase.instance === null) {                                                                                                     │
 431         reject("Run init() first!");                                                                                                        │
 432         return;                                                                                                                             │
 433       }                                                                                                                                     │
 434                                                                                                                                             │
 435       var firebaseAuth = com.google.firebase.auth.FirebaseAuth.getInstance();                                                               │
 436       var user = firebaseAuth.getCurrentUser();                                                                                             │
 437       if (user !== null) {                                                                                                                  │
 438         resolve({                                                                                                                           │
 439           uid: user.getUid(),                                                                                                               │
 440           name: user.getDisplayName(),                                                                                                      │
 441           email: user.getEmail(),                                                                                                           │
 442           profileImageURL: user.getPhotoUrl() ? user.getPhotoUrl().toString() : null                                                        │
 443         });                                                                                                                                 │
 444       } else {                                                                                                                              │
 445         reject();                                                                                                                           │
 446       }                                                                                                                                     │
 447     } catch (ex) {                                                                                                                          │
 448       console.log("Error in firebase.getCurrentUser: " + ex);                                                                               │
 449       reject(ex);                                                                                                                           │
 450     }                                                                                                                                       │
 451   });                                                                                                                                       │
 452 };  

In this context. user has property getToken().

but whats weird:

 472 function toLoginResult(user) {                                                                                                              │
 473   if (user === null) {                                                                                                                      │
 474     return false;                                                                                                                           │
 475   }                                                                                                                                         │
 476                                                                                                                                             │
 477   return {                                                                                                                                  │
 478     uid: user.getUid(),                                                                                                                     │
 479     name: user.getDisplayName(),                                                                                                            │
 480     email: user.getEmail(),                                                                                                                 │
 481     // expiresAtUnixEpochSeconds: authData.getExpires(),                                                                                    │
 482     profileImageURL: user.getPhotoUrl() ? user.getPhotoUrl().toString() : null                                                              │
 483     // token: user.getToken() // can be used to auth with a backend server                                                                  │
 484   };                                                                                                                                        │
 485 }  

Comments the token out, this is a method called, when a login has been successful.

@EddyVerbruggen
Copy link
Owner

I'll take a look..

@EddyVerbruggen EddyVerbruggen self-assigned this Oct 7, 2016
@EddyVerbruggen
Copy link
Owner

I think it's best to add a new method (something like) getAuthToken with an optional forceRefresh property which gets a Firebase authentication token. The reason is that it's an async method on both platforms so it would delay the login promise. The iOS refreshToken will remain but (as documented by Firebase) that should only be used for "advanced scenario's".

Sounds good?

@EddyVerbruggen
Copy link
Owner

The problem was getToken is nowgetToken(forceRefresh:boolean), so that explains the error (the signature is incorrect).

The GitHub version now has a new getAuthToken:

firebase.getAuthToken({
  forceRefresh: false
}).then(
  function (token) {
    console.log("Auth token retrieved: " + token);
  },
  function (errorMessage) {
    console.log("Auth token retrieval error: " + errorMessage);
  }
);

Please give it a spin and I'll soon publish to npm as well.

@EddyVerbruggen
Copy link
Owner

Available in 3.6.4.

@IvRRimum
Copy link
Author

IvRRimum commented Oct 10, 2016

firebase.getAuthToken is not a function
var firebase = require("nativescript-plugin-firebase");

 67     firebase.getAuthToken({
 68       forceRefresh: false
 69     }).then(
 70       function (token) {
 71         console.log("Auth token retrieved: " + token);
 72       },
 73       function (errorMessage) {
 74         console.log("Auth token retrieval error: " + errorMessage);
 75       }
 76     );
  1 {
  2   "_args": [
  3     [
  4       {
  5         "raw": "nativescript-plugin-firebase@3.6.4",
  6         "scope": null,
  7         "escapedName": "nativescript-plugin-firebase",
  8         "name": "nativescript-plugin-firebase",
  9         "rawSpec": "3.6.4",
 10         "spec": "3.6.4",
 11         "type": "version"
 12       },

Still doesn't work on android.

@IvRRimum
Copy link
Author

Tryed removing the plugin and install again. Delete the platform folder. and reinstall the app. Still the same error. When i open node_modules/firebase.d.ts i can see the getAuthMethod()

@IvRRimum
Copy link
Author

the firebase.android.js file doesnt contain the getAuthToken method

@IvRRimum
Copy link
Author

I am calling console.log(user.getToken(false)); in firebase.android.js in the get getCurrentUser method.
I am getting this: JS: com.google.android.gms.tasks.zzh@3f76ac84 This is not the token...

@EddyVerbruggen
Copy link
Owner

All ok now?

@IvRRimum
Copy link
Author

Looks like it :)

On Wed, Oct 12, 2016 at 4:37 PM, Eddy Verbruggen notifications@github.com
wrote:

All ok now?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#153 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFrl1_cGBBQiDntqXn2p0mXnQ9vceBplks5qzOKTgaJpZM4KQOgh
.

@mnuckols
Copy link

I'm using the Facebook login on an Android emulator. When I call firebase.getAuthToken() it always returns the uid as the token instead of a JWT. Is that how it's supposed to work? How can I get a JWT to authenticate the user in my own web services?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants