i did an auth on my site, it works fine, but i want to handle errors, for testing i caused one for this purpose with this return from firebase: "An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address.". Im trying to write an RXjs observable to do that, with the following code:
private subscribleAuth() {
this.af.auth
.subscribe(
auth => {
this._auth = auth && auth.auth ? auth.auth : null;
if (this._auth) {
this.loginState = 'logged';
} else {
this.loginState = 'login';
}
},
err => {
console.log('error'),
this.loginState = 'error';
},
() => { console.log('done.') }
);
}
My console never show the 'error' or 'done', so anything i put inside the err code doesn't work.
note: I know what causes that firebase return, i just want to know how treat it and why the code i wrote isn't worked as expected, when no errors are returned the site works as expected with the same observable.
I'm using angularfire2: ^2.0.0-beta.5 with Angular 2.0.0 final
EDIT 2: reported as an issue and maybe it's really a bug, gonna keep this post updated. The workarround by now is using this code written by yashmurty on github:
import { AngularFire, AuthProviders } from 'angularfire2';
import { AuthBackend } from 'angularfire2/auth/auth_backend';
export class SocialmenuComponent {
constructor(public af: AngularFire, private _authBackend: AuthBackend) {
// this.af.auth.subscribe(
this._authBackend.getRedirectResult().subscribe(
(auth) => {
if(auth) {
console.log(auth);
} else {
console.log('User Not Logged In');
}
},
(err) => {
if(err) {
console.log('Error in auth.subscribe : ');
console.log(err);
} else {
console.log('No Error detected in auth.subscribe');
}
},
() => { console.log('done.') }
);
}
}
Any errors that occur when calling the createUser
or login
methods of the AngularFire2 auth
observable are reported via the returned promises, not via the observable.
For example:
import { AngularFire } from 'angularfire2';
...
class MyComponent {
constructor(af: AngularFire) {
af.auth.createUser({
email: '[email protected]',
password: 'somepassword'
})
.then((authState) => { console.log('success'); })
.catch((error) => { console.log('failure'); });
}
If authenticating using a method that involves a redirect, you can do something like this (jeffbcross solution from github):
import { FirebaseApp } from 'angularfire2';
import * as firebase from 'firebase';
...
class MyComponent {
constructor(@Inject(FirebaseApp) fbApp: firebase.App) {
fbApp.auth().getRedirectResult()
.then((userCredential) => { console.log('success'); })
.catch((error) => { console.log('failure'); });
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments