Skip to content

Commit

Permalink
cf #9 - Use, and check, saved auth data at start
Browse files Browse the repository at this point in the history
  • Loading branch information
twidi committed May 9, 2013
1 parent 88741b1 commit 4237e13
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions js/lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@

Controller.prototype.get_current_user = (function Controller__get_current_user () {
this.current_user = $.jStorage.get('logged-user', null);

if (this.current_user && this.current_user.provider && this.providers[this.current_user.provider]) {
if (!this.providers[this.current_user.provider].init_auth(this.current_user)) {
this.current_user = null;
}
}

if (!this.current_user) {
this.logout();
}
}); // get_current_user

Controller.prototype.login = (function Controller__login (username, auth_data, provider) {
Expand Down
20 changes: 19 additions & 1 deletion js/lib/providers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,27 @@

Provider.prototype.set_token = (function Github__set_token (token) {
this.conf.token = token;
Gh3.Helper.headers['Authorization'] = 'token ' + token;
if (token) {
Gh3.Helper.headers.Authorization = 'token ' + token;
} else if (Gh3.Helper.headers.Authorization) {
delete Gh3.Helper.headers.Authorization;
}
}); // set_token

Provider.prototype.init_auth = (function Github__init_auth (auth_data) {
if (!auth_data || auth_data.provider != this.name || !auth_data.token) { return false; }
this.set_token(auth_data.token);
var provider = this,
user = new Gh3.CurrentUser();
user.fetch(function(error, user_data) {
if (error || !user_data) {
provider.set_token(null);
provider.controller.logout();
}
});
return true;
});

Provider.prototype.can_login = (function Github__can_login () {
return !!(this.conf.auth == 'oauth' && this.conf.client_id && this.conf.token_script);
}); // can_login
Expand Down

0 comments on commit 4237e13

Please sign in to comment.