node-go/packages/server/controllers/auth.js

89 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-06-27 00:35:13 +00:00
const { validationResult } = require("express-validator");
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");
const guestServices = require("../services/guestServices");
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) => {
checkValidationErrors(req, res);
const user = req.body;
try {
2020-01-16 04:04:55 +00:00
delete user.confirmPassword;
const existingUser = await userQueries.findUserByNameOrEmail(user);
2020-01-14 22:22:42 +00:00
const hashedPassword = await hashPassword(user.password);
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-16 04:04:55 +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-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) => {
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 {
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
if (!savedUser) {
2020-06-27 00:35:13 +00:00
return res.status(401).send({ errors: "bad credentials" });
}
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);
if (!passwordMatch) {
2020-06-27 00:35:13 +00:00
return res.status(401).send({ errors: "bad credentials" });
}
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}`
const { username, password } = guestServices.generateGuest();
2020-06-27 00:35:13 +00:00
// generateGuestUser();
const email = null;
// id generator returns `
const id = null;
2020-06-27 06:33:56 +00:00
const user = { username, email, id, password };
2020-06-27 00:35:13 +00:00
signToken(res, user);
2020-06-27 06:33:56 +00:00
delete user.password;
2020-06-27 00:35:13 +00:00
res.send(user);
} catch (e) {
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,
};