Kevin
Kevin

Reputation: 895

AngularFire Authentication Access to Google AccessToken

Using the Firebase Web SDK, you can reference the AccessToken as follows:

const provider = new auth.GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/plus.login');

firebase.auth().signInWithPopup(provider).then(function(result) {
  // This gives you a Google Access Token. You can use it to access the Google API.
  var token = result.credential.accessToken;
  // The signed-in user info.
  var user = result.user;
  // ...
});

You can subsequently use the token to get more information about your user; for example:

public getFirstName(token: string): Observable<string> {
  const url = 'https://www.googleapis.com/oauth2/v3/userinfo?access_token=' + token;

  return this.http.get(url).map(response => {
    return response.json().given_name;
  });
}

Question: How do you retrieve this access token using the AngularFire2 protocol?

public loginGoogle() {
  this.af.auth.login().then((authState) => {
    authState.WHERE_IS_IT; // <<<----- 
  })
}

Thanks in advance! // Kevin

Upvotes: 1

Views: 403

Answers (1)

cartant
cartant

Reputation: 58400

The implementation of login calls the authentication backend (the SDK) and passes the received UserCredential to authDataToAuthState - with the credential passed as the providerData.

authDataToAuthState has a switch statement that maps the credential to a provider-dependent property. For google.com, the credential should be stored in the google property:

case 'google.com':
  authState.google = providerData; // i.e. the credential
  authState.provider = AuthProviders.Google;
  break;

Note that the authentication methods are going to be removed from AngularFire2 for the release candidate. There is a discussion regarding that here. Once removed, the SDK is to be used directly for signing in, etc. - which, hopefully, will make things less confusing.

Upvotes: 1

Related Questions