angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Custom Authentication Provider Fails in auth_backend

Open clarkmalmgren opened this issue 10 years ago • 3 comments

The Problem:

When exchanging custom token, the array of providers returned is empty and thus looking up the providerId of the first provider ends up throwing an error.

browser_adapter.ts:78 TypeError: Cannot read property 'providerId' of undefined
    at Object.authDataToAuthState (auth_backend.ts:83)
    at eval (firebase_sdk_auth_backend.ts:61)
    at ZoneDelegate.invoke (zone.js:323)
    at Object.onInvoke (ng_zone_impl.ts:67)
    at ZoneDelegate.invoke (zone.js:322)
    at Zone.run (zone.js:216)
    at zone.js:571
    at ZoneDelegate.invokeTask (zone.js:356)
    at Object.onInvokeTask (ng_zone_impl.ts:56)
    at ZoneDelegate.invokeTask (zone.js:355)

Background:

My application relies on a federated identity solution to provide authentication. As a result, to gain access to firebase, I simply exchange my bearer token with my own backend service which generates a custom token. I then feed this token into the angularfire auth method. My code looks like this:

  /**
   * Authenticate with firebase using a token obtained from the backend service. 
   */
  authByToken(): Observable<any> {
    return this.security.get(this.backendAuthUrl)
      .mergeMap((response) => {
        let token = response.json() as String;
        let config = { method : AuthMethods.CustomToken } as AuthConfiguration;

        return this.angularfire.auth.login(token, config);
      });
  }

I can workaround this by changing the providerId lookup code from

  if (authData.isAnonymous) {
    providerId = 'anonymous';
    authState.provider = AuthProviders.Anonymous;
    authState.anonymous = true;
    return authState;
  } else {
    providerId = authData.providerData[0].providerId;
  }

to

  if (authData.isAnonymous) {
    providerId = 'anonymous';
    authState.provider = AuthProviders.Anonymous;
    authState.anonymous = true;
    return authState;
  } else if (authData.providerData.length == 0) {
    providerId = 'custom';
  } else {
    providerId = authData.providerData[0].providerId;
  }

I am more than willing to submit a PR if that would help.

clarkmalmgren avatar Jul 20 '16 22:07 clarkmalmgren

Duplicate issue. Will be fixed in next release, see 286

rockwotj avatar Jul 21 '16 20:07 rockwotj

Thanks @rockwotj.

clarkmalmgren avatar Jul 21 '16 21:07 clarkmalmgren

Is this merged? can you please put this in the docs?

evanjmg avatar Feb 15 '17 15:02 evanjmg