2020-06-27 00:35:13 +00:00
|
|
|
const { validationResult } = require("express-validator");
|
2020-01-17 08:23:18 +00:00
|
|
|
|
2020-06-27 00:35:13 +00:00
|
|
|
const userQueries = require("../data/queries/user");
|
|
|
|
const { hashPassword, compareHash } = require("../services/bcrypt");
|
|
|
|
const signToken = require("../services/signToken");
|
2020-06-27 06:22:17 +00:00
|
|
|
const guestServices = require("../services/guestServices");
|
2020-01-15 21:52:01 +00:00
|
|
|
|
|
|
|
const checkValidationErrors = (req, res) => {
|
|
|
|
const errors = validationResult(req);
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
return res.status(422).json({ errors: errors.array() });
|
|
|
|
}
|
2020-06-27 00:35:13 +00:00
|
|
|
};
|
2020-01-10 01:44:58 +00:00
|
|
|
|
2020-01-15 07:09:01 +00:00
|
|
|
const signup = async (req, res, next) => {
|
2020-01-15 21:52:01 +00:00
|
|
|
checkValidationErrors(req, res);
|
2020-01-13 22:47:45 +00:00
|
|
|
const user = req.body;
|
|
|
|
try {
|
2020-01-16 04:04:55 +00:00
|
|
|
delete user.confirmPassword;
|
2020-01-22 01:02:54 +00:00
|
|
|
const existingUser = await userQueries.findUserByNameOrEmail(user);
|
2020-01-14 22:22:42 +00:00
|
|
|
const hashedPassword = await hashPassword(user.password);
|
2020-01-17 08:23:18 +00:00
|
|
|
const secureUser = { ...user, password: hashedPassword };
|
|
|
|
if (existingUser.length) {
|
2020-06-27 00:35:13 +00:00
|
|
|
return res
|
|
|
|
.status(409)
|
|
|
|
.json({ errors: [{ auth: "User already exists!" }] });
|
2020-01-17 08:23:18 +00:00
|
|
|
}
|
2020-01-16 04:04:55 +00:00
|
|
|
|
2020-01-22 01:02:54 +00:00
|
|
|
const newUser = await userQueries.insertUser(secureUser);
|
2020-06-27 00:35:13 +00:00
|
|
|
signToken(res, newUser);
|
|
|
|
res.status(201).json({ ...newUser });
|
|
|
|
} catch (err) {
|
2020-01-16 04:04:55 +00:00
|
|
|
res.status(500).json(err);
|
2020-01-13 22:47:45 +00:00
|
|
|
}
|
2020-06-27 00:35:13 +00:00
|
|
|
};
|
2020-01-10 01:44:58 +00:00
|
|
|
|
2020-01-15 07:09:01 +00:00
|
|
|
const login = async (req, res, next) => {
|
2020-01-15 21:52:01 +00:00
|
|
|
checkValidationErrors(req, res);
|
2020-01-15 07:09:01 +00:00
|
|
|
const user = req.body;
|
2020-06-27 00:35:13 +00:00
|
|
|
|
2020-01-15 07:09:01 +00:00
|
|
|
try {
|
2020-01-17 08:23:18 +00:00
|
|
|
const queryResults = await userQueries.findUserByNameOrEmail(user);
|
2020-01-15 07:09:01 +00:00
|
|
|
const savedUser = queryResults[0] || null;
|
2020-06-27 00:35:13 +00:00
|
|
|
|
2020-01-17 08:23:18 +00:00
|
|
|
if (!savedUser) {
|
2020-06-27 00:35:13 +00:00
|
|
|
return res.status(401).send({ errors: "bad credentials" });
|
2020-01-17 08:23:18 +00:00
|
|
|
}
|
2020-06-27 00:35:13 +00:00
|
|
|
|
2020-01-15 07:09:01 +00:00
|
|
|
const hashedPassword = savedUser.password;
|
|
|
|
const passwordMatch = await compareHash(user.password, hashedPassword);
|
2020-01-17 08:23:18 +00:00
|
|
|
|
|
|
|
if (!passwordMatch) {
|
2020-06-27 00:35:13 +00:00
|
|
|
return res.status(401).send({ errors: "bad credentials" });
|
2020-01-17 08:23:18 +00:00
|
|
|
}
|
2020-06-27 00:35:13 +00:00
|
|
|
|
|
|
|
const authorizedUser = { ...savedUser };
|
2020-01-15 07:09:01 +00:00
|
|
|
delete authorizedUser.password;
|
2020-06-27 00:35:13 +00:00
|
|
|
|
2020-01-15 07:09:01 +00:00
|
|
|
signToken(res, authorizedUser);
|
2020-06-27 00:35:13 +00:00
|
|
|
res.send({ ...authorizedUser }).status(200);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send({ errors: e });
|
2020-01-15 07:09:01 +00:00
|
|
|
}
|
2020-06-27 00:35:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const guest = async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
// username generator returns `Guest-${num}`
|
2020-06-27 06:22:17 +00:00
|
|
|
const { username, password } = guestServices.generateGuest();
|
2020-06-27 00:35:13 +00:00
|
|
|
// generateGuestUser();
|
|
|
|
const email = null;
|
|
|
|
// id generator returns `
|
|
|
|
const id = null;
|
|
|
|
const user = { username, email, id };
|
|
|
|
signToken(res, user);
|
|
|
|
res.send(user);
|
|
|
|
} catch (e) {
|
2020-06-27 06:22:17 +00:00
|
|
|
console.log(e);
|
2020-06-27 00:35:13 +00:00
|
|
|
res.status(500).send({ errors: e });
|
2020-01-15 07:09:01 +00:00
|
|
|
}
|
2020-06-27 00:35:13 +00:00
|
|
|
};
|
2020-01-10 01:44:58 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2020-01-15 07:09:01 +00:00
|
|
|
signup,
|
2020-06-27 00:35:13 +00:00
|
|
|
login,
|
|
|
|
guest,
|
|
|
|
};
|