JavaScript
1
Вклад в тег
const jwt = require('jsonwebtoken');
const secret = process.env.TOKEN_SECRET || 'some other secret as default';
const tokenLife = +process.env.TOKEN_LIFE || 3600;
module.exports = (req, res) => {
const { refreshToken } = req.body;
jwt.verify(refreshToken, secret, (err, decoded) => {
if (err) {
return res.status(401).send('Unauthorised');
}
if (decoded) {
const payload = {
id: decoded.id,
email: decoded.email,
};
jwt.sign(payload, secret, { expiresIn: tokenLife }, (Error, token) => {
if (Error) {
return res.status(401).send('Unauthorised');
}
res.send({
success: true,
accessToken: `Bearer ${token}`,
});
});
}
});
};