2020-01-13 22:47:45 +00:00
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
require('dotenv').config();
|
|
|
|
|
|
|
|
const msDayOffset = 86400000;
|
|
|
|
const msHourOffset = 3600000;
|
|
|
|
|
|
|
|
const signToken = (res, user) => {
|
|
|
|
const expiration = process.env.NODE_ENV === 'test' ? msHourOffset : msDayOffset;
|
|
|
|
const secret = process.env.NODE_ENV === 'test' ? process.env.TEST_SECRET : process.env.JWT_SECRET;
|
|
|
|
const token = jwt.sign({ user }, secret, {
|
|
|
|
expiresIn: process.env.NODE_ENV === 'test' ? '1h' : '24h',
|
|
|
|
});
|
|
|
|
return res.cookie('token', token, {
|
|
|
|
expires: new Date(Date.now() + expiration),
|
2020-01-18 05:18:50 +00:00
|
|
|
domain: process.env.DOMAIN,
|
2020-01-19 07:53:42 +00:00
|
|
|
secure: process.env.NODE_ENV === 'production' ? true : false,
|
|
|
|
httpOnly: true,
|
|
|
|
sameSite: 'Strict'
|
2020-01-13 22:47:45 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
module.exports = signToken;
|