Adding “Forgot Password?” and “Resend Access” to the Hotel‑IT‑Ops Dashboard Login Flow

2026-11-30来源 dev.toEnglish

Adding “Forgot Password?” and “Resend Access” to the Hotel‑IT‑Ops Dashboard Login Flow

TL;DR: I introduced a password‑reset endpoint and a “resend access” UI to the dashboard’s auth flow. The change required new API routes, a TTL tweak in the cookie helper, and a handful of UI updates in index.html and app.js.

The Problem

The original login screen only let users type their credentials. When a user forgot their password or never received the initial access email, there was no way to recover or request a new one. The symptom was a dead‑end UI and a support backlog of tickets like:

POST /api/auth/login → 200 OK but user never gets an email to reset password 

From a security standpoint we also needed a short‑lived OTP for password changes, but the existing TTL constants in lib/http.js gave the OTP a 10‑minute window and the full session a 6‑day window, which was too permissive for a password‑reset flow.

What I Tried First

My first attempt was to hack the existing api/auth/login route: I added a query param ?reset=true that would trigger a password‑reset email. It worked locally, but it polluted the login logic, made the route harder to test, and broke the existing rate‑limiting middleware. Moreover, the UI had no place to expose that flag, so the approach was dead‑ended.

The next idea was to create a separate endpoint (/api/auth/reset) but I left the TTL unchanged, which caused the OTP to expire before most users could click the email link (especially on mobile networks). The result was a “code expired” error that confused users.

The Implementation

1. New Admin Action – reset (api/admin/[action].js)

I added a dedicated reset handler that validates the request, looks up the user, generates a one‑time code, and sends the email. The diff added ~31 lines and removed 9 obsolete checks.

// api/admin/[action].js async function reset(req, res, me) { if (!guard(req, res, "POST")) return; const { email } = req.body; if (!email) return send(res, 400, { error: "Email required" }); // Find the user const user = await sql` SELECT id, name, email FROM users WHERE email = ${email} `.first(); if (!user) return send(res, 404, { error: "User not found" }); // Issue a short‑lived OTP const code = await issueCode(user.id, "reset"); await sendResetEmail(user.email, code); // <- helper in lib/mail.js send(res, 200, { message: "Reset email sent" }); } 

The function lives alongside the existing update and delete admin actions, keeping the admin API cohesive.

2. Auth Action Enhancements (api/auth/[action].js)

The auth module now exports two new actions: requestReset (public) and confirmReset (private). The diff added ~64 lines.

// api/auth/[action].js import { sql, audit } from "../../lib/db.js"; import { verifyPassword, hashPassword, passwordProblems, } from "../../lib/crypto.js"; import { issueCode, checkCode } from "../../lib/otp.js"; // Public: user asks for a reset link export async function requestReset(req, res) { if (!guard(req, res, "POST")) return; const { email } = req.body; const user = await sql` SELECT id, email FROM users WHERE email = ${email} `.first(); if (!user) return send(res, 404, { error: "User not found" }); const code = await issueCode(user.id, "pwchange"); await sendResetEmail(user.email, code); send(res, 200, { message: "Reset code sent" }); } // Private: user submits new password + OTP export async function confirmReset(req, res) { if (!guard(req, res, "POST")) return; const { token, newPassword } = req.body; const payload = await checkCode(token, "pwchange"); if (!payload) return send(res, 400, { error: "Invalid or expired token" }); const problems = passwordProblems(newPassword); if (problems) return send(res, 400, { error: problems }); const hashed = await hashPassword(newPassword); await sql` UPDATE users SET password = ${hashed} WHERE id = ${payload.userId} `; audit(req, "password_reset", { userId: payload.userId }); send(res, 200, { message: "Password updated" }); } 

Notice the separation of concerns: the public endpoint only sends the OTP, while the private endpoint validates the token and updates the password.

3. Shortening OTP TTL (lib/http.js)

The OTP TTL for password changes was too long. I changed the constant from 15 minutes to 5 minutes, aligning it with typical security recommendations.

// lib/http.js export const TTL = { otp: 10 * 60, // 10 minutes for generic OTP pwchange: 5 * 60, // 5 minutes for password change full: 8 * 60 * 6, // 6 days for full session }; 

Only a single line changed, but it propagates through issueCode and checkCode without further code modifications.

4. UI – Adding Links (public/index.html)

Two new UI elements were added:

<!-- Near the login form --> <a href="#" id="forgotPwdLink" class="link">¿Olvidaste tu contraseña?</a> <!-- In the “general view” after a successful login --> <button id="resendAccessBtn" class="btn">Reenviar acceso</button> 

The diff added ~30 lines to the HTML, placing the link right under the password input and the button in the overview panel.

5. Front‑end Logic (public/js/app.js)

The JavaScript needed to toggle the new steps and call the new API endpoints. The main changes were in the showStep helper and the new requestReset flow.

 js // public/js/app.js function showStep(id) { ['stepLogin', 'stepCode', 'stepChange', 'stepForgot'].forEach(s => el(s).classList.toggle('active', s === id) ); } // Forgot password flow el('forgotPwdLink').addEventListener('click', e => { e.preventDefault(); showStep('stepForgot'); }); el('forgotSubmit').addEventListener('click', async () => { const email = el('forgotEmail').value.trim(); const res = await fetch('/api/auth/requestReset', { method: 'POST', headers: jsonHeaders, body: JSON.stringify({ email }) }); const data = await res.json(); if (res.ok) { alert('Check your inbox for the reset code.'); showStep('stepCode'); } else { alert(data.error || 'Something went wrong'); } }); // Resend access button (admin view) el('resendAccessBtn').addEventListener('click', async () => { const res = await fetch('/api/admin/reset', { method: 'POST', headers: jsonHeaders, body: JSON.stringify({ email: currentUser.email }) }); --- *Part of my [Build in Public](https://dev.to/zaerohell) series — sharing the real process of building SaaS projects from Playa del Carmen, México.* *Repo: `zaerohell/hotel-it-ops-dashboard` · 2026-11-28* \#playadev #buildinpublic 

原文:https://www.a8q9.cn/en/article/adding-forgot-password-and-resend-access-to-the-hotel-it-ops-dashboard-login-flo-4b22f5.html

相关文章