mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-24 04:24:52 +08:00
[auth] Simplify onAuthStateChanges and interceptUserValue to aid debugging
This commit is contained in:
@@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user