Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove session caching #208

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 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,12 +146,14 @@ 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);
});
Expand All @@ -167,22 +171,29 @@ const authClient = new Model({
},

checkCurrent: function() {
if (!this._currentUserPromise) {
console.log('Checking current user');
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;
}),
console.log('Checking current user (password grant)');
const self = this;
console.log('Client state', self)
if (!self._fetching) {
self._fetching = true;
var fetchUser = self._getBearerToken()
.then(self._getSession)
.catch(function() {
// Nobody's signed in. This isn't an error.
console.info('No current user');
return null;
})
.then(function (user) {
self._fetching = false;
return user;
});

self.update({
_currentUserPromise: fetchUser
});
}

return this._currentUserPromise;
return self._currentUserPromise;
},

checkBearerToken: function() {
Expand All @@ -204,6 +215,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,12 +232,14 @@ 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);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down