Skip to content

Commit

Permalink
fix(auth): make sure onAuth runs in Angular zone
Browse files Browse the repository at this point in the history
There is one place in auth that takes a promise from Firebase,
the promise from getRedirectResult(), and creates an observable
from it. Since Firebase is using their own Promise implementation,
the Promise and its derived observable were running in the root
zone instead of the Angular zone, causing onAuth changes to not
immediately be propagated to the view. By casting the firebase
Promise to a zone-patched Promise, the observable now runs
in the Angular zone.

Fixes #231
  • Loading branch information
jeffbcross committed Jun 21, 2016
1 parent 68d711e commit d9a6ae7
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/providers/firebase_sdk_auth_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
}

createUser(creds: EmailPasswordCredentials): Promise<FirebaseAuthState> {
return <Promise<FirebaseAuthState>>this._fbAuth.createUserWithEmailAndPassword(creds.email, creds.password)
return Promise.resolve(this._fbAuth.createUserWithEmailAndPassword(creds.email, creds.password))
.then((user: firebase.User) => authDataToAuthState(user));
}

Expand All @@ -53,21 +53,21 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
}

unauth(): void {
this._fbAuth.signOut();
Promise.resolve(this._fbAuth.signOut());
}

authWithCustomToken(token: string): Promise<FirebaseAuthState> {
return <Promise<FirebaseAuthState>>this._fbAuth.signInWithCustomToken(token)
return Promise.resolve(this._fbAuth.signInWithCustomToken(token))
.then((user: firebase.User) => authDataToAuthState(user));
}

authAnonymously(): Promise<FirebaseAuthState> {
return <Promise<FirebaseAuthState>>this._fbAuth.signInAnonymously()
return Promise.resolve(this._fbAuth.signInAnonymously())
.then((user: firebase.User) => authDataToAuthState(user));
}

authWithPassword(creds: EmailPasswordCredentials): Promise<FirebaseAuthState> {
return <Promise<FirebaseAuthState>>this._fbAuth.signInWithEmailAndPassword(creds.email, creds.password)
return Promise.resolve(this._fbAuth.signInWithEmailAndPassword(creds.email, creds.password))
.then((user: firebase.User) => authDataToAuthState(user));
}

Expand All @@ -76,7 +76,7 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
if (options.scope) {
options.scope.forEach(scope => providerFromFirebase.addScope(scope));
}
return <Promise<firebase.auth.UserCredential>>this._fbAuth.signInWithPopup(providerFromFirebase);
return Promise.resolve(this._fbAuth.signInWithPopup(providerFromFirebase));
}

/**
Expand All @@ -85,16 +85,16 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
* You should subscribe to the FirebaseAuth object to listen succesful login
*/
authWithOAuthRedirect(provider: AuthProviders, options?: any): Promise<void> {
return <Promise<void>>this._fbAuth.signInWithRedirect(this._enumToAuthProvider(provider));
return Promise.resolve(this._fbAuth.signInWithRedirect(this._enumToAuthProvider(provider)));
}

authWithOAuthToken(credential: firebase.auth.AuthCredential): Promise<FirebaseAuthState> {
return <Promise<FirebaseAuthState>>this._fbAuth.signInWithCredential(credential)
return Promise.resolve(this._fbAuth.signInWithCredential(credential))
.then((user: firebase.User) => authDataToAuthState(user));
}

getRedirectResult(): Observable<firebase.auth.UserCredential> {
return Observable.fromPromise(<Promise<any>>this._fbAuth.getRedirectResult());
return Observable.fromPromise(Promise.resolve(this._fbAuth.getRedirectResult()));
}

private _enumToAuthProvider(providerId: AuthProviders): firebase.auth.AuthProvider | FirebaseOAuthProvider {
Expand Down

0 comments on commit d9a6ae7

Please sign in to comment.