const jwt = require('jsonwebtoken');
const cfg = require('./config');

function issue (payload, req, res) {

	const token = jwt.sign(payload, cfg.jwt.secret, cfg.jwt.options);

	if (req) {
		req.user = payload;
	}

	if (res) {
		res.cookie("JWT", token, {maxAge: cfg.jwt.options.expiresIn*1000 || 0});
	}

	return token;
}

module.exports = {
	issue
};
