added auth method result interceptor - grab user objects and shim into currentUser

This commit is contained in:
Salakar
2017-03-13 20:02:44 +00:00
parent 7710dd94a7
commit 15167a4f81
2 changed files with 183 additions and 101 deletions

View File

@@ -28,21 +28,49 @@ export default class Auth extends Base {
// generally though the initial event fired will get ignored
// but this is ok as we fake it with the getCurrentUser below
FirebaseAuthEvt.addListener('onAuthStateChanged', this._onAuthStateChanged.bind(this));
FirebaseAuth.createAuthStateListener();
FirebaseAuth.addAuthStateListener();
}
/**
* Internal auth changed listener
* @param auth
* @param emit
* @private
*/
_onAuthStateChanged(auth: AuthResultType) {
_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);
this.emit('onAuthStateChanged', this._authResult.user || null);
if (emit) this.emit('onAuthStateChanged', this._authResult.user || null);
return auth ? this._user : null;
}
/**
* Remove auth change listener
* @param listener
*/
_offAuthStateChanged(listener: Function) {
this.log.info('Removing onAuthStateChanged listener');
this.removeListener('onAuthStateChanged', listener);
}
/**
* Intercept all user actions and send their results to
* auth state change before resolving
* @param promise
* @returns {Promise.<TResult>|*}
* @private
*/
_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({ user: result }, false);
return result;
});
}
/*
@@ -61,21 +89,19 @@ export default class Auth extends Base {
}
/**
* Remove auth change listener
* @param listener
* Sign the current user out
* @return {Promise}
*/
_offAuthStateChanged(listener: Function) {
this.log.info('Removing onAuthStateChanged listener');
this.removeListener('onAuthStateChanged', listener);
signOut(): Promise<null> {
return this._interceptUserValue(FirebaseAuth.signOut());
}
/**
* Sign a user in anonymously
* @return {Promise} A promise resolved upon completion
* @return {Promise} A promise resolved upon completion
*/
signInAnonymously(): Promise<Object> {
return FirebaseAuth.signInAnonymously();
return this._interceptUserValue(FirebaseAuth.signInAnonymously());
}
/**
@@ -85,8 +111,7 @@ export default class Auth extends Base {
* @return {Promise} A promise indicating the completion
*/
createUserWithEmailAndPassword(email: string, password: string): Promise<Object> {
this.log.info('Creating user with email and password', email);
return promisify('createUserWithEmail', FirebaseAuth, 'auth/')(email, password);
return this._interceptUserValue(FirebaseAuth.createUserWithEmailAndPassword(email, password));
}
/**
@@ -201,15 +226,6 @@ export default class Auth extends Base {
return promisify('getToken', FirebaseAuth, 'auth/')();
}
/**
* Sign the current user out
* @return {Promise}
*/
signOut(): Promise<Object> {
return promisify('signOut', FirebaseAuth, 'auth/')();
}
/**
* Get the currently signed in user
* @return {Promise}