This commit is contained in:
2020-11-20 18:13:08 +01:00
parent 8830303fb4
commit c1eb7d87c5
2 changed files with 37 additions and 2 deletions

View File

@@ -54,11 +54,20 @@ function generateAuthToken(user) {
restaurants: user.restaurants,
},
jwtSecret,
{ expiresIn: "1h" }
{ expiresIn: "15m" }
);
return token;
}
function generateRefreshToken(userId) {
const token = jwt.sign({
id: userId
}, jwtSecret, {
expiresIn: "1h"
});
return token;
}
function generatePasswordResetToken(email) {
const token = jwt.sign(
{
@@ -98,6 +107,17 @@ function validateUserToken(token) {
}
}
function validateRefreshToken(token) {
if (!token) throw newError("Brak dostępu", 401);
try {
const verified = jwt.verify(token, jwtSecret, { ignoreExpiration: false });
if (!verified) throw newError("Brak dostępu", 401);
return verified;
} catch (error) {
throw newError("Brak dostępu", 401);
}
}
async function validateDishId(id) {
if (!mongoose.Types.ObjectId.isValid(id)) {
throw newError("Niewłaściwy ID", 400);
@@ -174,3 +194,5 @@ exports.verifyRestaurantAccess = verifyRestaurantAccess;
exports.yearFromNowDate = yearFromNowDate;
exports.hashPass = hashPass;
exports.saveImage = saveImage;
exports.generateRefreshToken = generateRefreshToken;
exports.validateRefreshToken = validateRefreshToken;