[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;
});
}

View File

@@ -7,32 +7,17 @@ export default class User {
/**
*
* @param authClass Instance of Authentication class
* @param authObj authentication result object from native
* @param user user result object from native
*/
constructor(authClass, authObj) {
constructor(authClass, userObj) {
this._auth = authClass;
this._user = null;
this._updateValues(authObj);
this._user = userObj;
}
/**
* INTERNALS
*/
/**
*
* @param authObj
* @private
*/
_updateValues(authObj) {
this._authObj = authObj;
if (authObj.user) {
this._user = authObj.user;
} else {
this._user = null;
}
}
/**
* Returns a user property or null if does not exist
* @param prop
@@ -40,7 +25,6 @@ export default class User {
* @private
*/
_valueOrNull(prop) {
if (!this._user) return null;
if (!Object.hasOwnProperty.call(this._user, prop)) return null;
return this._user[prop];
}
@@ -52,7 +36,6 @@ export default class User {
* @private
*/
_valueOrFalse(prop) {
if (!this._user) return false;
if (!Object.hasOwnProperty.call(this._user, prop)) return false;
return this._user[prop];
}