2

Can you help me for creating cookies cause i can't make it work. I would like to set and create cookies after the user logs in but I don't know what's wrong with my codes. Thanks Guys.

Here's my code, if you think there's other error's or code correction can you help me fix it? Thanks guys. :)

app.js

//deps

var express = require('express');
var app = express();
var redis = require('redis');
var client = redis.createClient();


var delcookie = function(req, res) { res.clearCookie('login_token'); res.redirect('/');     };
var setcookie = function(req, res) { res.cookie('login_token', +new Date(), { maxAge: 3600000, path: '/' }); res.redirect('/'); };

//configs
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(app.router);

client.on('error', function (err) {
console.log('Error: ' + client.host + ':' + client.port + ' - ' + err);
});

app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});

app.get('/login/', function (req, res) {
res.sendfile(__dirname + '/login.html');
});

app.get('/register/', function (req, res) {
res.sendfile(__dirname + '/register.html');
});

app.get('/restricted/', function (req, res) {
res.sendfile(__dirname + '/restricted.html');
});

app.get('/logout/', delcookie, function (req, res) {
res.sendfile(__dirname + '/logout.html');
});

//post

app.post('/login/', function (req, res) {
//res.sendfile(__dirname + '/restricted.html');
client.hmget( req.body.user.username, 'password', function (err,pass) {
if ( (!err) && pass && pass == req.body.user.password ){
    res.redirect('/restricted/', setcookie);
    }   else if ( pass == false)   {
    res.redirect('/register/');
    }   else    {
    res.redirect('/login/');
    }
    });
});

app.post('/register/', function (req, res) {    
client.hmset(req.body.user.username, 'password',req.body.user.password, 
'fname',req.body.user.fname, 'lname', req.body.user.lname, 
'password', req.body.user.password, 'email', req.body.user.email,
'mobile', req.body.user.mobile, redis.print);
res.write('Successfully Registered');
});

app.listen(80);

1 Answer 1

1

To use cookies you should look at the Express cookieSession middleware. The docs are here: http://expressjs.com/api.html#cookieSession

Update (I can't test it right now so this is from what I can remember):

You can add the cookieSession middleware and specify the settings for your cookie with something like:

app.use(express.cookieSession({
    cookie: {
        path: '/',
        maxAge: 3600000
    }
}));

Then when your user logs in you can set the login token on the session with req.session.login_token = 'login_token';.

And when your user logs out you can clear the session by doing req.session = null;.

In your authentication middleware you can then check that the login token is set on the session to see if the user is authenticated.

Sign up to request clarification or add additional context in comments.

2 Comments

I've already read that -_- but I still don't know how to make it work Brett.
Updated my answer. Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.