Cleanup
This commit is contained in:
@@ -4,10 +4,18 @@ const User = require("../models/users.js");
|
||||
const mongoose = require("mongoose");
|
||||
const sanitizer = require("string-sanitizer");
|
||||
const { renameBlob } = require("./oceanServices.js");
|
||||
const jwt = require("jsonwebtoken");
|
||||
const bcrypt = require("bcryptjs");
|
||||
const crypto = require("crypto")
|
||||
const { jwtSecret, publicKey } = require("../config/index.js");
|
||||
|
||||
// AUTH
|
||||
|
||||
function authenticate() {
|
||||
auth({
|
||||
audience: "https://menui.pl/api",
|
||||
issuerBaseURL: "https://bankaisoftware.eu.auth0.com/",
|
||||
tokenSigningAlg: "RS256",
|
||||
});
|
||||
}
|
||||
|
||||
// NEW ERROR
|
||||
|
||||
function newError(message, status) {
|
||||
const error = {
|
||||
@@ -17,6 +25,8 @@ function newError(message, status) {
|
||||
return error;
|
||||
}
|
||||
|
||||
// HANDLE ERROR
|
||||
|
||||
function handleError(error, responseObject) {
|
||||
if (!error.status) {
|
||||
console.log(error);
|
||||
@@ -26,6 +36,8 @@ function handleError(error, responseObject) {
|
||||
}
|
||||
}
|
||||
|
||||
// VALIDATE RESTAURANT ID
|
||||
|
||||
async function validateRestaurant(id) {
|
||||
if (!mongoose.Types.ObjectId.isValid(id))
|
||||
throw newError("Nieprawidłowy ID", 204);
|
||||
@@ -34,87 +46,14 @@ async function validateRestaurant(id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// DECODE AND SANITIZE URL
|
||||
|
||||
function decodeAndSanitize(query) {
|
||||
if (!query) throw newError("Brak danych.", 204);
|
||||
return sanitizer.sanitize.keepUnicode(decodeURI(query));
|
||||
}
|
||||
|
||||
async function checkPassword(password, hash) {
|
||||
const result = await bcrypt.compare(password, hash);
|
||||
if (!result) throw newError("Hasło nieprawidłowe", 403);
|
||||
}
|
||||
|
||||
function generateAuthToken(user) {
|
||||
const token = jwt.sign(
|
||||
{
|
||||
email: user.email,
|
||||
id: user.id,
|
||||
},
|
||||
jwtSecret,
|
||||
{ expiresIn: "15m" }
|
||||
);
|
||||
return token;
|
||||
}
|
||||
|
||||
function generateRefreshToken(user) {
|
||||
const token = jwt.sign({
|
||||
email: user.email,
|
||||
id: user.id,
|
||||
}, jwtSecret, {
|
||||
expiresIn: "1h"
|
||||
});
|
||||
return token;
|
||||
}
|
||||
|
||||
function generatePasswordResetToken(email) {
|
||||
const token = jwt.sign(
|
||||
{
|
||||
email: email,
|
||||
},
|
||||
jwtSecret,
|
||||
{ expiresIn: "30m" }
|
||||
);
|
||||
return token;
|
||||
}
|
||||
|
||||
// SET FRONTEND URL HERE
|
||||
|
||||
function generatePasswordResetLink(email) {
|
||||
const token = generatePasswordResetToken(email);
|
||||
const link = `https://www.menui.pl/resetpassword?token=${token}`;
|
||||
return link;
|
||||
}
|
||||
|
||||
async function checkEmailTaken(email) {
|
||||
if (!email) throw newError("Brak adresu email", 204);
|
||||
await User.exists({ email: email }).then((res) => {
|
||||
if (res) {
|
||||
throw newError("Adres email zajęty", 409);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function validateUserToken(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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
// VALIDATE DISH ID
|
||||
|
||||
async function validateDishId(id) {
|
||||
if (!mongoose.Types.ObjectId.isValid(id)) {
|
||||
@@ -124,6 +63,8 @@ async function validateDishId(id) {
|
||||
if (!dishDoesExist) throw newError("Te danie nie istnieje w bazie.", 404);
|
||||
}
|
||||
|
||||
// VERIFY DISH ACCESS
|
||||
|
||||
async function verifyDishAccess(dishId, decodedToken) {
|
||||
const fetch = await User.findById(decodedToken.id, "restaurants").catch(
|
||||
(error) => {
|
||||
@@ -138,6 +79,8 @@ async function verifyDishAccess(dishId, decodedToken) {
|
||||
if (!valid) throw newError("Nie masz dostępu do tego dania.", 401);
|
||||
}
|
||||
|
||||
// VERIFY RESTAURANT ACCESS
|
||||
|
||||
async function verifyRestaurantAccess(restaurantId, decodedToken) {
|
||||
const fetch = await User.findById(decodedToken.id, "restaurants").catch(
|
||||
(error) => {
|
||||
@@ -149,6 +92,8 @@ async function verifyRestaurantAccess(restaurantId, decodedToken) {
|
||||
if (!valid) throw newError("Nie masz dostępu do tej restauracji.", 401);
|
||||
}
|
||||
|
||||
// YEAR FROM NOW DATE
|
||||
|
||||
function yearFromNowDate() {
|
||||
Date.prototype.addDays = function (days) {
|
||||
var date = new Date(this.valueOf());
|
||||
@@ -159,38 +104,18 @@ function yearFromNowDate() {
|
||||
return date.addDays(365);
|
||||
}
|
||||
|
||||
async function hashPass(pass) {
|
||||
if (pass.length < 6) {
|
||||
throw newError("Hasło za krótkie.", 400);
|
||||
}
|
||||
try {
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
const hash = await bcrypt.hash(pass, salt);
|
||||
return hash;
|
||||
} catch (error) {
|
||||
throw newError("Błąd", 500);
|
||||
}
|
||||
}
|
||||
|
||||
async function saveImage(url) {
|
||||
const newURL = await renameBlob(url);
|
||||
return newURL;
|
||||
}
|
||||
|
||||
exports.authenticate = authenticate;
|
||||
exports.newError = newError;
|
||||
exports.handleError = handleError;
|
||||
exports.validateRestaurant = validateRestaurant;
|
||||
exports.decodeAndSanitize = decodeAndSanitize;
|
||||
exports.checkPassword = checkPassword;
|
||||
exports.generateAuthToken = generateAuthToken;
|
||||
exports.generatePasswordResetLink = generatePasswordResetLink;
|
||||
exports.checkEmailTaken = checkEmailTaken;
|
||||
exports.validateUserToken = validateUserToken;
|
||||
exports.validateDishId = validateDishId;
|
||||
exports.verifyDishAccess = verifyDishAccess;
|
||||
exports.verifyRestaurantAccess = verifyRestaurantAccess;
|
||||
exports.yearFromNowDate = yearFromNowDate;
|
||||
exports.hashPass = hashPass;
|
||||
exports.saveImage = saveImage;
|
||||
exports.generateRefreshToken = generateRefreshToken;
|
||||
exports.validateRefreshToken = validateRefreshToken;
|
||||
|
||||
Reference in New Issue
Block a user