Skip to content

Commit

Permalink
created a logout system, if logged in, redirected to home
Browse files Browse the repository at this point in the history
  • Loading branch information
pwshugar committed Jul 31, 2013
1 parent 8707857 commit b1dafd8
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 41 deletions.
17 changes: 17 additions & 0 deletions css/home.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@

body {
background-color: #5ccdc9;
}

#logout {
position:absolute;
right: 30px;
top: 20px;
}

.loginEls {
font-size: 18px;
padding-left: 10px;
}

#fakeLink {
color: blue;
text-decoration: underline;
cursor: pointer;
}
50 changes: 28 additions & 22 deletions html/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ <h2>Create a new game?</h2>
<button type="button" id="post" >Submit</button>
</div>
<div class="left" id="right">
<h2>Join a Game?</h2>
<label >Group Name<input class="enter" id="joingroupname" type="text" name="joingroupname" ></label>
<label>Password<input class="enter" id="joinpassword" type="password" name="joinpassword" ></label>
<h2>Join a game?</h2>
<label >Group Name<input class="joinenter" id="joingroupname" type="text" name="joingroupname" ></label>
<label>Password<input class="joinenter" id="joinpassword" type="password" name="joinpassword" ></label>
<button type="button" id="joinpost" >Submit</button>
</div>
<script>
$(function(){

var loggedIn = false;

var logcheck = function(){
$.ajax({
url:"/logcheck",
Expand All @@ -39,7 +41,8 @@ <h2>Join a Game?</h2>
success: function(data){
if (data){
$('#logout').css("display", "block");
$('#welcome').text("Welcome, " + data);
$('#welcome').text("Welcome, " + data[0].toUpperCase() + data.slice(1));
loggedIn = true;
} else {
$('#login').css("display", "block");
}
Expand Down Expand Up @@ -85,13 +88,10 @@ <h2>Join a Game?</h2>
} else {
$('#groupname')[0].value = '';
$('#password')[0].value = '';
console.log('success');
updateGroups();
}
}
});
});
});

$(".joinenter").on('keypress',function(e) {
if(e.which === 13) {
Expand All @@ -100,23 +100,29 @@ <h2>Join a Game?</h2>
});

$("#joinpost").on('click',function() {
console.log('joined');
$.ajax({
url:"/join",
type: "post",
data: {
groupname: $('#groupname')[0].value,
password: $('#password')[0].value
},
success: function(data){
if (data === 'false'){
console.log('wrong password');
} else {
window.location.href = 'http://127.0.0.1:8080/home';
if (!loggedIn){
console.log('worked');
} else {
$.ajax({
url:"/joingroup",
type: "post",
data: {
groupname: $('#joingroupname')[0].value,
password: $('#joinpassword')[0].value
},
success: function(data){
if (data === 'false'){
console.log('wrong password');
$('#joingroupname')[0].value = '';
$('#joinpassword')[0].value = '';
} else {
window.location.href = 'http://127.0.0.1:8080/home';
}
}
}
});
});
}
});

});
</script>
</body>
Expand Down
32 changes: 32 additions & 0 deletions html/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,37 @@
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<body>
<h1>Home</h1>
<div id="logout">
<span id="welcome" class="loginEls" >Welcome</span>
<span id="fakeLink" class="loginEls" >Log Out?<span>
</div>
<script>
$(function(){
var logcheck = function(){
$.ajax({
url:"/logcheck",
type: "post",
data: {},
success: function(data){
$('#welcome').text("Welcome, " + data[0].toUpperCase() + data.slice(1));
}
});
};

logcheck();

$("#fakeLink").on('click',function() {
$.ajax({
url:"/logout",
type: "post",
data: {},
success: function(data){
window.location.href = 'http://127.0.0.1:8080/';
}
});
});

});
</script>
</body>
</html>
34 changes: 23 additions & 11 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ exports.signup = function(req, res){
exports.login = function(req, res){
var username = req.body.username.toLowerCase();
console.log('Retrieving user: ' + username);

UserModel.findOne({'username': username}, function(err, data){
if (data === null){
res.send('false');
Expand All @@ -70,25 +69,38 @@ exports.create = function(req, res){
password: req.body.password
};
var group = new GroupModel(group_data);

group.save(function(err, data){
if (err){
res.send('false');
} else {
res.send('true');
}
});
group.save(function(err, data){
if (err){
res.send('false');
} else {
res.send('true');
}
});
};

exports.logcheck = function(req, res){
if (req.session){
console.log(req.session);
if (req.session.username){
res.send(req.session.username);
} else {
res.end();
}
};

exports.joingroup = function(req, res){
var groupname = req.body.groupname.toLowerCase();
console.log('Retrieving group: ' + groupname);
GroupModel.findOne({'groupname': groupname}, function(err, data){
if (data === null){
res.send('false');
} else if (data.password !== req.body.password){
res.send('false');
} else {
req.session.groupname = req.body.groupname;
res.send('true');
}
});
};




Expand Down
36 changes: 28 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,49 @@ app.post('/create', function(req, res){
router.create(req, res);
});

app.post('/joingroup', function(req, res){
router.joingroup(req, res);
});

app.post('/logcheck', function(req, res){
router.logcheck(req, res);
});

app.post('/logout', function(req, res){
req.session.username = null;
req.session.destroy();
res.end();
});

app.get('/home', function(req, res){
res.sendfile('./html/home.html');
if (req.session.username && req.session.groupname){
res.sendfile('./html/home.html');
} else {
res.redirect('/');
}
});

app.get('/login', function(req, res) {
res.sendfile('./html/login.html');
app.get('/login', function(req, res){
if (req.session.username && req.session.groupname){
res.redirect('/home');
} else {
res.sendfile('./html/login.html');
}
});

app.get('/signup', function(req, res) {
res.sendfile('./html/signup.html');
app.get('/signup', function(req, res){
if (req.session.username && req.session.groupname){
res.redirect('/home');
} else {
res.sendfile('./html/signup.html');
}
});

app.get('/', function(req, res){
res.sendfile('./html/create.html');
if (req.session.username && req.session.groupname){
res.redirect('/home');
} else {
res.sendfile('./html/create.html');
}
});

// app.get('/', function(req, res){
Expand All @@ -74,7 +94,7 @@ app.get('/css/create.css', function(req, res){
});

app.get('/*', function(req, res) {
res.redirect('/');
res.redirect('/home');
});


Expand Down

0 comments on commit b1dafd8

Please sign in to comment.