|
| 1 | +import { findProject, resolveProjectId, API } from "./config.mjs"; |
| 2 | + |
| 3 | +const HELP = `run402 auth — Manage project user authentication |
| 4 | +
|
| 5 | +Usage: |
| 6 | + run402 auth <subcommand> [args...] |
| 7 | +
|
| 8 | +Subcommands: |
| 9 | + magic-link --email <addr> --redirect <url> [--project <id>] |
| 10 | + Send a passwordless login link to the given email. Auto-creates user on first use. |
| 11 | +
|
| 12 | + verify --token <token> [--project <id>] |
| 13 | + Exchange a magic link token for access_token + refresh_token. |
| 14 | +
|
| 15 | + set-password --token <bearer> --new <password> [--current <password>] [--project <id>] |
| 16 | + Change, reset, or set a user's password. Requires the user's access_token. |
| 17 | +
|
| 18 | + settings --allow-password-set <true|false> [--project <id>] |
| 19 | + Update project auth settings (requires service_key). |
| 20 | +
|
| 21 | + providers [--project <id>] |
| 22 | + List available auth providers for the project. |
| 23 | +
|
| 24 | +Examples: |
| 25 | + run402 auth magic-link --email user@example.com --redirect https://myapp.run402.com/cb |
| 26 | + run402 auth verify --token abc123def456 |
| 27 | + run402 auth set-password --token eyJ... --new "new-pass" --current "old-pass" |
| 28 | + run402 auth settings --allow-password-set true |
| 29 | + run402 auth providers |
| 30 | +`; |
| 31 | + |
| 32 | +function parseFlag(args, flag) { |
| 33 | + for (let i = 0; i < args.length; i++) { |
| 34 | + if (args[i] === flag && args[i + 1]) return args[i + 1]; |
| 35 | + } |
| 36 | + return null; |
| 37 | +} |
| 38 | + |
| 39 | +async function magicLink(args) { |
| 40 | + const email = parseFlag(args, "--email"); |
| 41 | + const redirect = parseFlag(args, "--redirect"); |
| 42 | + const projectId = resolveProjectId(parseFlag(args, "--project")); |
| 43 | + const p = findProject(projectId); |
| 44 | + |
| 45 | + if (!email) { console.error(JSON.stringify({ status: "error", message: "Missing --email" })); process.exit(1); } |
| 46 | + if (!redirect) { console.error(JSON.stringify({ status: "error", message: "Missing --redirect <url>" })); process.exit(1); } |
| 47 | + |
| 48 | + const res = await fetch(`${API}/auth/v1/magic-link`, { |
| 49 | + method: "POST", |
| 50 | + headers: { "Authorization": `Bearer ${p.anon_key}`, "Content-Type": "application/json" }, |
| 51 | + body: JSON.stringify({ email, redirect_url: redirect }), |
| 52 | + }); |
| 53 | + const data = await res.json(); |
| 54 | + if (!res.ok) { |
| 55 | + console.error(JSON.stringify({ status: "error", http: res.status, ...data })); |
| 56 | + process.exit(1); |
| 57 | + } |
| 58 | + console.log(JSON.stringify({ status: "ok", ...data })); |
| 59 | +} |
| 60 | + |
| 61 | +async function verify(args) { |
| 62 | + const token = parseFlag(args, "--token"); |
| 63 | + const projectId = resolveProjectId(parseFlag(args, "--project")); |
| 64 | + const p = findProject(projectId); |
| 65 | + |
| 66 | + if (!token) { console.error(JSON.stringify({ status: "error", message: "Missing --token" })); process.exit(1); } |
| 67 | + |
| 68 | + const res = await fetch(`${API}/auth/v1/token?grant_type=magic_link`, { |
| 69 | + method: "POST", |
| 70 | + headers: { "Authorization": `Bearer ${p.anon_key}`, "Content-Type": "application/json" }, |
| 71 | + body: JSON.stringify({ token }), |
| 72 | + }); |
| 73 | + const data = await res.json(); |
| 74 | + if (!res.ok) { |
| 75 | + console.error(JSON.stringify({ status: "error", http: res.status, ...data })); |
| 76 | + process.exit(1); |
| 77 | + } |
| 78 | + console.log(JSON.stringify({ status: "ok", ...data })); |
| 79 | +} |
| 80 | + |
| 81 | +async function setPassword(args) { |
| 82 | + const accessToken = parseFlag(args, "--token"); |
| 83 | + const newPassword = parseFlag(args, "--new"); |
| 84 | + const currentPassword = parseFlag(args, "--current"); |
| 85 | + |
| 86 | + if (!accessToken) { console.error(JSON.stringify({ status: "error", message: "Missing --token <bearer_token>" })); process.exit(1); } |
| 87 | + if (!newPassword) { console.error(JSON.stringify({ status: "error", message: "Missing --new <password>" })); process.exit(1); } |
| 88 | + |
| 89 | + const body = { new_password: newPassword }; |
| 90 | + if (currentPassword) body.current_password = currentPassword; |
| 91 | + |
| 92 | + const res = await fetch(`${API}/auth/v1/user/password`, { |
| 93 | + method: "PUT", |
| 94 | + headers: { "Authorization": `Bearer ${accessToken}`, "Content-Type": "application/json" }, |
| 95 | + body: JSON.stringify(body), |
| 96 | + }); |
| 97 | + const data = await res.json(); |
| 98 | + if (!res.ok) { |
| 99 | + console.error(JSON.stringify({ status: "error", http: res.status, ...data })); |
| 100 | + process.exit(1); |
| 101 | + } |
| 102 | + console.log(JSON.stringify({ status: "ok", ...data })); |
| 103 | +} |
| 104 | + |
| 105 | +async function settings(args) { |
| 106 | + const allowPasswordSet = parseFlag(args, "--allow-password-set"); |
| 107 | + const projectId = resolveProjectId(parseFlag(args, "--project")); |
| 108 | + const p = findProject(projectId); |
| 109 | + |
| 110 | + if (allowPasswordSet === null) { console.error(JSON.stringify({ status: "error", message: "Missing --allow-password-set <true|false>" })); process.exit(1); } |
| 111 | + |
| 112 | + const res = await fetch(`${API}/auth/v1/settings`, { |
| 113 | + method: "PATCH", |
| 114 | + headers: { "Authorization": `Bearer ${p.service_key}`, "Content-Type": "application/json" }, |
| 115 | + body: JSON.stringify({ allow_password_set: allowPasswordSet === "true" }), |
| 116 | + }); |
| 117 | + const data = await res.json(); |
| 118 | + if (!res.ok) { |
| 119 | + console.error(JSON.stringify({ status: "error", http: res.status, ...data })); |
| 120 | + process.exit(1); |
| 121 | + } |
| 122 | + console.log(JSON.stringify({ status: "ok", ...data })); |
| 123 | +} |
| 124 | + |
| 125 | +async function providers(args) { |
| 126 | + const projectId = resolveProjectId(parseFlag(args, "--project")); |
| 127 | + const p = findProject(projectId); |
| 128 | + |
| 129 | + const res = await fetch(`${API}/auth/v1/providers`, { |
| 130 | + headers: { "Authorization": `Bearer ${p.anon_key}` }, |
| 131 | + }); |
| 132 | + const data = await res.json(); |
| 133 | + if (!res.ok) { |
| 134 | + console.error(JSON.stringify({ status: "error", http: res.status, ...data })); |
| 135 | + process.exit(1); |
| 136 | + } |
| 137 | + console.log(JSON.stringify(data, null, 2)); |
| 138 | +} |
| 139 | + |
| 140 | +export async function run(sub, args) { |
| 141 | + if (!sub || sub === "--help" || sub === "-h") { console.log(HELP); process.exit(0); } |
| 142 | + switch (sub) { |
| 143 | + case "magic-link": await magicLink(args); break; |
| 144 | + case "verify": await verify(args); break; |
| 145 | + case "set-password": await setPassword(args); break; |
| 146 | + case "settings": await settings(args); break; |
| 147 | + case "providers": await providers(args); break; |
| 148 | + default: |
| 149 | + console.error(`Unknown subcommand: ${sub}\n`); |
| 150 | + console.log(HELP); |
| 151 | + process.exit(1); |
| 152 | + } |
| 153 | +} |
0 commit comments