node-go/packages/play-node-go/server/services/signToken.js
2020-02-05 16:44:31 -08:00

19 lines
No EOL
656 B
JavaScript

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),
secure: false, // set to true if your using https
httpOnly: true,
});
};
module.exports = signToken;