Skip to content

Commit

Permalink
implements logout button #119
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 12, 2015
1 parent 9d3558d commit 0756a16
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 16 deletions.
73 changes: 60 additions & 13 deletions front/public/js/alpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ $(document).ready(function() {
var DEFAULTDESC = "Tap/click here to update the description for this timer";
var EMAIL;
var JWT;
var COIN = new Audio("http://www.orangefreesounds.com/wp-content/uploads/2014/08/Mario-coin-sound.mp3");
var COIN = new Audio("http://themushroomkingdom.net/sounds/wav/smb/smb_coin.wav");
var OUT = new Audio('http://themushroomkingdom.net/sounds/wav/smb/smb_mariodie.wav');
/**
* timerupsert is our generic API CRUD method which allows us to
* CREATE a new timer, UPDATE the description of the timer and
Expand Down Expand Up @@ -332,28 +333,39 @@ $(document).ready(function() {
event.stopImmediatePropagation();
event.preventDefault();
return login_or_register();
})
});

$('#logout').click(function() {
return logout();
});

$('#start').click(function() {
return start();
})
});

$("#stop").click( function() {
console.log("#stop Clicked!")
return stop();
});

$("#clear").click( function() {
localStorage.clear(); // erase all history (client-side only)
clear();
boot(function(){
console.log("#clear localStorage - erase session/JWT & timers");
clearactive();
timers = {}; // make sure timers are cleared!
rendertimers(); // re-render list
return start();
})
});
}
/**
* clears everyting on the client
* used in logout and in dev see: https://github.com/ideaq/time/issues/115
*/
var clear = function(){
console.log("#clear localStorage - erase session/JWT & timers");
localStorage.clear(); // erase all history (client-side only)
$("#past-timers-list").html(''); // clear past timers
clearactive();
timers = {}; // make sure timers are cleared!
}


var editlistener = function(id) {
Expand Down Expand Up @@ -395,13 +407,18 @@ $(document).ready(function() {
password: $('#password').val()
};
// >> input validation here!

db.set('email', person.email);
JWT = localStorage.getItem('JWT');
var head = {}
if(JWT) {
head = {
Authorization: JWT
}
}

$.ajax({
type: "POST",
headers: {
Authorization: JWT
},
headers : head,
url: "/login-or-register",
data: person,
dataType: "json",
Expand All @@ -416,9 +433,11 @@ $(document).ready(function() {
})
rendertimers();
}
localStorage.setItem('JWT', xhr.getResponseHeader("authorization"));
JWT = xhr.getResponseHeader("authorization");
COIN.play();

$('#login').fadeOut();
$('#loggedinas').html(person.email);
$('#nav').fadeIn();
},
error: function(xhr, err) {
Expand All @@ -428,6 +447,27 @@ $(document).ready(function() {
});
}

var logout = function(){
$.ajax({
type: "POST",
headers: {
Authorization: JWT
},
url: "/logout",
success: function(res, status, xhr) {
console.log('LOGOUT - - - - - - - res:')
console.log(res);
$('#nav').fadeOut();
$('#login').fadeIn();
OUT.play();
return clear();
},
error: function(xhr, err) {
console.log(err);
}
});
}

/**
* loadtimers fetches existing timers from API
*
Expand Down Expand Up @@ -465,6 +505,13 @@ $(document).ready(function() {
JWT = localStorage.getItem('JWT');
if(JWT) {
console.log('existing person', JWT);
var email = db.get('email');
console.log("EMAIL: "+email);
if(email) {
$('#login').fadeOut();
$('#loggedinas').html(email);
$('#nav').fadeIn();
}
return callback();
} else {
$.ajax({
Expand Down
6 changes: 3 additions & 3 deletions front/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
<section id="nav" style="display:none;">
<div id="profile-button">
<i class="fa fa-user"></i>
<p id="offlinemsg"> You are logged in as </p>
<i class="fa fa-sign-out" id="logout"></i>
<p id="offlinemsg"> You are logged in as <b id="loggedinas"> </b>&nbsp;</p>
<button id="logout"><i class="fa fa-sign-out" ></i>Logout</button>
</div>
</section>

Expand Down Expand Up @@ -90,6 +90,6 @@
<!-- https://github.com/ideaq/faster -->
<script src="https://rawgit.com/ideaq/faster/master/lib/client.js"></script>

<i id="clear" class="fa fa-recycle" style="position:fixed; bottom:0.2em; left:10px; z-index:100; color:#BEF202; border: 1px white solid; padding:4px;"></i>
<i id="clear" class="fa fa-recycle" style="position:fixed; bottom:0.2em; left:10px; z-index:100; color:#BEF202; border: 1px white solid; padding:4px; display: none;"></i>
</body>
</html>

0 comments on commit 0756a16

Please sign in to comment.