The '/stories' page is user-specific and requires valid login credentials.
Validation is accounted for with node, server-side, and properly logs before the redirect, but does not redirect to the stories page..
This seems to be because "You can't make a redirection after an AJAX. You need to do it yourself in Javascript", but the code in the first answer to this question seems somewhat incomplete..for the succcess call..
Also, how can a subsequent function (refreshStories) be called on success after the redirect?
Here's the AJAX:
if (loginUsername.length != 0) {
console.log('there is a login: ' + loginUsername);
// make an ajax call
$.ajax({
dataType: 'json',
data: AjaxLoginData,
type: 'post',
url:"http://localhost:4200/api/v1/users",
success: (data, textStatus, jqXHR) ->
if typeof data.redirect == 'string'
window.location = data.redirect
success: refreshStories,
error: foundError
});
Node.js + Express4
router.route('/users')
// log in a user (accessed at POST http://localhost:4200/api/v1/users)
.post(function(req, res) {
var username = req.body.loginUsername;
var password = req.body.loginPassword;
authenticateUser(username, password, function(err, user){
console.log('authenticate user..');
if (user) {
console.log('yes user');
// subsequent requests will know the user is logged in
req.session.username = user.username;
console.log('set session username to: ' + req.session.username);
// res.redirect('/stories');
res.send({redirect: '/stories'});
console.log('user logged in .. redirect to stories');
} else {
console.log('user authentication badCredentials error..');
res.render('index', {badCredentials: true});
}
});
});