Skip to content

Commit

Permalink
changed login to ajax added error message for incorret login, closes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Jun 26, 2013
1 parent 8e10ce1 commit 9b43f80
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
27 changes: 27 additions & 0 deletions public/src/forum/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,31 @@
document.location.href = target.getAttribute('data-url');
}
});


$('#login').on('click', function() {

var loginData = {
'username': $('#username').val(),
'password': $('#password').val(),
'_csrf': $('#csrf-token').val()
};

$.ajax({
type: "POST",
url: '/login',
data: loginData,
success: function(data, textStatus, jqXHR) {
$('#login-error-notify').hide();
window.location.replace("/");
},
error : function(data, textStatus, jqXHR) {
$('#login-error-notify').show().delay(1000).fadeOut(250);
},
dataType: 'json'
});

return false;
});

}());
8 changes: 6 additions & 2 deletions public/templates/login.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
<button type="button" class="close" data-dismiss="alert">&times;</button>
<strong>Failed Login Attempt</strong> <p></p>
</div>
<form method="post" action="/login">

<form>
<label>Username</label><input type="text" placeholder="Enter Username" name="username" id="username" /><br />
<label>Password</label><input type="password" placeholder="Enter Password" name="password" id="password" /><br />
<input type="hidden" name="_csrf" value="{token}" />
<input type="hidden" name="_csrf" value="{token}" id="csrf-token" />
<button class="btn btn-primary" id="login" type="submit">Login</button> &nbsp; <a href="/reset">Forgot Password?</a>
</form>

<span id="login-error-notify" class="label label-important hide">Invalid username/password</span><br/>
</div>

<div class="well span6 {alternate_logins:display}">
<h4>Alternative Logins</h4>
<ul class="alt-logins">
Expand Down
7 changes: 3 additions & 4 deletions src/routes/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@
});


app.post('/login', passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login'
}));
app.post('/login', passport.authenticate('local'), function(req, res) {
res.json({success:1});
});

app.post('/register', function(req, res) {
user_module.create(req.body.username, req.body.password, req.body.email, function(err, uid) {
Expand Down
5 changes: 4 additions & 1 deletion src/webserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ var express = require('express'),

app.get('/' + route, function(req, res) {
if ((route === 'login' || route ==='register') && (req.user && req.user.uid > 0)) {
res.redirect('/account');

user.getUserField(req.user.uid, 'userslug', function(userslug) {
res.redirect('/users/'+userslug);
});
return;
}

Expand Down

0 comments on commit 9b43f80

Please sign in to comment.