reset password

Should check if can reset pass of another user
This commit is contained in:
2020-09-12 20:47:41 +02:00
parent 4d2d62d777
commit cec24fa01a
9 changed files with 160 additions and 73 deletions

View File

@@ -5,7 +5,12 @@ import {
fetchRestaurant,
fetchAllDishesForRestaurant,
} from "../services/databaseServices.js";
import * as services from "../services/services.js";
import {
decodeAndSanitize,
validateRestaurant,
handleError,
validateUserToken,
} from "../services/services.js";
import Restaurant from "../models/restaurant.js";
var router = express.Router();
@@ -14,11 +19,11 @@ var router = express.Router();
router.get("/", async (req, res) => {
try {
const query = services.decodeAndSanitize(req.query.restaurantId);
await services.validateRestaurant(query);
const query = decodeAndSanitize(req.query.restaurantId);
await validateRestaurant(query);
Restaurant.findById(query).then((data) => res.send(data));
} catch (error) {
services.handleError(error, res);
handleError(error, res);
}
});
@@ -27,13 +32,13 @@ router.get("/", async (req, res) => {
router.post("/", async (req, res) => {
try {
const token = req.headers["x-auth-token"];
const user = services.validateUserToken(token);
const user = validateUserToken(token);
const restaurant = createRestaurant(req.body);
await restaurant.save();
await addRestaurantToUser(user, restaurant);
res.sendStatus(201);
} catch (error) {
services.handleError(error, res);
handleError(error, res);
}
});
@@ -41,13 +46,28 @@ router.post("/", async (req, res) => {
router.get("/dishes", async (req, res) => {
try {
const query = services.decodeAndSanitize(req.query.restaurantId);
await services.validateRestaurant(query);
const query = decodeAndSanitize(req.query.restaurantId);
await validateRestaurant(query);
let restaurant = await fetchRestaurant(query);
let dishes = await fetchAllDishesForRestaurant(restaurant);
res.send(dishes);
} catch (error) {
services.handleError(error, res);
handleError(error, res);
}
});
// DELETE RESTAURANT
router.post("/delete", async (req, res) => {
try {
const token = req.headers["x-auth-token"];
const user = validateUserToken(token);
await validateRestaurant(req.body.restaurantId);
//check access
//delete restaurant
res.send("Restauracja została pomyślnie usunięta.");
} catch (error) {
handleError(error, res);
}
});

View File

@@ -26,7 +26,7 @@ var agileAPI = new AgileCRMManager(CRM_USER, CRM_KEY, CRM_EMAIL);
router.post("/login", async (req, res) => {
try {
if (!req.body.password || !req.body.email) {
throw newError("No input data", 204);
throw newError("Niepełne dane.", 204);
}
const user = await fetchUser(req.body.email);
await checkPassword(req.body.password, user.password);
@@ -56,7 +56,7 @@ router.post("/register", async (req, res) => {
router.post("/changepass", async (req, res) => {
try {
if (!req.body.password || !req.body.email || !req.body.newPass) {
throw newError("No input data", 204);
throw newError("Niepełne dane.", 204);
}
const token = req.headers["x-auth-token"];
validateUserToken(token);
@@ -64,7 +64,7 @@ router.post("/changepass", async (req, res) => {
await checkPassword(req.body.password, user.password);
const newPassword = await hashPass(req.body.newPass);
await changeUserPass(user._id, newPassword);
res.status(200).send("Password changed");
res.status(200).send("Hasło zostało zmienione.");
} catch (error) {
handleError(error, res);
}
@@ -83,5 +83,17 @@ router.post("/forgotpassword", async (req, res) => {
});
// RESET PASS
router.post("/resetpass", async (req, res) => {
try {
validateUserToken(req.body.token);
const user = await fetchUser(req.body.email);
const newPassword = await hashPass(req.body.newPass);
await changeUserPass(user._id, newPassword);
res.send("Hasło zostało zmienione.");
} catch (error) {
console.log(error);
handleError(error, res);
}
});
export default router;