From 135c1621f23212e6153ad6756625154321d7aa70 Mon Sep 17 00:00:00 2001 From: Jim O'Donnell Date: Tue, 27 Jun 2023 15:00:31 +0100 Subject: [PATCH] Remove session caching Remove session caching from `auth.checkCurrent()`. Instead, add a `_fetching` state which is true while a user session is being fetched from Panoptes. --- lib/auth.js | 43 ++++++++++++++++++++++++++++++++----------- lib/oauth.js | 2 +- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/lib/auth.js b/lib/auth.js index 7522cf7..c29506d 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -17,6 +17,7 @@ const authClient = new Model({ _bearerTokenExpiration: NaN, _refreshToken: '', _tokenRefreshPromise: null, + _fetching: false, _getBearerToken: function() { console.log('Getting bearer token'); @@ -124,6 +125,7 @@ const authClient = new Model({ return this.register.apply(this, originalArguments); }.bind(this)); } else { + this._fetching = true; console.log('Registering new account', given.login); var registrationRequest = getCSRFToken(config.host).then(function(token) { var data = { @@ -144,18 +146,21 @@ const authClient = new Model({ .then(function() { return this._getBearerToken().then(function() { return this._getSession().then(function(user) { + this._fetching = false; console.info('Registered account', user.login, user.id); return user; }); }.bind(this)); }.bind(this)) .catch(function(request) { + this._fetching = false; console.error('Failed to register'); return apiClient.handleError(request); }); }.bind(this)); this.update({ + _fetching: true, _currentUserPromise: registrationRequest.catch(function() { return null; }), @@ -167,18 +172,30 @@ const authClient = new Model({ }, checkCurrent: function() { - if (!this._currentUserPromise) { - console.log('Checking current user'); + console.log('Checking current user (password grant)'); + const state = { + fetching: this._fetching, + user: this._currentUserPromise + } + console.log('Client state', state) + if (!this._fetching) { + var fetchUser = this._getBearerToken() + .then(function() { + return this._getSession(); + }.bind(this)) + .catch(function() { + // Nobody's signed in. This isn't an error. + console.info('No current user'); + return null; + }) + .then(function (user) { + this._fetching = false; + return user; + }.bind(this)); + this.update({ - _currentUserPromise: this._getBearerToken() - .then(function() { - return this._getSession(); - }.bind(this)) - .catch(function() { - // Nobody's signed in. This isn't an error. - console.info('No current user'); - return null; - }), + _fetching: true, + _currentUserPromise: fetchUser }); } @@ -204,6 +221,7 @@ const authClient = new Model({ }.bind(this)); } else { console.log('Signing in', credentials.login); + this._fetching = true; var signInRequest = getCSRFToken(config.host).then(function(token) { var url = config.host + '/users/sign_in'; @@ -220,18 +238,21 @@ const authClient = new Model({ .then(function() { return this._getBearerToken().then(function() { return this._getSession().then(function(user) { + this._fetching = false; console.info('Signed in', user.login, user.id); return user; }.bind(this)); }.bind(this)); }.bind(this)) .catch(function(request) { + this._fetching = false; console.error('Failed to sign in'); return apiClient.handleError(request); }); }.bind(this)); this.update({ + _fetching: true, _currentUserPromise: signInRequest.catch(function() { return null; }), diff --git a/lib/oauth.js b/lib/oauth.js index 2ba691d..50f9f7c 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -37,7 +37,7 @@ const authClient = new Model({ }, checkCurrent: function() { - console.log('Checking current user'); + console.log('Checking current user (implicit grant)'); // If we're checking for an existing session already, defer this until // it's finished