From 9a17379e0eb594a87b085e6459416ead6b662b09 Mon Sep 17 00:00:00 2001 From: Chris Bianca Date: Fri, 27 Oct 2017 14:05:19 +0100 Subject: [PATCH] [auth][js] Add in `onUserChanged` method --- lib/modules/auth/index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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}