Angularfire: How can I get userToken?

Created on 21 Dec 2016  路  5Comments  路  Source: angular/angularfire

Version info

"angularfire2": "^2.0.0-beta.6"

AngularFire:

Interface not found.

firebase.auth.currentUser.getToken()

Expected behavior

I want to get User token from user object.

````
let userToken = firebase.auth.currentUser.getToken();

let userToken = firebase.User.getToken();

let userToken = firebase.UserInfo.getToken();
````

Actual behavior

interface not found.

Thanks

Most helpful comment

@nothing2lose Hellou i think this is better:

You can use this:

JSON.parse(JSON.stringify(this.afAuth.auth.currentUser)).stsTokenManager.accessToken

You need to parse this.afAuth.auth.currentUser to can acces to accesToken:
My variable afAuth is:

public afAuth: AngularFireAuth

Example data output:

{
  "uid": "i3z0bkWWLieqZB69rVTeI5o5wxk2",
  "displayName": "David Garcia",
  "photoURL": "https://lh5.googleusercontent.com/-aBVy-QeZmes/AAAAAAAAAAI/AAAAAAAABWw/R4IQMv5B8lE/photo.jpg",
  "email": "[email protected]",
  "emailVerified": true,
  "phoneNumber": null,
  "isAnonymous": false,
  "providerData": [
    {
      "uid": "113831236375587749692",
      "displayName": "David Garcia",
      "photoURL": "https://lh5.googleusercontent.com/-aBVy-QeZmes/AAAAAAAAAAI/AAAAAAAABWw/R4IQMv5B8lE/photo.jpg",
      "email": "[email protected]",
      "phoneNumber": null,
      "providerId": "google.com"
    }
  ],
  "apiKey": "AIzaSyDciKMsZaNEF4af0Ixfu0ooA7RE_hxRT7s",
  "appName": "[DEFAULT]",
  "authDomain": "blancineg.firebaseapp.com",
  "stsTokenManager": {
    "apiKey": "AIzaSyDciKMsZaNEF4af0Ixfu0ooA7RE_hxRT7s",
    "refreshToken": "AK2wQ-wO85W3TZvhwT4LJqPgcy9bFdbbd6RAtKHqxZB5hjmHYgqj5Qh6RSTMRtta2wsjFC2CsMnVwXHbYvvWrKHjPi3r9zaKmhH7s0fUKTFjwr8869oCjCwbPUI1HbAX5zEK6CSknmadeODXokf20uXYIzJlngBgZgbFnmDbObti82twubSsRCEJwLTNj3eOqxHkN3d2NKcHQv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0qdP7jCitCfMV02NAcuUhH1WPohbzRAUpAa0zhwwgEOZp1GcO0ByyYRMlD_NczcMWErk1vu3gBKSw9SCQex-oxTW0zKm01UKfihBpbJWQ943oqk4eYKUfDH1HL0TDCqeqvxdP1e9hjNUy7XOcv2WotKDOQs_iz5G-ROhyYSiI5F07liN0xbl8kt0R3q0WrgOrY9kP1CpV6On8F87g5cNNY999Dt3_8jj-o2Lk_kb1zPmg1w",
    "expirationTime": 1525796735790
  },
  "redirectEventId": null,
  "lastLoginAt": "1525793136000",
  "createdAt": "1524859209000"
}

All 5 comments

Try firebase.auth().currentUser as described in the docs. Not an AngularFire bug or feature request.

ok thnaks you for your answer. but could you provide a snippet how to call this method in angular fire or angular 2. I use angular 2 in frontend and I don't now how to do it.

I solve this problem like a below.

Please let me know is correct way?

````
// authService.ts

...
import { Injectable, Inject } from '@angular/core';
import { FirebaseApp, FirebaseAuth, AngularFire, AuthProviders, AuthMethods } from 'angularfire2';
declare var firebase : any;

@Injectable()
export class AuthService {
firebaseApp: any;

constructor(
private http: Http,
public af: AngularFire,
public auth: FirebaseAuth,
@Inject(FirebaseApp) firebaseApp: any
) {
this.firebaseApp = firebaseApp; // firebase
}

something(): void {
this.firebaseApp.auth();
}
}
````

how do you send (post) it to your backend (I use djang rest framework as backend)?
And if you can, could you explain how you redirect in angular after authenticating?

@nothing2lose Hellou i think this is better:

You can use this:

JSON.parse(JSON.stringify(this.afAuth.auth.currentUser)).stsTokenManager.accessToken

You need to parse this.afAuth.auth.currentUser to can acces to accesToken:
My variable afAuth is:

public afAuth: AngularFireAuth

Example data output:

{
  "uid": "i3z0bkWWLieqZB69rVTeI5o5wxk2",
  "displayName": "David Garcia",
  "photoURL": "https://lh5.googleusercontent.com/-aBVy-QeZmes/AAAAAAAAAAI/AAAAAAAABWw/R4IQMv5B8lE/photo.jpg",
  "email": "[email protected]",
  "emailVerified": true,
  "phoneNumber": null,
  "isAnonymous": false,
  "providerData": [
    {
      "uid": "113831236375587749692",
      "displayName": "David Garcia",
      "photoURL": "https://lh5.googleusercontent.com/-aBVy-QeZmes/AAAAAAAAAAI/AAAAAAAABWw/R4IQMv5B8lE/photo.jpg",
      "email": "[email protected]",
      "phoneNumber": null,
      "providerId": "google.com"
    }
  ],
  "apiKey": "AIzaSyDciKMsZaNEF4af0Ixfu0ooA7RE_hxRT7s",
  "appName": "[DEFAULT]",
  "authDomain": "blancineg.firebaseapp.com",
  "stsTokenManager": {
    "apiKey": "AIzaSyDciKMsZaNEF4af0Ixfu0ooA7RE_hxRT7s",
    "refreshToken": "AK2wQ-wO85W3TZvhwT4LJqPgcy9bFdbbd6RAtKHqxZB5hjmHYgqj5Qh6RSTMRtta2wsjFC2CsMnVwXHbYvvWrKHjPi3r9zaKmhH7s0fUKTFjwr8869oCjCwbPUI1HbAX5zEK6CSknmadeODXokf20uXYIzJlngBgZgbFnmDbObti82twubSsRCEJwLTNj3eOqxHkN3d2NKcHQvNCNAKvS0Cut2zctslDOSCDWSH1tdLoNscVimRGkg8B8AZM2VfcalfyVeayhi183wG_0bQ1xv69yscQSgxQHC4pZXZvWWnEmdbnZzhKqVWKfwUTqMIWeizNdz71TJ9oVWoL2bGaHEkBJgjVwEoRjMJnjLwyv0ALXWe9QTSr324",
    "accessToken": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImQ5NGQ1ZjMyZTE4NmRjMWUxNjA0MjhiZDdhODE1NDI2ZjI3NDg4MmIifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vYmxhbmNpbmVnIiwibmFtZSI6IkRhdmlkIEdhcmNpYSIsInBpY3R1cmUiOiJodHRwczovL2xoNS5nb29nbGV1c2VyY29udGVudC5jb20vLWFCVnktUWVabWVzL0FBQUFBQUFBQUFJL0FBQUFBQUFBQld3L1I0SVFNdjVCOGxFL3Bob3RvLmpwZyIsImF1ZCI6ImJsYW5jaW5lZyIsImF1dGhfdGltZSI6MTUyNTc5MzEzNiwidXNlcl9pZCI6ImkzejBia1dXTGllcVpCNjlyVlRlSTVvNXd4azIiLCJzdWIiOiJpM3owYmtXV0xpZXFaQjY5clZUZUk1bzV3eGsyIiwiaWF0IjoxNTI1NzkzMTM2LCJleHAiOjE1MjU3OTY3MzYsImVtYWlsIjoiZGF2aWRnYXJjaWFzYW50ZXNAZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImZpcmViYXNlIjp7ImlkZW50aXRpZXMiOnsiZ29vZ2xlLmNvbSI6WyIxMTM4MzEyMzYzNzU1ODc3NDk2OTIiXSwiZW1haWwiOlsiZGF2aWRnYXJjaWFzYW50ZXNAZ21haWwuY29tIl19LCJzaWduX2luX3Byb3ZpZGVyIjoiZ29vZ2xlLmNvbSJ9fQ.KedsM1G4YOrYa-aCdAK7Ihv8m6PuWwowesyyZuRnGB3sZQMBGE1jASf5GOvvJhpzsFYjM6fO_Vu7Cspvbbu7hrtzRhiPVShecwx8oNG0qdP7jCitCfMV02NAcuUhH1WPohbzRAUpAa0zhwwgEOZp1GcO0ByyYRMlD_NczcMWErk1vu3gBKSw9SCQex-oxTW0zKm01UKfihBpbJWQ943oqk4eYKUfDH1HL0TDCqeqvxdP1e9hjNUy7XOcv2WotKDOQs_iz5G-ROhyYSiI5F07liN0xbl8kt0R3q0WrgOrY9kP1CpV6On8F87g5cNNY999Dt3_8jj-o2Lk_kb1zPmg1w",
    "expirationTime": 1525796735790
  },
  "redirectEventId": null,
  "lastLoginAt": "1525793136000",
  "createdAt": "1524859209000"
}
Was this page helpful?
0 / 5 - 0 ratings