Skip to content

Commit

Permalink
Remove session caching
Browse files Browse the repository at this point in the history
Remove session caching from `auth.checkCurrent()`. Instead, add a `_fetching` state which is true while a user session is being fetched from Panoptes.
  • Loading branch information
eatyourgreens committed Jun 29, 2023
1 parent bbda9c2 commit c8ceb17
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const authClient = new Model({
_bearerTokenExpiration: NaN,
_refreshToken: '',
_tokenRefreshPromise: null,
_fetching: false,

_getBearerToken: function() {
console.log('Getting bearer token');
Expand Down Expand Up @@ -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 = {
Expand All @@ -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;
}),
Expand All @@ -167,18 +172,25 @@ const authClient = new Model({
},

checkCurrent: function() {
if (!this._currentUserPromise) {
console.log('Checking current user');
console.log('Checking current user');
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
});
}

Expand All @@ -204,6 +216,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';

Expand All @@ -220,18 +233,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;
}),
Expand Down

0 comments on commit c8ceb17

Please sign in to comment.