feat(auth): Enforce global email verification for all users, restrict dashboard if unverified, add verification badges and banners

This commit is contained in:
Mohan Ki
2026-07-27 19:11:58 +05:30
parent 836693192e
commit 4fe25f3de5
5 changed files with 124 additions and 18 deletions
+21 -1
View File
@@ -37,7 +37,27 @@ router.post('/register', async (req, res) => {
[email, hash, assignedRole]
);
const token = jwt.sign({ id: newUser.rows[0].id, email: newUser.rows[0].email, role: newUser.rows[0].role }, JWT_SECRET, { expiresIn: '14d' });
const userId = newUser.rows[0].id;
const userEmail = newUser.rows[0].email;
// Auto send verification email
const tokenStr = crypto.randomBytes(32).toString('hex');
await pgPool.query('UPDATE users SET email_verification_token = $1 WHERE id = $2', [tokenStr, userId]);
const baseUrl = process.env.FRONTEND_URL || 'https://qr.houseofwebsites.ai';
const verifyLink = `${baseUrl}/verify-email?token=${tokenStr}`;
const html = `
<h2>Verify Your Email</h2>
<p>Thanks for registering for CK-QR! Please verify your email address to unlock your dashboard features.</p>
<a href="${verifyLink}" style="display:inline-block;padding:10px 20px;background:#4f46e5;color:#fff;text-decoration:none;border-radius:5px;">Verify Email</a>
<p>Or copy and paste this link: <br> ${verifyLink}</p>
`;
// Send email asynchronously without blocking registration response
sendMail(userEmail, 'Verify your CK-QR account', html).catch(e => console.error('Failed to send auto-verify email:', e));
const token = jwt.sign({ id: userId, email: userEmail, role: newUser.rows[0].role }, JWT_SECRET, { expiresIn: '14d' });
res.cookie('token', token, { httpOnly: true, maxAge: 14 * 24 * 60 * 60 * 1000, secure: false, sameSite: 'lax' });
res.status(201).json({ message: 'User registered', user: newUser.rows[0] });