[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

@ -1312,21 +1312,22 @@ class RNFirebaseAuth extends ReactContextBaseJavaModule {
final Uri photoUrl = userInfo.getPhotoUrl(); final Uri photoUrl = userInfo.getPhotoUrl();
if (photoUrl != null) { if (photoUrl != null && !"".equals(photoUrl)) {
userInfoMap.putString("photoURL", photoUrl.toString()); userInfoMap.putString("photoURL", photoUrl.toString());
} else { } else {
userInfoMap.putNull("photoURL"); userInfoMap.putNull("photoURL");
} }
final String phoneNumber = userInfo.getPhoneNumber(); final String phoneNumber = userInfo.getPhoneNumber();
if (phoneNumber != null) { if (phoneNumber != null && !"".equals(phoneNumber)) {
userInfoMap.putString("phoneNumber", phoneNumber); userInfoMap.putString("phoneNumber", phoneNumber);
} else { } else {
userInfoMap.putNull("phoneNumber"); userInfoMap.putNull("phoneNumber");
} }
// The Android SDK is missing the email property for the email provider, so we use UID instead // The Android SDK is missing the email property for the email provider, so we use UID instead
if (EmailAuthProvider.PROVIDER_ID.equals(userInfo.getProviderId())) { if (EmailAuthProvider.PROVIDER_ID.equals(userInfo.getProviderId())
&& (userInfo.getEmail() == null || "".equals(userInfo.getEmail()))) {
userInfoMap.putString("email", userInfo.getUid()); userInfoMap.putString("email", userInfo.getUid());
} else { } else {
userInfoMap.putString("email", userInfo.getEmail()); userInfoMap.putString("email", userInfo.getEmail());
@ -1361,25 +1362,25 @@ class RNFirebaseAuth extends ReactContextBaseJavaModule {
userMap.putBoolean("emailVerified", verified); userMap.putBoolean("emailVerified", verified);
userMap.putBoolean("isAnonymous", user.isAnonymous()); userMap.putBoolean("isAnonymous", user.isAnonymous());
if (email != null) { if (email != null && !"".equals(email)) {
userMap.putString("email", email); userMap.putString("email", email);
} else { } else {
userMap.putNull("email"); userMap.putNull("email");
} }
if (name != null) { if (name != null && !"".equals(name)) {
userMap.putString("displayName", name); userMap.putString("displayName", name);
} else { } else {
userMap.putNull("displayName"); userMap.putNull("displayName");
} }
if (photoUrl != null) { if (photoUrl != null && !"".equals(photoUrl)) {
userMap.putString("photoURL", photoUrl.toString()); userMap.putString("photoURL", photoUrl.toString());
} else { } else {
userMap.putNull("photoURL"); userMap.putNull("photoURL");
} }
if (phoneNumber != null) { if (phoneNumber != null && !"".equals(phoneNumber)) {
userMap.putString("phoneNumber", phoneNumber); userMap.putString("phoneNumber", phoneNumber);
} else { } else {
userMap.putNull("phoneNumber"); userMap.putNull("phoneNumber");

View File

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

View File

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