const express = require('express'), router = express.Router(), crypto = require('crypto'); /* Simple NO-OP to set session cookie so user-id can use it as the * index */ router.get('/', (req, res/*, next*/) => { let userId; if (!req.cookies.user) { userId = crypto.randomBytes(16).toString('hex'); res.cookie('user', userId); } else { userId = req.cookies.user; } console.log(`[${userId.substring(0, 8)}]: Browser hand-shake achieved.`); return res.status(200).send({ user: userId }); }); router.get('/', async (req, res/*, next*/) => { console.log('GET /groups/', req.session.userId); return res.status(200).send( [ { id: 1, ownerId: 1, name: 'Beer Tuesday', nextEvent: Date.now() + 86400 * 14 * 1000 /* 2 weeks from now */ } ] ); }); router.post('/:id', async (req, res) => { const { id } = req.params; let userId; if (!req.cookies.user) { userId = crypto.randomBytes(16).toString('hex'); res.cookie('user', userId); } else { userId = req.cookies.user; } const group = { id: 1 }; if (id) { console.log(`[${userId.substring(0,8)}]: Attempting load of ${id}`); } else { console.log(`[${userId.substring(0,8)}]: Creating new group.`); } console.log(`[${userId.substring(0,8)}]: ${group.id} loaded.`); return res.status(200).send({ id: group.id }); }); module.exports = router;