samedi 11 février 2017

Express cookies not saving in post method

I am using express with cookie-parser and trying to save cookies using cookie parser and I am only able to save them in app.get but not in app.post.

The cookiess are able to be saved here:

        router.get('/login', function(req,res){
            res.sendFile('/Users/samkirkiles/Desktop/AWSDemo/views/login.html');
//**** These cookies do save
            res.cookie('username',"username")
            res.cookie('password',"password")
//****

        });

When I try to call the same code res.cookie('username',"uesrname") in router.post, everything runs but the cookies are not saved.

    router.post('/login', function(req,res,next){
        var mysql = require('mysql')
        var connection = mysql.createConnection({
            host : 'localhost',
            user : 'root',
            password : 'password',
            database : 'users'
        });


            var username = req.body.username
            var password = req.body.password


            connection.query('SELECT username,password FROM accounts WHERE username=? AND password=?;', [username,password], function (error, results, fields) {
                if (error) throw error;

                if(results.length >= 1){
                    console.log("Login successful with given username to VerifyUserLoggedIn: " + username)
    // ***** These cookies do not save
                    res.cookie('username',username)
                    res.cookie('password',password)
    //***********
                }else{
                    console.log("Login unsuccessful with given username to VerifyUserLoggedIn: " + username)

                }
            }); 
        });

Does anyone know why the cookies would not be saved in router.post but they would be saved in router.get?

Aucun commentaire:

Enregistrer un commentaire