mkcart / backend /utils /generateToken.js
Kumar
updated
c2efbe6
const jwt = require("jsonwebtoken");
const generateToken = (res, userId) => {
const token = jwt.sign({ userId }, process.env.JWT_SECRET_KEY, {
expiresIn: "10d",
});
const isProduction = process.env.NODE_ENV === 'production';
const cookieOptions = {
httpOnly: true,
maxAge: 10 * 24 * 60 * 60 * 1000,
sameSite: isProduction ? 'none' : 'lax',
secure: isProduction,
path: '/',
...(isProduction && process.env.COOKIE_DOMAIN && {
domain: process.env.COOKIE_DOMAIN
})
};
res.cookie("token", token, cookieOptions);
};
module.exports = generateToken;