// Email/password auth + the sign-in page (which also hosts passkey + CoinPay buttons). import { Router } from "express"; import { page, footer, esc } from "../lib/html.mjs"; import { csrfInput, createSession, destroySession, takeNext } from "../lib/session.mjs"; import { hashPassword, verifyPassword, id, token, sha256 } from "../lib/crypto.mjs"; import { createUserWithPassword, userByEmail } from "../lib/users.mjs"; import { get, run, batch } from "../db.mjs"; import { dashboardHandler } from "./pages.mjs"; import { config } from "../config.mjs"; import { ringFooter } from "../lib/ring-footer.mjs"; import { renderProfileFields, saveSignupProfile, validateProfile } from "@profullstack/profile"; import { profiles } from "../lib/profile.mjs"; // Asked for when creating an account, beside the email box; never on sign-in. const SIGNUP_FIELDS = ["first_name", "last_name", "company_domain", "job_title", "linkedin_url"]; export const authRouter = Router(); function authPage(req, { error = "", notice = "", mode = "in", values = {}, errors = {} } = {}) { const isForgot = mode === "forgot"; const isReset = mode === "reset" || mode === "recover"; const isSignup = mode === "up"; const action = isForgot ? "/auth/forgot" : mode === "recover" ? "/auth/recover" : isReset ? "/auth/reset" : isSignup ? "/auth/register" : "/auth/login"; const body = `
LSLogicSRCcredentials

Share secrets, end-to-end encrypted

${error ? `
${esc(error)}
` : ""} ${notice ? `
${esc(notice)}
` : ""}
${csrfInput(req)} ${isReset ? `` : ""} ${isSignup ? renderProfileFields({ fields: SIGNUP_FIELDS, values, errors, idPrefix: "signup-" }) : ""} ${isForgot ? "" : ``} ${isReset ? `` : ""}

${isForgot || isReset ? `Back to sign in` : isSignup ? `Already have an account? Sign in` : `New here? Create account`}

${mode === "in" ? `

Forgot password?

` : ""} ${isForgot || isReset ? "" : `
or
`} ${isForgot || isReset ? "" : ` ${config.coinpayLoginEnabled ? `â—† Continue with CoinPay` : ``}

`}
${footer}${ringFooter()} ${isForgot || isReset ? "" : ``}${isSignup ? `` : ""}`; return page({ title: "LogicSRC ▸ sign in", body }); } async function sendPasswordResetEmail(to, rawToken) { if (!config.resend.apiKey) return false; const url = `${config.origin}/auth/reset?token=${encodeURIComponent(rawToken)}`; const response = await fetch("https://api.resend.com/emails", { method: "POST", headers: { authorization: `Bearer ${config.resend.apiKey}`, "content-type": "application/json" }, body: JSON.stringify({ from: config.resend.from, to, subject: "Reset your LogicSRC password", text: `Someone requested a password reset for your LogicSRC account.\n\nReset it here: ${url}\n\nThis link expires in 30 minutes and can only be used once. If you did not request it, you can ignore this email.`, }), }).catch(() => null); return Boolean(response && response.ok); } authRouter.get("/", (req, res) => { if (req.user) { const next = takeNext(req, res); if (next) return res.redirect(next); return dashboardHandler(req, res); // dashboard lives at the root } res.type("html").send(authPage(req, { mode: req.query.mode === "up" ? "up" : req.query.mode === "forgot" ? "forgot" : "in", notice: req.query.reset === "1" ? "Password reset. You can sign in with your new password." : "", })); }); authRouter.get("/auth/reset", async (req, res) => { const rawToken = String(req.query.token || ""); const reset = rawToken ? await get( `SELECT u.email FROM password_resets r JOIN users u ON u.id = r.user_id WHERE r.token_hash = ? AND r.used_at IS NULL AND r.expires_at > ?`, [sha256(rawToken), Date.now()], ) : null; if (!reset) return res.type("html").send(authPage(req, { mode: "in", error: "That reset link is invalid or expired. Request a new one." })); res.type("html").send(authPage(req, { mode: "reset", values: { email: reset.email, token: rawToken } })); }); authRouter.get("/auth/recover", async (req, res) => { const rawToken = String(req.query.token || ""); const recovery = rawToken ? await get( `SELECT u.email FROM recovery_tokens r JOIN users u ON u.id = r.user_id WHERE r.token_hash = ? AND r.used_at IS NULL AND r.revoked_at IS NULL AND r.expires_at > ?`, [sha256(rawToken), Date.now()], ) : null; if (!recovery) return res.type("html").send(authPage(req, { mode: "in", error: "That recovery token is invalid, revoked, or already used." })); res.type("html").send(authPage(req, { mode: "recover", values: { email: recovery.email, token: rawToken } })); }); authRouter.post("/auth/register", async (req, res) => { const email = String(req.body.email || "").trim().toLowerCase(); const password = String(req.body.password || ""); // What was typed comes back on a refused form (never the password). const values = Object.fromEntries(["email", ...SIGNUP_FIELDS].map((f) => [f, String(req.body[f] ?? "")])); const refuse = (error, errors = {}) => res.type("html").send(authPage(req, { mode: "up", error, values, errors })); if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email)) return refuse("Enter a valid email."); // The profile is checked before the account exists, so a refused form leaves nothing half-made. const check = validateProfile(req.body); if (!check.ok) return refuse("A few of those fields need another look.", check.errors); if (password.length < 8) return refuse("Password must be at least 8 characters."); if (await userByEmail(email)) return refuse("That email already has an account — sign in."); const user = await createUserWithPassword(email, hashPassword(password)); // A failed profile write never fails the signup; Settings can fill it in. try { await saveSignupProfile(profiles, user.id, req.body, { email }); } catch (e) { console.error("signup profile save failed:", e); } await createSession(res, user.id); res.redirect(takeNext(req, res) || "/"); }); authRouter.post("/auth/login", async (req, res) => { const email = String(req.body.email || "").trim().toLowerCase(); const password = String(req.body.password || ""); const user = await userByEmail(email); if (!user || !verifyPassword(password, user.password_hash)) { return res.type("html").send(authPage(req, { mode: "in", error: "Wrong email or password." })); } await createSession(res, user.id); res.redirect(takeNext(req, res) || "/"); }); authRouter.post("/auth/forgot", async (req, res) => { const email = String(req.body.email || "").trim().toLowerCase(); const user = /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email) ? await userByEmail(email) : null; if (user) { const rawToken = token(32); await run(`DELETE FROM password_resets WHERE user_id = ? OR expires_at <= ?`, [user.id, Date.now()]); await run(`INSERT INTO password_resets (id, user_id, token_hash, created_at, expires_at) VALUES (?,?,?,?,?)`, [id(), user.id, sha256(rawToken), Date.now(), Date.now() + 1000 * 60 * 30]); await sendPasswordResetEmail(email, rawToken); } res.type("html").send(authPage(req, { mode: "forgot", values: { email }, notice: "If an account exists for that email, a reset link is on its way.", })); }); authRouter.post("/auth/reset", async (req, res) => { const rawToken = String(req.body.token || ""); const password = String(req.body.password || ""); const confirmation = String(req.body.password_confirmation || ""); const reset = rawToken ? await get( `SELECT r.id, r.user_id, u.email FROM password_resets r JOIN users u ON u.id = r.user_id WHERE r.token_hash = ? AND r.used_at IS NULL AND r.expires_at > ?`, [sha256(rawToken), Date.now()], ) : null; if (!reset) return res.type("html").send(authPage(req, { mode: "in", error: "That reset link is invalid or expired. Request a new one." })); if (password.length < 8) return res.type("html").send(authPage(req, { mode: "reset", error: "Password must be at least 8 characters.", values: { email: reset.email, token: rawToken } })); if (password !== confirmation) return res.type("html").send(authPage(req, { mode: "reset", error: "Passwords do not match.", values: { email: reset.email, token: rawToken } })); const now = Date.now(); await batch([ { sql: `UPDATE users SET password_hash = ? WHERE id = ?`, args: [hashPassword(password), reset.user_id] }, { sql: `UPDATE password_resets SET used_at = ? WHERE id = ?`, args: [now, reset.id] }, { sql: `DELETE FROM sessions WHERE user_id = ?`, args: [reset.user_id] }, ]); res.redirect("/?reset=1"); }); authRouter.post("/auth/recover", async (req, res) => { const rawToken = String(req.body.token || ""); const password = String(req.body.password || ""); const confirmation = String(req.body.password_confirmation || ""); const recovery = rawToken ? await get( `SELECT r.id, r.user_id, u.email FROM recovery_tokens r JOIN users u ON u.id = r.user_id WHERE r.token_hash = ? AND r.used_at IS NULL AND r.revoked_at IS NULL AND r.expires_at > ?`, [sha256(rawToken), Date.now()], ) : null; if (!recovery) return res.type("html").send(authPage(req, { mode: "in", error: "That recovery token is invalid, revoked, or already used." })); if (password.length < 8) return res.type("html").send(authPage(req, { mode: "recover", error: "Password must be at least 8 characters.", values: { email: recovery.email, token: rawToken } })); if (password !== confirmation) return res.type("html").send(authPage(req, { mode: "recover", error: "Passwords do not match.", values: { email: recovery.email, token: rawToken } })); const now = Date.now(); await batch([ { sql: `UPDATE users SET password_hash = ? WHERE id = ?`, args: [hashPassword(password), recovery.user_id] }, { sql: `UPDATE recovery_tokens SET used_at = ? WHERE id = ?`, args: [now, recovery.id] }, { sql: `DELETE FROM sessions WHERE user_id = ?`, args: [recovery.user_id] }, ]); res.redirect("/?reset=1"); }); authRouter.post("/auth/logout", async (req, res) => { await destroySession(req, res); res.redirect("/"); });