[auth] Simplify onAuthStateChanges and interceptUserValue to aid debugging

This commit is contained in:
Chris Bianca
2017-10-27 11:00:06 +01:00
parent f467e2d904
commit 31594756cc
3 changed files with 26 additions and 50 deletions

View File

@@ -22,13 +22,11 @@ export default class Auth extends ModuleBase {
_native: Object;
_getAppEventName: Function;
_authResult: AuthResultType | null;
authenticated: boolean;
constructor(firebaseApp: Object, options: Object = {}) {
super(firebaseApp, options, true);
this._user = null;
this._authResult = null;
this.authenticated = false;
this.addListener(
// sub to internal native event - this fans out to
@@ -65,20 +63,19 @@ export default class Auth extends ModuleBase {
this.emit(eventKey, event.state);
}
_setAuthState(auth: AuthResultType) {
this._authResult = auth;
this._user = auth && auth.user ? new User(this, auth.user) : null;
}
/**
* Internal auth changed listener
* @param auth
* @param emit
* @private
*/
_onAuthStateChanged(auth: AuthResultType, emit: boolean = true) {
this._authResult = auth;
this.authenticated = auth ? auth.authenticated || false : false;
if (auth && auth.user && !this._user) this._user = new User(this, auth);
else if ((!auth || !auth.user) && this._user) this._user = null;
else if (this._user) this._user._updateValues(auth);
if (emit) this.emit(this._getAppEventName('onAuthStateChanged'), this._user);
return auth ? this._user : null;
_onAuthStateChanged(auth: AuthResultType) {
this._setAuthState(auth);
this.emit(this._getAppEventName('onAuthStateChanged'), this._user);
}
/**
@@ -96,14 +93,9 @@ export default class Auth extends ModuleBase {
* @param emit
* @private
*/
_onIdTokenChanged(auth: AuthResultType, emit: boolean = true) {
this._authResult = auth;
this.authenticated = auth ? auth.authenticated || false : false;
if (auth && auth.user && !this._user) this._user = new User(this, auth);
else if ((!auth || !auth.user) && this._user) this._user = null;
else if (this._user) this._user._updateValues(auth);
if (emit) this.emit(this._getAppEventName('onIdTokenChanged'), this._user);
return auth ? this._user : null;
_onIdTokenChanged(auth: AuthResultType) {
this._setAuthState(auth);
this.emit(this._getAppEventName('onIdTokenChanged'), this._user);
}
/**
@@ -124,10 +116,10 @@ export default class Auth extends ModuleBase {
*/
_interceptUserValue(promise) {
return promise.then((result) => {
if (!result) return this._onAuthStateChanged(null, false);
if (result.user) return this._onAuthStateChanged(result, false);
if (result.uid) return this._onAuthStateChanged({ authenticated: true, user: result }, false);
return result;
if (!result) this._setAuthState(null);
else if (result.user) this._setAuthState(result);
else if (result.uid) this._setAuthState({ authenticated: true, user: result });
return this._user;
});
}