diff --git a/lib/modules/auth/index.js b/lib/modules/auth/index.js index 3146e74e..378a23aa 100644 --- a/lib/modules/auth/index.js +++ b/lib/modules/auth/index.js @@ -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}