A
A
Alexey Artyushkov2016-05-03 17:17:11
Node.js
Alexey Artyushkov, 2016-05-03 17:17:11

Why does a Node.Js site hang after res.redirect(url)?

Hello! I'm trying to make an authorization system for the site. Assignment from the university: you can’t use Passport.js and similar things, you can only use session storage. I use connect-pg as session storage. After clicking on the Login button on the site, the browser sends a POST request to the server with a username and password. Upon receiving this request, the server crawls into the database and looks for the user in it. If such a user is found, then the server redirects to the root of the site. At the root of the site, index is rendered. So, after the redirect, the site hangs, the browser displays the inscription "Waiting for a response from localhost". What could be wrong?

router.post('/login.:format?', function (req, res) {
  var login = req.body;

  if (req.params.format === 'json'){
    res.send(login);
  }
  else {
    userUtils.login(login, function (user){
      if (user) {
        req.session.user_id = user.id;
        res.redirect('/');
      } else {
        res.redirect('/users/login');
      }
    },
    function (error) {
      express.error(error);
    });
  }
});

var user_utils = {
    pgConnect: function (callback) {
        pg.connect(connection, function(err, client){
            if (err) {
                console.log('connection to PG error ', err);
            }

            if (client) {
                callback(client);
            }
        });
    },

    loadUser: function(req, res, next) {
        if (req.session.user_id) {
            connectToDatabase(function (database) {
                database.one('select * from users where id=${id}', {id: req.session.user_id})
                    .then(function (user) {
                        if (user) {
                            req.currentUser = user;
                            next();
                        }
                        else {
                            res.redirect('/users/login');
                        }
                    }).catch(function (e) {
                        res.redirect('/users/login')
                    })
            })
        } else {
            res.redirect('/users/login');
        }
    },
    
    login: function (user, loginCallback, errorCallback) {
        connectToDatabase(function (database) {
            database.one('select * from users where login=${login}', {login: user.login})
                .then(function (u) {
                    console.log('login');
                    if (user.password === u.password){
                        console.log('user ' + u.id + ' logged');
                        loginCallback(u);
                    }
                })
                .catch(function (error) {
                    errorCallback(error)
                })
        })
    },

    register: function (user, registerCallback, errorCallback) {
        connectToDatabase(function (database) {
            database.one('insert into employers (id, first_name, middle_name, last_name) values (${id}, ${firstName}, ${middleName}, ${lastName}) returning id',
            {
                    id: user.sequenceNumber,
                    firstName: user.firstName,
                    middleName: user.middleName,
                    lastName: user.lastName
            })
                .then(function (employer) {
                    database.none('insert into users (login, password, employer_id) values(${login}, ${password}, ${id})',
                        {
                            login: user.login,
                            password: user.password,
                            id: employer.id
                        })
                        .then(function () {
                            registerCallback();
                        })
                        .catch(function (error) {
                            errorCallback(error)
                        })
                })
                .catch(function (error) {
                    errorCallback(error)
                })
        })
    }
};

block content
    form(name='login' action='/users/login' method='post')
        label
            h1 Login
        input(type='text' name='login')
        br
        br
        label
            h1 Password
        input(type='text' name='password')
        br
        br
        br
        input(type='submit' value='Login')
        a(href='/users/register') Register

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question