Skip to content

Commit

Permalink
rename checkToken to checkLogin, and change status message to string.…
Browse files Browse the repository at this point in the history
… Add registration functions, getProfile, setProfile, and add navigation flow to registration and login.
  • Loading branch information
swznd committed Dec 27, 2012
1 parent c31e715 commit 3de1fa7
Showing 1 changed file with 97 additions and 22 deletions.
119 changes: 97 additions & 22 deletions js/user.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@


this.user = (function() { this.user = (function() {
var openLogin = false; var openLogin = false;
var openRegister = false;
var nextDo;
var currentToken;


function checkToken(callback) { function checkLogin(callback) {
var status;

model.get(1,'user',function(data) { model.get(1,'user',function(data) {
if(data.token && data.token_expires && data.token_exchange) { if(data.token && data.token_expires && data.token_exchange) {
if(data.token_expires > Math.ceil(new Date().getTime() / 1000)) { if(data.token_expires > Math.ceil(new Date().getTime() / 1000)) {
var status = true; currentToken = data.token;
if(data.firstname && data.lastname) {
status = 'ok';
}
else {
status = 'no_name';
}
} }
else { else {
exchangeToken(data, function(resp) { exchangeToken(data, function(resp) {
if(!resp) { if(!resp) {
var status = false; status = 'failed_exchange_token';
} }
}); });
} }
} }
else { else {
var status = false; status = 'no_user'
} }


if(callback && typeof callback === "function") { if(callback && typeof callback === "function") {
Expand All @@ -27,16 +38,46 @@ this.user = (function() {
}) })
} }


function showLogin() { function showLogin(next) {
navigation.go('view-login','popup'); navigation.go('view-login','popup');
openLogin = true; openLogin = true;
if(next && typeof next === "function") {
nextDo = next;
}
return true; return true;
} }


function hideLogin() { function hideLogin(executeNext) {
if(openLogin()) { if(openLogin) {
navigation.back(); navigation.back();
openLogin = false; openLogin = false;
if(nextDo && executeNext) {
nextDo();
nextDo = null;
}
return true;
}
return false;
}

function showRegister(next) {
hideLogin();
navigation.go('view-register','popup');
openRegister = true;
if(next && typeof next === "function") {
nextDo = next;
}
return true;
}

function hideRegister(executeNext) {
if(openRegister) {
navigation.back();
openRegister = false;
if(nextDo && executeNext) {
nextDo();
nextDo = null;
}
return true; return true;
} }
return false; return false;
Expand Down Expand Up @@ -72,23 +113,56 @@ this.user = (function() {
}); });
} }


function update(firstname,lastname) { function setProfile(option, callback) {
checkToken(function() { checkLogin(function(resp) {
$.post(API+'user/update',{ firstname: firstname, lastname: lastname }, function(resp) { if(resp == 'no_user') {
if(!resp.status) { showLogin();
if(resp.message.toLowerCase() == 'invalid or expired token') { return false;
showLogin(); }
}
} var data = {
}) firstname: '',
lastname: '',
bio: '',
token: currentToken
}

$.extend(data,option);

api.updateProfile(data, function(resp) {
model.get(1,'user',function(datadb) {
$.extend(datadb,option);
model.set(datadb,'user', function() {
if(callback && typeof callback === "function") {
callback(resp);
}
})
})
})
}) })
} }


function getProfile() {
model.get(1,'user',function(data) {
if(data.firstname && data.lastname) {
$('#firstname').val(data.firstname);
$('#lastname').val(data.lastname);
$('#submit-profile').removeAttr('disabled');
}

$('#bio').val(data.bio);
})
}

return { return {
checkToken: checkToken, checkLogin: checkLogin,
showLogin: showLogin, showLogin: showLogin,
hideLogin: hideLogin, hideLogin: hideLogin,
setLogged: setLogged setLogged: setLogged,
showRegister: showRegister,
hideRegister: hideRegister,
setProfile: setProfile,
getProfile: getProfile
}; };
}()); }());


Expand All @@ -97,10 +171,11 @@ window.onmessage = function(event) {
if(event.data.status == true) { if(event.data.status == true) {
user.setLogged(event.data.results, function() { user.setLogged(event.data.results, function() {
if(event.data.action == 'input/name') { if(event.data.action == 'input/name') {
window.location.hash = '#profile'; user.showRegister();
} }

else {
hideLogin(); user.hideLogin(true);
}
}); });
} }
else { else {
Expand Down

0 comments on commit 3de1fa7

Please sign in to comment.