mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-12-01 17:24:48 +00:00
feat(pwa): signup profile fields via @profullstack/profile (#256)
The create-account form (/?mode=up) asks for first/last name (required) and company domain, job title, LinkedIn (optional) beside the email box, rendered by renderProfileFields. /auth/register validates them with validateProfile before the user exists and stores them with saveSignupProfile in a new user_profiles table keyed by users.id. GET/PUT/PATCH /api/v1/me/profile (session cookie) and a "Your details" card on /settings. Sign-in, passkey and CoinPay are unchanged. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ff0c903c6d
commit
50eb8ac30d
10 changed files with 230 additions and 6 deletions
|
|
@ -7,10 +7,15 @@ import { createUserWithPassword, userByEmail } from "../lib/users.mjs";
|
|||
import { dashboardHandler } from "./pages.mjs";
|
||||
import { config } from "../config.mjs";
|
||||
import { ringFooter } from "../lib/ring-footer.mjs";
|
||||
import { renderProfileFields, PROFILE_ENHANCE_SCRIPT, 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 = "", mode = "in" } = {}) {
|
||||
function authPage(req, { error = "", mode = "in", values = {}, errors = {} } = {}) {
|
||||
const body = `
|
||||
<main class="wrap" style="max-width:440px;padding-top:8vh">
|
||||
<a class="brand" href="/" style="justify-content:center;font-size:1.5rem;margin-bottom:6px"><span class="mark">LS</span>LogicSRC<span class="app">credentials</span></a>
|
||||
|
|
@ -20,7 +25,8 @@ function authPage(req, { error = "", mode = "in" } = {}) {
|
|||
<form method="post" action="/auth/${mode === "up" ? "register" : "login"}">
|
||||
${csrfInput(req)}
|
||||
<label class="field"><span>Email</span>
|
||||
<input type="email" name="email" autocomplete="username" required placeholder="you@example.com" value="${esc(req.query.email || "")}"></label>
|
||||
<input type="email" name="email" autocomplete="username" required placeholder="you@example.com" value="${esc(values.email ?? req.query.email ?? "")}"></label>
|
||||
${mode === "up" ? renderProfileFields({ fields: SIGNUP_FIELDS, values, errors, idPrefix: "signup-" }) : ""}
|
||||
<label class="field"><span>Password</span>
|
||||
<input type="password" name="password" autocomplete="${mode === "up" ? "new-password" : "current-password"}" required minlength="8" placeholder="8+ characters"></label>
|
||||
<button class="btn acid block" type="submit">${mode === "up" ? "Create account" : "Sign in"}</button>
|
||||
|
|
@ -41,7 +47,7 @@ function authPage(req, { error = "", mode = "in" } = {}) {
|
|||
</div></div>
|
||||
</main>${footer}${ringFooter()}
|
||||
<script src="/vendor/simplewebauthn-browser.umd.js"></script>
|
||||
<script src="/passkey.js"></script>`;
|
||||
<script src="/passkey.js"></script>${mode === "up" ? `<script>${PROFILE_ENHANCE_SCRIPT}</script>` : ""}`;
|
||||
return page({ title: "LogicSRC ▸ sign in", body });
|
||||
}
|
||||
|
||||
|
|
@ -57,10 +63,22 @@ authRouter.get("/", (req, res) => {
|
|||
authRouter.post("/auth/register", async (req, res) => {
|
||||
const email = String(req.body.email || "").trim().toLowerCase();
|
||||
const password = String(req.body.password || "");
|
||||
if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email)) return res.type("html").send(authPage(req, { mode: "up", error: "Enter a valid email." }));
|
||||
if (password.length < 8) return res.type("html").send(authPage(req, { mode: "up", error: "Password must be at least 8 characters." }));
|
||||
if (await userByEmail(email)) return res.type("html").send(authPage(req, { mode: "up", error: "That email already has an account — sign in." }));
|
||||
// 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) || "/");
|
||||
});
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ import { requestOrigin } from "../lib/origin.mjs";
|
|||
import { CLI_HINT } from "../lib/cli-hint.mjs";
|
||||
import { vaultPageBody } from "../lib/vault-page.mjs";
|
||||
import { config } from "../config.mjs";
|
||||
import { COLUMNS, renderProfileFields, validateProfile } from "@profullstack/profile";
|
||||
import { profiles } from "../lib/profile.mjs";
|
||||
import {
|
||||
TeamMemberError,
|
||||
canManageMember,
|
||||
|
|
@ -272,6 +274,9 @@ function keyRow(req, k) {
|
|||
|
||||
pagesRouter.get("/settings", requireAuth, async (req, res) => {
|
||||
const keys = await listApiKeys(req.user.id);
|
||||
const mine = (await profiles.get(req.user.id)) ?? {};
|
||||
const profileValues = Object.fromEntries(COLUMNS.map((c) => [c, mine[c] ?? null]));
|
||||
profileValues.email ??= req.user.email || null;
|
||||
const teams = await all(`SELECT t.slug FROM credshare_teams t JOIN credshare_members m ON m.team_id = t.id WHERE m.user_id = ? AND m.status = 'active' ORDER BY t.slug`, [req.user.id]);
|
||||
const newKey = req.query.key ? String(req.query.key) : "";
|
||||
const err = req.query.err ? String(req.query.err).slice(0, 200) : "";
|
||||
|
|
@ -281,6 +286,14 @@ pagesRouter.get("/settings", requireAuth, async (req, res) => {
|
|||
<h1 style="font-size:1.5rem;margin-bottom:20px">Settings</h1>
|
||||
${newKey ? `<div class="notice ok">New API key (copy it now — shown once):<br><b class="mono" style="word-break:break-all">${esc(newKey)}</b></div>` : ""}
|
||||
${err ? `<div class="notice err">${esc(KEY_ERRORS[err] || err)}</div>` : ""}
|
||||
${req.query.saved === "profile" ? `<div class="notice ok">Your details are saved.</div>` : ""}
|
||||
<div class="card" style="margin-bottom:20px"><div class="card-head"><span class="h">Your details</span></div>
|
||||
<div class="card-body">
|
||||
<form method="post" action="/settings/profile">${csrfInput(req)}
|
||||
${renderProfileFields({ values: profileValues, required: ["first_name", "last_name"], idPrefix: "account-" })}
|
||||
<button class="btn">Save details</button>
|
||||
</form>
|
||||
</div></div>
|
||||
<div class="card"><div class="card-head"><span class="h">API keys</span></div>
|
||||
<div class="card-body">
|
||||
<p class="dim" style="font-size:.85rem;margin-top:0">A <b>person</b> key acts as you; <code>logicsrc login</code> makes one for you. A <b>machine</b> key is for a deploy box or CI: one team, the vaults you list, read-only unless you say otherwise, its own identity key. After the box runs one command with it, grant it a vault from the vault page or with <code>logicsrc teams grant <team> <project> <env> --key <name></code>.</p>
|
||||
|
|
@ -308,6 +321,15 @@ pagesRouter.get("/settings", requireAuth, async (req, res) => {
|
|||
res.type("html").send(page({ title: "LogicSRC ▸ settings", body }));
|
||||
});
|
||||
|
||||
// The signup profile, editable. Names are required; the email falls back to the account's.
|
||||
pagesRouter.post("/settings/profile", requireAuth, async (req, res) => {
|
||||
const input = { ...req.body, email: String(req.body.email || "").trim() || req.user.email || "" };
|
||||
const check = validateProfile(input, { required: ["first_name", "last_name"] });
|
||||
if (!check.ok) return res.redirect(`/settings?err=${encodeURIComponent(`Check your details: ${Object.values(check.errors).join(" ")}`)}`);
|
||||
await profiles.save(req.user.id, check.profile);
|
||||
res.redirect("/settings?saved=profile");
|
||||
});
|
||||
|
||||
pagesRouter.post("/settings/apikeys", requireAuth, async (req, res) => {
|
||||
const name = String(req.body.name || "").trim().slice(0, 40);
|
||||
if (!name) return res.redirect("/settings?err=no-name");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue