Auth

作者 get-convex6ca54f6e2e75無授權條款6 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫今天更新

Add authentication to the current Convex + web app — passkeys by default (OAuth/password optional), wired end to end incl. the auth.config.ts that's the #1 auth footgun. TRIGGER when the user wants login / sign-in / accounts / passkeys / OAuth for an existing app.

AI 產生的概覽

為現有網頁應用接入 Convex 身分驗證,預設使用通行密鑰,並包含伺服器設定與登入介面。

功能
此技能會為現有的 Convex 網頁應用安裝並設定 @convex-dev/auth。它會加入提供者(預設為通行密鑰,可依需求改用密碼或 OAuth)、使用 jose 以非互動方式產生 JWT 金鑰、設定部署環境變數、撰寫 convex/auth.config.ts,並接上用戶端鉤子、登入元件與路由守衛。最後會驗證一次完整的登入流程。
適用情境
適用於需要為現有 Convex 網頁應用加入登入、註冊、帳號、通行密鑰或 OAuth 的情境。它著重於端對端的身分驗證接入,而非一般應用開發。
執行需求
需要一個 Convex 專案與網頁前端、@convex-dev/auth 套件,使用 pnpm 時還需要 jose 套件。必須設定部署環境變數 JWT_PRIVATE_KEY、JWKS 與 SITE_URL,可透過 Convex MCP 的 envSet 工具或 Convex CLI 完成。若引用 shadcn/ui 元件,需先安裝。僅含說明,不附指令碼。

Add sign-in to the app

Install and wire @convex-dev/auth for the current app: a provider (passkeys by default, or OAuth/password), the server config, the client hooks, and a sign-in UI — correctly, including the auth.config.ts that's the #1 real-world auth footgun.

Steps

  1. Install @convex-dev/auth (pinned build) and add it to convex.config.ts. With pnpm, also pnpm add jose (it won't hoist otherwise); you need it for step 3.
  2. Add the provider in convex/auth.ts (Passkey by default; Password or OAuth like Google on request).
  3. Generate the auth keys HEADLESSLY. Do NOT run the interactive npx @convex-dev/auth wizard: it needs a login/TTY and hangs in non-interactive, anonymous, or CI runs (the #1 auth time-sink). Generate JWT_PRIVATE_KEY + JWKS deterministically with jose: node -e 'import("jose").then(async({generateKeyPair,exportPKCS8,exportJWK})=>{const k=await generateKeyPair("RS256",{extractable:true});const priv=await exportPKCS8(k.privateKey);const pub=await exportJWK(k.publicKey);process.stdout.write(JSON.stringify({JWT_PRIVATE_KEY:priv.trimEnd().replace(/\n/g," "),JWKS:JSON.stringify({keys:[{use:"sig",...pub}]})}))})' > .auth-keys.json Then set JWT_PRIVATE_KEY and JWKS (from .auth-keys.json) plus SITE_URL on the deployment. Prefer the Convex MCP envSet tool, one call per var, to avoid shell-quoting the multi-line key. CLI fallback: use the NAME=VALUE form (npx convex env set "JWT_PRIVATE_KEY=$JWT"), NEVER env set JWT_PRIVATE_KEY "$JWT" (the value starts with -----BEGIN and the CLI parses the leading - as an unknown flag). SITE_URL is the dev URL (e.g. http://localhost:3000). Delete .auth-keys.json after.
  4. Write convex/auth.config.ts (the silently-always-signed-out bug lives here if it's wrong).
  5. Wire the client: ConvexAuthProvider, the sign-in component, and route guards. If you import shadcn/ui primitives (button, input, textarea, label, and so on), add them first with npx shadcn@latest add <name>; a missing @/components/ui/* is a hard build error.
  6. Verify a sign-in round-trips before declaring done.

Rules

  • Generate JWT_PRIVATE_KEY/JWKS with jose (extractable RS256; PKCS8 newlines to spaces; JWKS = {keys:[{use:"sig", ...publicJwk}]}). Do NOT run the interactive npx @convex-dev/auth wizard: it hangs headless/anonymous. Set the vars via the MCP envSet tool or the NAME=VALUE CLI form.
  • Always write auth.config.ts: a missing/incorrect one makes the app silently always-signed-out with no error.
  • Passkeys by default; only switch to password/OAuth on explicit request.
  • Install any shadcn/ui primitive you import up front (npx shadcn@latest add ...); a missing @/components/ui/* is a hard build failure.
  • Verify a real sign-in works before finishing.

來源與署名

來源:get-convex/convex-backend-skill位於skills/auth提交6ca54f6

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架