I would like to work with sessions on low level. How is it possible to generate session id in node.js?
It is not clear what you are trying to achieve, but... a session ID is just an ID! You generate it however you want. There are no requirements except for uniqueness. It is a good idea though to make it secure. For example this function may be your session id generator:
var crypto = require('crypto');
var generate_key = function() {
// 16 bytes is likely to be more than enough,
// but you may tweak it to your needs
return crypto.randomBytes(16).toString('base64');
};
You call generate_key()
and you check whether it exists in a database. If it does you call it again and so on and so on.
EDIT: Let me address comments:
Math.random()
here and there were some concerns that this is not cryptographically secure. While it is true, I don't see OP asking for cryptographically secure solution. For all we know he may not need it, after all we don't know what kind of sessions he is dealing with. Ultimately I've decided to change it, cause crypto solution is safer and (most of the time) it doesn't have any drawback.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With