Very new to NodeJS/AngularJS. I am having problem getting a login page to redirect to another page once the id and password are verified.
Codes in the controller are as follows triggered by the route '/login/check':
exports.logincheck = function(req, res) {
User.findOne({email: req.body.email}).exec(function (err,user) {
if (!user) {
err = 'User not found.';
} else if (user.hashed_password == hashPW(req.body.password.toString())) {
req.session.regenerate(function () {
req.session.user = user.id;
req.session.username = user.username;
req.session.msg = 'Authenticated as ' + user.username ;
res.redirect('/');
});
} else {
err = 'Authentication failed.';
}
if (err) {
req.session.regenerate(function () {
req.session.msg = err;
res.status(404).send({err: 'User Not Found.'});
res.redirect('/login');
});
}
});
My AngularJS codes are as follows:
var loginApp = angular.module('loginApp', []);
loginApp.controller('loginController', ['$scope', '$http', function($scope,$http){
$scope.status = '';
$scope.processForm = function(checkemail,checkpassword) {
$http.post('/login/check', { email : checkemail, password: checkpassword})
.success(function(data, status, headers, config) {
$scope.user = data;
$scope.error = "";
$scope.status = status;
})
.error(function(data, status, headers, config) {
$scope.user = {};
$scope.error = data;
$scope.status = status;
});
};
}]);
Just trying to pass 2 variables from my HTML form to be verified. The errors I got are:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.header (/Users/josh_loh/Code/project/node_modules/express/lib/response.js:700:10)
at ServerResponse.send (/Users/josh_loh/Code/project/node_modules/express/lib/response.js:154:12)
at fn (/Users/josh_loh/Code/project/node_modules/express/lib/response.js:934:10)
at View.exports.renderFile [as engine] (/Users/josh_loh/Code/project/node_modules/ejs/lib/ejs.js:353:10)
at View.render (/Users/josh_loh/Code/project/node_modules/express/lib/view.js:93:8)
at EventEmitter.app.render (/Users/josh_loh/Code/project/node_modules/express/lib/application.js:566:10)
at ServerResponse.res.render (/Users/josh_loh/Code/project/node_modules/express/lib/response.js:938:7)
at /Users/josh_loh/Code/project/routes.js:52:7
at Layer.handle [as handle_request] (/Users/josh_loh/Code/project/node_modules/express/lib/router/layer.js:82:5)
at next (/Users/josh_loh/Code/project/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/Users/josh_loh/Code/project/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/Users/josh_loh/Code/project/node_modules/express/lib/router/layer.js:82:5)
at /Users/josh_loh/Code/project/node_modules/express/lib/router/index.js:267:22
at Function.proto.process_params (/Users/josh_loh/Code/project/node_modules/express/lib/router/index.js:321:12)
at next (/Users/josh_loh/Code/project/node_modules/express/lib/router/index.js:261:10)
Please help. Really like to know what I have understood wrongly. Thanks!