0

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!

Josh
  • 3
  • 2
  • Error seems to be on the `Node.js` side (I see `express` in your logs). Make sure you're not calling `res.sendStatus`, `res.send`, `res.send*` more than once. – bardzusny Jun 15 '15 at 15:11

1 Answers1

0

Looks like there are at least two problems in your controller.

You have res.status(404).send({err: 'User Not Found.'}); followed by res.redirect('/login');. Both send and redirect finish the request and respond the request, so that's basically like trying to respond twice. That's probably what is giving you the "can't set headers" error. This answer to a similar problem has good summary of when the various res methods should be used.

You are also setting values on the req object. Req just gives you information about the request, and should generally be treated like a read-only object. Only data in the res object is going to get back to the client.

Community
  • 1
  • 1
Tony
  • 388
  • 3
  • 11
  • Removed the res.redirect() completely from my controller and added $location.path('/'); $window.location.reload(); in and angular codes. It worked but am wondering if this is the proper way to do it. – Josh Jun 17 '15 at 07:20
  • Does your app really need to get the 404 error code? I would have dropped that, and just done the res.redirect() (which defaults to using 302 as the status code in express). That is typically what I do in my apps for login. I don't use angular though. – Tony Jun 17 '15 at 14:51
  • Thanks for the note. Will experiment more on this. – Josh Jun 19 '15 at 14:35