Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const crypto = require('crypto');
- const express = require('express');
- const app = express();
- app.use(cookieParser());
- app.get('/', function (req, res) {
- let user_token = req.cookies['house_user']; // always empty
- if (user_token) {
- // if the token exists, great!
- } else {
- crypto.randomBytes(24, function(err, buffer) {
- let token = buffer.toString('hex');
- res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
- res.cookie('house_user', token, {maxAge: 9000000000, httpOnly: true, secure: true });
- res.append('Set-Cookie', 'house_user=' + token + ';');
- res.send(token);
- });
- }
- });
Add Comment
Please, Sign In to add comment