Custom Authentication Provider Fails in auth_backend
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.
Duplicate issue. Will be fixed in next release, see 286
Thanks @rockwotj.
Is this merged? can you please put this in the docs?