/hypervibe:_setup-2fa-usersfrom hypervibe
Sets up optional per-user TOTP 2FA on a Hypervibe accounts-based auth. Users enable 2FA from their account page with QR scan and backup codes stored in the database.
Ships in hypervibe. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
/bootstrap carries no language signal by itself). If nothing in the conversation gives a signal, fall back to the OS locale (node -e "console.log(Intl.DateTimeFormat().resolvedOptions().locale)") before defaulting to English. ALWAYS reply in that language for every user-facing message: questions, progress, confirmations, summaries, errors - including any example text quoted in this skill, which is illustrative and must be translated, never sent verbatim.Sets up optional per-user 2FA on a Hypervibe accounts-based auth. No deterministic script: the Account page and the login flow vary from one project to another, so Claude implements it contextually by following this guide. Check the build (pnpm tsc --noEmit + pnpm lint) after each large block.
Input variables (passed by add-2fa): PROJECT_NAME, WEB_DIR, AUTH_APP.
⚠️ Sensitive auth flow + project-specific. Implement block by block, check the build between each one, and test the login (enable 2FA → log out → log back in with a code) before concluding.
_check-deps db → if db_ok=false, point to /add-db then stop.src/server/auth.ts contains the users marker and that the users table exists (schema). Otherwise stop (the target is the accounts-based auth).cd <WEB_DIR> && pnpm add otpauth qrcode.Add to src/server/db/schema.ts (making sure text, boolean, timestamp, serial are imported from drizzle-orm/pg-core):
/** Per-user 2FA (TOTP). */
export const userTwoFactor = createTable("user_two_factor", {
userId: text("user_id").primaryKey(),
secret: text("secret").notNull(),
enabled: boolean("enabled").default(false).notNull(),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
});
export const userBackupCode = createTable("user_backup_code", {
id: serial("id").primaryKey(),
userId: text("user_id").notNull(),
codeHash: text("code_hash").notNull(),
usedAt: timestamp("used_at", { withTimezone: true }),
});
Then cd <WEB_DIR> && pnpm db:push.
src/lib/totp.ts (generic, the secret is passed as an argument because it is per-user):
import { TOTP, Secret } from "otpauth";
import { randomBytes } from "crypto";
export function generateTotpSecret(): string { return new Secret({ size: 20 }).base32; }
export function totpUrl(issuer: string, account: string, secret: string): string {
return new TOTP({ issuer, label: account, algorithm: "SHA1", digits: 6, period: 30, secret: Secret.fromBase32(secret) }).toString();
}
export function verifyTotp(secret: string, token: string): boolean {
const clean = token.replace(/\s/g, "");
if (!/^\d{6}$/.test(clean)) return false;
return new TOTP({ algorithm: "SHA1", digits: 6, period: 30, secret: Secret.fromBase32(secret) }).validate({ token: clean, window: 1 }) !== null;
}
export function generateBackupCodes(): string[] {
return Array.from({ length: 8 }, () => {
const h = randomBytes(4).toString("hex").toUpperCase();
return `${h.slice(0, 4)}-${h.slice(4, 8)}`;
});
}
src/lib/user-2fa.ts (DB; reuses hashPassword/verifyPassword from ~/lib/password):
import { eq, and, isNull } from "drizzle-orm";
import { db } from "~/server/db";
import { userTwoFactor, userBackupCode } from "~/server/db/schema";
import { hashPassword, verifyPassword } from "~/lib/password";
export async function getUser2fa(userId: string) {
return (await db.select().from(userTwoFactor).where(eq(userTwoFactor.userId, userId)).limit(1))[0] ?? null;
}
export async function isTwoFactorEnabled(userId: string) {
return (await getUser2fa(userId))?.enabled ?? false;
}
export async function setPendingSecret(userId: string, secret: string) {
const existing = await getUser2fa(userId);
if (existing) await db.update(userTwoFactor).set({ secret, enabled: false }).where(eq(userTwoFactor.userId, userId));
else await db.insert(userTwoFactor).values({ userId, secret, enabled: false });
}
export async function enableTwoFactor(userId: string) {
await db.update(userTwoFactor).set({ enabled: true }).where(eq(userTwoFactor.userId, userId));
}
export async function disableTwoFactor(userId: string) {
await db.delete(userTwoFactor).where(eq(userTwoFactor.userId, userId));
await db.delete(userBackupCode).where(eq(userBackupCode.userId, userId));
}
export async function replaceBackupCodes(userId: string, codes: string[]) {
await db.delete(userBackupCode).where(eq(userBackupCode.userId, userId));
for (const c of codes) await db.insert(userBackupCode).values({ userId, codeHash: await hashPassword(c.toUpperCase()) });
}
export async function consumeUserBackupCode(userId: string, code: string) {
const norm = code.trim().toUpperCase();
const rows = await db.select().from(userBackupCode).where(and(eq(userBackupCode.userId, userId), isNull(userBackupCode.usedAt)));
for (const r of rows) if (await verifyPassword(norm, r.codeHash)) {
await db.update(userBackupCode).set({ usedAt: new Date() }).where(eq(userBackupCode.id, r.id));
return true;
}
return false;
}
src/app/account/two-factor-actions.ts)Generate the secret on the server, return the QR (SVG) so it can be displayed in the browser (never the chat). Replace <ISSUER> with the project name.
"use server";
import QRCode from "qrcode";
import { auth } from "~/server/auth";
import { generateTotpSecret, totpUrl, verifyTotp, generateBackupCodes } from "~/lib/totp";
import { getUser2fa, setPendingSecret, enableTwoFactor, disableTwoFactor, replaceBackupCodes, isTwoFactorEnabled } from "~/lib/user-2fa";
const ISSUER = "<ISSUER>";
export async function start2faSetup() {
const s = await auth(); const userId = s?.user?.id;
if (!userId) return { error: "unauthorized" as const };
if (await isTwoFactorEnabled(userId)) return { error: "already_enabled" as const };
const secret = generateTotpSecret();
await setPendingSecret(userId, secret);
const url = totpUrl(ISSUER, s.user.email ?? "account", secret);
const qrSvg = await QRCode.toString(url, { type: "svg", margin: 2, width: 200 });
return { secret, qrSvg };
}
export async function confirm2faSetup(code: string) {
const s = await auth(); const userId = s?.user?.id;
if (!userId) return { error: "unauthorized" as const };
const row = await getUser2fa(userId);
if (!row) return { error: "no_setup" as const };
if (!verifyTotp(row.secret, code)) return { error: "invalid_code" as const };
await enableTwoFactor(userId);
const backupCodes = generateBackupCodes();
await replaceBackupCodes(userId, backupCodes);
return { enabled: true as const, backupCodes };
}
export async function disable2fa() {
const s = await auth(); const userId = s?.user?.id;
if (!userId) return { error: "unauthorized" as const };
await disableTwoFactor(userId); return { disabled: true as const };
}
export async function regenerateBackupCodes() {
const s = await auth(); const userId = s?.user?.id;
if (!userId || !(await isTwoFactorEnabled(userId))) return { error: "not_enabled" as const };
const backupCodes = generateBackupCodes();
await replaceBackupCodes(userId, backupCodes);
return { backupCodes };
}
Check that the session exposes
user.id(the Hypervibe user auth does). If it exposes anid, adapt the helpers so the key = email.
Create a client component src/app/account/two-factor-section.tsx ("Security - Two-factor authentication") with 3 states:
start2faSetup() → display the QR (qrSvg) + the key + a "6-digit code" field + a "Confirm" button → confirm2faSetup(code).disable2fa) and "Regenerate codes" (regenerateBackupCodes) buttons.Style: reuse the project's shadcn/ui components (Card, Button, Input, toast). Import the section into src/app/account/page.tsx (pass the component the initial state isTwoFactorEnabled(userId) computed server-side in the page).
The users flow logs in via signIn("credentials", …). Add a 2FA step only for accounts that enabled it. Recommended approach (mirror of admin mode):
Create a server action loginAction({ email, password, code? }) in src/app/signin/actions.ts:
login:<ip> (see ~/lib/rate-limit), the key authorize() counts on too,verifyPassword) → if KO bad_credentials,isTwoFactorEnabled(userId): require code (2fa_required if absent), check verifyTotp(secret, code) || consumeUserBackupCode(userId, code) → invalid_code otherwise,AUTH_SECRET, like admin mode: create a reduced src/lib/auth-2fa.ts with createLoginProof/verifyLoginProof) and call signIn("credentials", { email, proof, redirect: false }).Adapt the Credentials provider in src/server/auth.ts: a proof field in addition to password. A password alone must never open an account that enabled 2FA: the sign-in route (/api/auth/callback/credentials) can be called directly, without the action, so authorize() accepts the password ONLY for an account that has NOT enabled 2FA (isTwoFactorEnabled(user.id) false); for an account that enabled it, only a valid signed proof (verifyLoginProof) opens the session, and the password alone gives null. Never "the password OR the proof" for everyone: that lets anyone who knows the password skip the second factor. Keep the user DB lookup (id, email…). Keep the limit (TooManyAttempts), with one change, as ${CLAUDE_SKILL_DIR}/../../templates/2fa/auth.ts does: a request that carries a valid proof is not counted (the action has already counted it, under the same key), any other request is. A forged proof then never buys more attempts, and a sign-in in two steps never counts twice. The shape to write (with the imports of the users template, verifyLoginProof from ~/lib/auth-2fa and isTwoFactorEnabled from ~/lib/user-2fa):
async authorize(credentials, request) {
const email = credentials?.email as string | undefined;
const password = credentials?.password as string | undefined;
const proof = credentials?.proof as string | undefined;
// A proof signed by loginAction: the password and the second factor were checked there,
// and the attempt counted. It alone opens an account that enabled 2FA.
if (email && proof && verifyLoginProof(email, proof)) {
const user = await db.query.users.findFirst({ columns: { id: true, email: true, name: true, image: true, passwordHash: true }, where: eq(users.email, email) });
return user ? { id: user.id, email: user.email, name: user.name, image: user.image } : null;
}
// Anything else counts: a forged proof, a password.
const ip = request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? "unknown";
if (!checkRateLimit(`login:${ip}`).allowed) throw new TooManyAttempts();
if (!email || !password || proof) return null;
const user = await db.query.users.findFirst({ columns: { id: true, email: true, name: true, image: true, passwordHash: true }, where: eq(users.email, email) });
if (!user?.passwordHash) return null;
const ok = await verifyPassword(password, user.passwordHash);
// A password alone never opens an account that enabled 2FA, whoever calls the route.
if (!ok || (await isTwoFactorEnabled(user.id))) return null;
return { id: user.id, email: user.email, name: user.name, image: user.image };
}
Adapt src/app/signin/page.tsx: 2 steps (email+password → if 2fa_required, a code field), reusing the admin signin pattern (credentials/2fa states).
This is the only part that touches the core of the auth. Implement it carefully, check the build, and test end to end: enable 2FA on a test account, log out, log back in (the code must be requested), test a wrong code, test a backup code, and verify that an account WITHOUT 2FA still logs in with password only. Then the direct route, the case that matters most: from the browser's console on the sign-in page (the person types the test password there, never in the chat),
const { csrfToken } = await (await fetch("/api/auth/csrf")).json(); const r = await fetch("/api/auth/callback/credentials", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", "X-Auth-Return-Redirect": "1" }, body: new URLSearchParams({ email: "<test account>", password: "<its password>", csrfToken }) }); (await r.json()).urlwith the right password of the account WITH 2FA must be REFUSED (the address it gives carrieserror=CredentialsSignin, and no session opens); the same call for the account without 2FA succeeds. If the account with 2FA gets a session,authorize()still accepts the password alone: fix it before going on.
_update-claude-md (section ## Two-factor authentication (2FA) on the users side: optional per account, tables user_two_factor / user_backup_code, helpers src/lib/{totp,user-2fa}.ts, actions account/two-factor-actions.ts, 2FA step at login).
Summary to the user:
✅ Two-factor authentication available for your users
Each user can enable it from their Account page ("Security"): they scan a QR in <AUTH_APP>, confirm a code, and receive 8 backup codes (shown once in their browser). At the next login, they will be asked for their code after the password. Accounts that do not enable it keep going with password only.
This sub-skill is piloted by Claude (no deterministic script) because the Account page and the login flow are project-specific. The secrets/codes never go through the chat: enrollment happens in the user's browser. To be tested on a real user-auth project before relying on it in production.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub flavien-ia/hypervibe-craft --plugin hypervibe90 skills in this plugin
/accessibilityAccessibility audit of a Next.js app: reads the code (images, buttons, links, form fields, keyboard, focus, zoom, motion), measures the live pages with PageSpeed Insights, explains each problem plainly, then applies the fixes the user approves./add-2faAdd two-factor authentication (2FA TOTP) to an existing T3 project's login. Orchestrator - asks which authenticator app the user wants, detects whether the project uses admin auth (single fixed login) or user accounts, and delegates to the right setup. Admin mode - 2FA is mandatory for the single admin, secret + backup codes stored in the Bitwarden vault. User mode - 2FA is optional per user, each user enables it from their account page, secrets + backup codes stored per-user in the database. Requires the project to already have hypervibe auth (`/add-auth`)./Other plugins' skills for the same job