forked from Bee-Balanced/Server-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.js
More file actions
118 lines (102 loc) · 3.32 KB
/
Copy pathsignup.js
File metadata and controls
118 lines (102 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// signup.js - handles POST signup logic and new user creation
import bcrypt from "bcrypt";
import db from "./db.js";
import {
createEmailVerificationToken,
ensureEmailVerificationColumns,
getEmailVerificationExpiryDate,
sendVerificationEmail,
} from "./verification.js";
// Basic email validation regex
const isValidEmail = (email) => {
const trimmed = email.trim().toLowerCase();
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmed);
};
const ERRORS = {
invalidEmail: "Invalid email format.",
missingFields: "All required fields must be filled in.",
passwordMismatch: "Passwords do not match.",
nameLength: "Name must be between 2 and 100 characters.",
emailInUse: "Email is already registered.",
};
export async function handleSignup(req, res) {
let { name, email, password, confirm_password, age, gender, country } = req.body;
name = name.trim();
email = email.trim().toLowerCase();
gender = gender || "Prefer not to answer";
// Basic field validation
if (!name || !email || !password || !confirm_password) {
return res.render("signup", { error: ERRORS.missingFields });
}
if (name.length < 2 || name.length > 100) {
return res.render("signup", { error: ERRORS.nameLength });
}
if (!isValidEmail(email)) {
return res.render("signup", { error: ERRORS.invalidEmail });
}
if (password !== confirm_password) {
return res.render("signup", { error: ERRORS.passwordMismatch });
}
if (password.length < 6) {
return res.render("signup", { error: "Password must be at least 6 characters long." });
}
let conn;
try {
await ensureEmailVerificationColumns();
conn = await db.getConnection();
const [existingUser] = await conn.query("SELECT id FROM users WHERE LOWER(email) = ?", [email]);
if (existingUser.length > 0) {
return res.render("signup", { error: ERRORS.emailInUse });
}
const hashedPassword = await bcrypt.hash(password, 10);
const verificationToken = createEmailVerificationToken();
const verificationExpiresAt = getEmailVerificationExpiryDate();
await conn.query(
`INSERT INTO users (
full_name,
email,
password,
age,
gender,
country,
unsubscribed,
email_verified,
email_verification_token,
email_verification_expires_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
name,
email,
hashedPassword,
age || null,
gender,
country,
false,
false,
verificationToken,
verificationExpiresAt,
]
);
try {
await sendVerificationEmail({
email,
name,
token: verificationToken,
req,
});
} catch (emailErr) {
console.error("Error sending verification email:", emailErr);
return res.render("login", {
error: "Account created, but we could not send the verification email yet. Please resend verification below.",
message: null,
verificationEmail: email,
});
}
res.redirect(`/login?message=${encodeURIComponent("Account created. Please check your email to verify your account.")}&verificationEmail=${encodeURIComponent(email)}`);
} catch (err) {
console.error("Error during signup:", err);
res.status(500).send("Internal Server Error");
} finally {
if (conn) conn.release();
}
}