[auth][js] Add in `onUserChanged` method

This commit is contained in:
Chris Bianca 2017-10-27 14:05:19 +01:00
parent e535a1ebaa
commit 9a17379e0e
1 changed files with 21 additions and 0 deletions

View File

@ -66,6 +66,7 @@ export default class Auth extends ModuleBase {
_setAuthState(auth: AuthResultType) {
this._authResult = auth;
this._user = auth && auth.user ? new User(this, auth.user) : null;
this.emit(this._getAppEventName('onUserChanged'), this._user);
}
/**
@ -149,6 +150,26 @@ export default class Auth extends ModuleBase {
this.removeListener(this._getAppEventName('onIdTokenChanged'), listener);
}
/**
* Listen for user changes.
* @param listener
*/
onUserChanged(listener: Function) {
this.log.info('Creating onUserChanged listener');
this.on(this._getAppEventName('onUserChanged'), listener);
if (this._authResult) listener(this._user || null);
return this._offUserChanged.bind(this, listener);
}
/**
* Remove user change listener
* @param listener
*/
_offUserChanged(listener: Function) {
this.log.info('Removing onUserChanged listener');
this.removeListener(this._getAppEventName('onUserChanged'), listener);
}
/**
* Sign the current user out
* @return {Promise}