1

my index.js file an node js file looks like this below

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
let cryptr = require('cryptr');
let localStorage = require('node-localstorage');
cryptr = new cryptr('myTotalySecretKey');
const jwt = require('jsonwebtoken');
var dir = path.join(__dirname, 'public');
const app = express();
app.use(cookieParser());
app.use(express.static(dir));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static(__dirname + 'public'));

var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'trello',
    password: 'trello',
    database: 'trello'
});
connection.connect(function (err) {
    if (!err) {
        console.log("Database is connected");
    } else {
        console.log("Error while connecting with database");
    }
});
module.exports = connection;

app.post('/Authenticate', (req, res) => {
    let email = req.body.email;
    let password = req.body.password;
    let sql = 'select * from users where email = ?';
    let values = [[email]];

    connection.query(sql, [values], function (err, result) {
        if (err) throw err;

        if (result.length > 0) {
            decryptedString = cryptr.decrypt(result[0].password);
            if (password == decryptedString) {
                console.log('user is logged in: ');

                jwt.sign({ result }, 'secretkey', (err, token) => {
                    console.log('token = ' + token);
                    res.cookies('email', req.body.email);


                });

                return res.redirect('/home.html');
            } else {
                res.redirect('/login.html');
            }
        }
    });
});

app.listen(3000);

but i am not able to set cookies to the client side i.e on the browser, i used res.cookie(key, value) but it is not working an anyone guide why it is happening, i am pretty new to node js and express js

Avinash jain
  • 486
  • 1
  • 7
  • 15

1 Answers1

0

It appears that you've typed res.cookies() instead of res.cookie(). That looks like the problem, though I could be wrong.

A little tip: when authenticating users, it's best to use sessions. That just means that the cookie values are stored in the database (or somewhere else), while the browser only stores an ID unique to that session. This prevents malicious people from stealing passwords and the like. Express has a module called express-session, and that's what I use. Here's a MySQL plugin for it. Just something to look into.

More on that: What is the difference between server side cookie and client side cookie?

Caleb Denio
  • 1,465
  • 8
  • 15
  • thanks Caleb i have changed it but still it does not work by the way thanks for telling me about express-sessions –  Sep 17 '19 at 17:38
  • Hmm... that's strange. May I ask what the `module.exports` is accomplishing here? – Caleb Denio Sep 17 '19 at 20:19