200 lines
3.5 KiB
JavaScript
200 lines
3.5 KiB
JavaScript
const bcrypt = require("bcrypt");
|
|
|
|
const db = require("../config/database");
|
|
const HouseholdService = require("../services/HouseholdService");
|
|
|
|
|
|
|
|
class AuthController {
|
|
|
|
|
|
registerForm(req, res) {
|
|
|
|
res.locals.title = "Registreren";
|
|
|
|
res.render("auth/register");
|
|
|
|
}
|
|
|
|
|
|
async register(req, res) {
|
|
|
|
const name = (req.body.name || "").trim();
|
|
const email = (req.body.email || "").trim().toLowerCase();
|
|
const password = req.body.password || "";
|
|
const inviteCode = (req.body.household_code || "").trim();
|
|
|
|
if (name.length < 2 || !/^\S+@\S+\.\S+$/.test(email) || password.length < 8) {
|
|
req.flash("error", "Vul een naam, geldig e-mailadres en een wachtwoord van minimaal 8 tekens in.");
|
|
return res.redirect("/register");
|
|
}
|
|
|
|
|
|
const existingUser = db.prepare(
|
|
"SELECT id FROM users WHERE email = ?"
|
|
).get(email);
|
|
|
|
|
|
if (existingUser) {
|
|
|
|
req.flash(
|
|
"error",
|
|
"E-mailadres bestaat al"
|
|
);
|
|
|
|
return res.redirect("/register");
|
|
|
|
}
|
|
|
|
const invitedHousehold = inviteCode
|
|
? HouseholdService.findByInviteCode(inviteCode)
|
|
: null;
|
|
|
|
if (inviteCode && !invitedHousehold) {
|
|
req.flash("error", "De huishoudcode bestaat niet.");
|
|
return res.redirect("/register");
|
|
}
|
|
|
|
|
|
const passwordHash = await bcrypt.hash(
|
|
password,
|
|
12
|
|
);
|
|
|
|
|
|
const result = db.prepare(`
|
|
|
|
INSERT INTO users
|
|
(
|
|
name,
|
|
email,
|
|
password
|
|
)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
`).run(
|
|
name,
|
|
email,
|
|
passwordHash
|
|
);
|
|
|
|
|
|
const userId = result.lastInsertRowid;
|
|
|
|
|
|
if (invitedHousehold) {
|
|
HouseholdService.addMember(invitedHousehold.id, userId);
|
|
} else {
|
|
HouseholdService.createForUser(
|
|
userId,
|
|
`${name} huishouden`
|
|
);
|
|
}
|
|
|
|
|
|
req.session.userId = userId;
|
|
|
|
req.flash(
|
|
"success",
|
|
"Welkom bij PantryHub!"
|
|
);
|
|
|
|
res.redirect("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
loginForm(req, res) {
|
|
|
|
res.locals.title = "Inloggen";
|
|
|
|
if (req.query.logout) {
|
|
|
|
req.flash(
|
|
"info",
|
|
"Je bent uitgelogd."
|
|
);
|
|
|
|
}
|
|
|
|
res.render("auth/login");
|
|
|
|
}
|
|
|
|
|
|
|
|
async login(req, res) {
|
|
|
|
const email = (req.body.email || "").trim().toLowerCase();
|
|
const password = req.body.password || "";
|
|
|
|
|
|
const user = db.prepare(
|
|
"SELECT * FROM users WHERE email = ?"
|
|
).get(email);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
req.flash(
|
|
"error",
|
|
"Ongeldige gegevens"
|
|
);
|
|
|
|
return res.redirect("/login");
|
|
|
|
}
|
|
|
|
|
|
const valid = await bcrypt.compare(
|
|
password,
|
|
user.password
|
|
);
|
|
|
|
|
|
if (!valid) {
|
|
|
|
req.flash(
|
|
"error",
|
|
"Ongeldige gegevens"
|
|
);
|
|
|
|
return res.redirect("/login");
|
|
|
|
}
|
|
|
|
|
|
req.session.userId = user.id;
|
|
|
|
req.flash(
|
|
"success",
|
|
`Welkom terug ${user.name}!`
|
|
);
|
|
|
|
res.redirect("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
logout(req, res) {
|
|
|
|
req.session.destroy(() => {
|
|
|
|
res.clearCookie("connect.sid");
|
|
|
|
res.redirect("/login?logout=1");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
module.exports = new AuthController();
|