@realiizlabs/admin 0.9.1 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/index.cjs +110 -0
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.d.cts +94 -1
- package/dist/auth/index.d.ts +94 -1
- package/dist/auth/index.js +101 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/shell/index.cjs +78 -8
- package/dist/shell/index.cjs.map +1 -1
- package/dist/shell/index.d.cts +45 -5
- package/dist/shell/index.d.ts +45 -5
- package/dist/shell/index.js +78 -9
- package/dist/shell/index.js.map +1 -1
- package/package.json +1 -1
package/dist/auth/index.cjs
CHANGED
|
@@ -165,23 +165,133 @@ async function removeSiteUser(ctx, input) {
|
|
|
165
165
|
if (target.error) throw target.error;
|
|
166
166
|
const targetRole = target.data?.role ?? "editor";
|
|
167
167
|
assertMayGrant(actorRole, input.siteId, targetRole);
|
|
168
|
+
if (targetRole === "owner") await assertNotLastOwner(svc, input.siteId);
|
|
168
169
|
const del = await svc.from("site_users").delete().eq("user_id", input.userId).eq("site_id", input.siteId);
|
|
169
170
|
if (del.error) throw del.error;
|
|
170
171
|
}
|
|
172
|
+
async function updateSiteRole(ctx, input) {
|
|
173
|
+
const svc = createServiceClient(ctx);
|
|
174
|
+
const actorRole = await resolveActorRole(svc, input.actor.id, input.siteId);
|
|
175
|
+
assertMayGrant(actorRole, input.siteId, input.role);
|
|
176
|
+
const target = await svc.from("site_users").select("role").eq("user_id", input.userId).eq("site_id", input.siteId).maybeSingle();
|
|
177
|
+
if (target.error) throw target.error;
|
|
178
|
+
const current = target.data?.role;
|
|
179
|
+
if (!current) throw new IdentityError("That person isn't a member of this site.");
|
|
180
|
+
if (current === "owner") assertMayGrant(actorRole, input.siteId, "owner");
|
|
181
|
+
if (current === "owner" && input.role === "editor") await assertNotLastOwner(svc, input.siteId);
|
|
182
|
+
const upd = await svc.from("site_users").update({ role: input.role }).eq("user_id", input.userId).eq("site_id", input.siteId);
|
|
183
|
+
if (upd.error) throw upd.error;
|
|
184
|
+
}
|
|
185
|
+
async function assertNotLastOwner(svc, siteId) {
|
|
186
|
+
const owners = await svc.from("site_users").select("user_id").eq("site_id", siteId).eq("role", "owner");
|
|
187
|
+
if (owners.error) throw owners.error;
|
|
188
|
+
if ((owners.data ?? []).length <= 1) throw new IdentityError("A site needs at least one owner \u2014 make someone else an owner first.");
|
|
189
|
+
}
|
|
190
|
+
async function resendInvite(ctx, input) {
|
|
191
|
+
const svc = createServiceClient(ctx);
|
|
192
|
+
const actorRole = await resolveActorRole(svc, input.actor.id, input.siteId);
|
|
193
|
+
assertMayGrant(actorRole, input.siteId, "editor");
|
|
194
|
+
const email = input.email.trim().toLowerCase();
|
|
195
|
+
const again = await svc.auth.admin.inviteUserByEmail(email, { redirectTo: input.redirectTo, data: { site_id: input.siteId } });
|
|
196
|
+
if (!again.error) return { resent: true, message: `Invitation sent again to ${email}.` };
|
|
197
|
+
if (/already|exists|registered|confirmed/i.test(again.error.message)) {
|
|
198
|
+
return { resent: false, message: `${email} has already accepted \u2014 they can sign in with an email link any time.` };
|
|
199
|
+
}
|
|
200
|
+
throw new IdentityError(`Resend failed: ${again.error.message}`);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// src/auth/branding.ts
|
|
204
|
+
async function getSiteBranding(client, siteId) {
|
|
205
|
+
const { data, error } = await client.from("sites").select("name, logo_url, favicon_url, business_email, business_phone").eq("site_id", siteId).maybeSingle();
|
|
206
|
+
if (error) throw error;
|
|
207
|
+
if (!data) return null;
|
|
208
|
+
const r = data;
|
|
209
|
+
return { name: r.name, logoUrl: r.logo_url, faviconUrl: r.favicon_url, businessEmail: r.business_email, businessPhone: r.business_phone };
|
|
210
|
+
}
|
|
211
|
+
async function updateSiteBranding(client, siteId, patch) {
|
|
212
|
+
const row = {
|
|
213
|
+
site_id: siteId,
|
|
214
|
+
name: patch.name.trim(),
|
|
215
|
+
logo_url: patch.logoUrl ?? null,
|
|
216
|
+
favicon_url: patch.faviconUrl ?? null,
|
|
217
|
+
business_email: emptyToNull(patch.businessEmail),
|
|
218
|
+
business_phone: emptyToNull(patch.businessPhone)
|
|
219
|
+
};
|
|
220
|
+
const { error } = await client.from("sites").upsert(row, { onConflict: "site_id" });
|
|
221
|
+
if (error) throw error;
|
|
222
|
+
}
|
|
223
|
+
function emptyToNull(v) {
|
|
224
|
+
const s = (v ?? "").trim();
|
|
225
|
+
return s === "" ? null : s;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/auth/members.ts
|
|
229
|
+
async function listSiteMembers(client, siteId) {
|
|
230
|
+
const { data, error } = await client.rpc("site_members", { p_site_id: siteId });
|
|
231
|
+
if (error) {
|
|
232
|
+
if (error.code === "42501" || /not allowed/i.test(error.message)) throw new ForbiddenError(siteId, "owner", null);
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
return (data ?? []).map((r) => ({
|
|
236
|
+
userId: r.user_id,
|
|
237
|
+
email: r.email,
|
|
238
|
+
name: r.name,
|
|
239
|
+
avatarUrl: r.avatar_url,
|
|
240
|
+
role: r.role === "owner" ? "owner" : "editor",
|
|
241
|
+
status: r.status === "active" ? "active" : "invited",
|
|
242
|
+
invitedAt: r.invited_at
|
|
243
|
+
}));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/auth/profile.ts
|
|
247
|
+
var MIN_PASSWORD_LENGTH = 10;
|
|
248
|
+
var PASSWORD_TOO_SHORT = "Use at least 10 characters.";
|
|
249
|
+
function profileOf(user) {
|
|
250
|
+
const m = user?.user_metadata ?? {};
|
|
251
|
+
const str = (v) => typeof v === "string" && v.trim() ? v.trim() : null;
|
|
252
|
+
const name = str(m.name);
|
|
253
|
+
const displayName = str(m.display_name) ?? name;
|
|
254
|
+
const avatarUrl = typeof m.avatar_url === "string" && m.avatar_url ? m.avatar_url : null;
|
|
255
|
+
return { name, displayName, avatarUrl };
|
|
256
|
+
}
|
|
257
|
+
async function updateProfile(client, patch) {
|
|
258
|
+
const data = {};
|
|
259
|
+
if (patch.name !== void 0) data.name = (patch.name ?? "").trim().slice(0, 80);
|
|
260
|
+
if (patch.displayName !== void 0) data.display_name = (patch.displayName ?? "").trim().slice(0, 80);
|
|
261
|
+
if (patch.avatarUrl !== void 0) data.avatar_url = patch.avatarUrl ?? "";
|
|
262
|
+
const { data: res, error } = await client.auth.updateUser({ data });
|
|
263
|
+
if (error) throw new IdentityError(error.message);
|
|
264
|
+
return profileOf(res.user);
|
|
265
|
+
}
|
|
266
|
+
async function setPassword(client, password) {
|
|
267
|
+
if (typeof password !== "string" || password.length < MIN_PASSWORD_LENGTH) throw new IdentityError(PASSWORD_TOO_SHORT);
|
|
268
|
+
const { error } = await client.auth.updateUser({ password });
|
|
269
|
+
if (error) throw new IdentityError(error.message);
|
|
270
|
+
}
|
|
171
271
|
|
|
172
272
|
exports.ForbiddenError = ForbiddenError;
|
|
173
273
|
exports.IdentityError = IdentityError;
|
|
274
|
+
exports.MIN_PASSWORD_LENGTH = MIN_PASSWORD_LENGTH;
|
|
275
|
+
exports.PASSWORD_TOO_SHORT = PASSWORD_TOO_SHORT;
|
|
174
276
|
exports.ROLE_RANK = ROLE_RANK;
|
|
175
277
|
exports.UnauthenticatedError = UnauthenticatedError;
|
|
176
278
|
exports.createIdentityClient = createIdentityClient;
|
|
177
279
|
exports.getCurrentUser = getCurrentUser;
|
|
280
|
+
exports.getSiteBranding = getSiteBranding;
|
|
178
281
|
exports.getSiteRole = getSiteRole;
|
|
179
282
|
exports.handleAuthCallback = handleAuthCallback;
|
|
180
283
|
exports.inviteUser = inviteUser;
|
|
284
|
+
exports.listSiteMembers = listSiteMembers;
|
|
285
|
+
exports.profileOf = profileOf;
|
|
181
286
|
exports.removeSiteUser = removeSiteUser;
|
|
182
287
|
exports.requireSiteUser = requireSiteUser;
|
|
288
|
+
exports.resendInvite = resendInvite;
|
|
183
289
|
exports.roleAtLeast = roleAtLeast;
|
|
184
290
|
exports.safeNextPath = safeNextPath;
|
|
291
|
+
exports.setPassword = setPassword;
|
|
185
292
|
exports.signOut = signOut;
|
|
293
|
+
exports.updateProfile = updateProfile;
|
|
294
|
+
exports.updateSiteBranding = updateSiteBranding;
|
|
295
|
+
exports.updateSiteRole = updateSiteRole;
|
|
186
296
|
//# sourceMappingURL=index.cjs.map
|
|
187
297
|
//# sourceMappingURL=index.cjs.map
|
package/dist/auth/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/auth/client.ts","../../src/auth/session.ts","../../src/auth/errors.ts","../../src/auth/types.ts","../../src/auth/roles.ts","../../src/auth/callback.ts","../../src/auth/signout.ts","../../src/auth/service.ts"],"names":["createServerClient","createClient"],"mappings":";;;;;;;AAuBO,SAAS,qBAAqB,GAAA,EAAsC;AACzE,EAAA,MAAM,QAAQ,GAAA,CAAI,OAAA;AAClB,EAAA,OAAOA,sBAAA,CAAmB,GAAA,CAAI,GAAA,EAAK,GAAA,CAAI,OAAA,EAAS;AAAA,IAC9C,OAAA,EAAS;AAAA,MACP,MAAA,EAAQ,MAAM,KAAA,CAAM,MAAA,EAAO;AAAA,MAC3B,MAAA,EAAQ,CAAC,IAAA,KAAwB;AAI/B,QAAA,IAAI;AACF,UAAA,IAAI,OAAO,KAAA,CAAM,MAAA,KAAW,UAAA,EAAY;AACtC,YAAA,KAAK,KAAA,CAAM,OAAO,IAAI,CAAA;AAAA,UACxB,CAAA,MAAA,IAAW,OAAO,KAAA,CAAM,GAAA,KAAQ,UAAA,EAAY;AAC1C,YAAA,KAAA,MAAW,CAAA,IAAK,MAAM,KAAA,CAAM,GAAA,CAAI,EAAE,IAAA,EAAM,CAAA,CAAE,KAAA,EAAO,CAAA,CAAE,OAAO,CAAA;AAAA,UAC5D;AAAA,QACF,CAAA,CAAA,MAAQ;AAAA,QAER;AAAA,MACF;AAAA;AACF,GACD,CAAA;AACH;;;AClCA,eAAsB,eAAe,MAAA,EAAsD;AACz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AA,EAC/B;AACA,EAAA,OAAO,MAAA;AACT;AASA,eAAsB,cAAA,CAAe,KAAqB,KAAA,EAAmC;AAC3F,EAAA,MAAM,GAAA,GAAM,oBAAoB,GAAG,CAAA;AACnC,EAAA,MAAM,SAAA,GAAY,MAAM,gBAAA,CAAiB,GAAA,EAAK,MAAM,KAAA,CAAM,EAAA,EAAI,MAAM,MAAM,CAAA;AAE1E,EAAA,MAAM,SAAS,MAAM,GAAA,CAAI,KAAK,YAAY,CAAA,CAAE,OAAO,MAAM,CAAA,CAAE,GAAG,SAAA,EAAW,KAAA,CAAM,MAAM,CAAA,CAAE,EAAA,CAAG,WAAW,KAAA,CAAM,MAAM,EAAE,WAAA,EAAY;AAC/H,EAAA,IAAI,MAAA,CAAO,KAAA,EAAO,MAAM,MAAA,CAAO,KAAA;AAC/B,EAAA,MAAM,UAAA,GAAe,MAAA,CAAO,IAAA,EAAmC,IAAA,IAAQ,QAAA;AACvE,EAAA,cAAA,CAAe,SAAA,EAAW,KAAA,CAAM,MAAA,EAAQ,UAAU,CAAA;AAElD,EAAA,MAAM,MAAM,MAAM,GAAA,CAAI,IAAA,CAAK,YAAY,EAAE,MAAA,EAAO,CAAE,EAAA,CAAG,SAAA,EAAW,MAAM,MAAM,CAAA,CAAE,EAAA,CAAG,SAAA,EAAW,MAAM,MAAM,CAAA;AACxG,EAAA,IAAI,GAAA,CAAI,KAAA,EAAO,MAAM,GAAA,CAAI,KAAA;AAC3B","file":"index.cjs","sourcesContent":["/**\n * createIdentityClient — a Supabase client bound to the request's cookies.\n *\n * Built per request from arguments. A Next 16 route does:\n *\n * const store = await cookies();\n * const client = createIdentityClient({ url, anonKey, cookies: store });\n *\n * No `next` import here. The CookieStore interface accepts either shape a\n * host might hand us: @supabase/ssr's { getAll, setAll }, or Next's\n * cookies() store, which has { getAll, set } — the session cookies are\n * written through whichever exists. (Observed 2026-09-08: with only setAll\n * supported, Next's store silently wrote nothing and every sign-in bounced.)\n */\n\nimport { createServerClient } from \"@supabase/ssr\";\nimport type { SupabaseClient } from \"@supabase/supabase-js\";\nimport type { IdentityContext } from \"./types\";\n\nexport type IdentityClient = SupabaseClient;\n\ntype CookieToSet = { name: string; value: string; options?: Record<string, unknown> };\n\nexport function createIdentityClient(ctx: IdentityContext): IdentityClient {\n const store = ctx.cookies;\n return createServerClient(ctx.url, ctx.anonKey, {\n cookies: {\n getAll: () => store.getAll(),\n setAll: (list: CookieToSet[]) => {\n // Server Components can't set cookies; @supabase/ssr documents that\n // swallowing the failure there is correct — middleware / route\n // handlers refresh the session instead.\n try {\n if (typeof store.setAll === \"function\") {\n void store.setAll(list);\n } else if (typeof store.set === \"function\") {\n for (const c of list) store.set(c.name, c.value, c.options);\n }\n } catch {\n /* read-only cookie store */\n }\n },\n },\n });\n}\n","/**\n * getCurrentUser — who is signed in, verified against the auth server.\n *\n * `getUser()` round-trips to Supabase and validates the JWT; `getSession()`\n * only decodes the cookie and is not trustworthy on the server. Always this.\n */\n\nimport type { IdentityClient } from \"./client\";\nimport type { IdentityUser } from \"./types\";\n\nexport async function getCurrentUser(client: IdentityClient): Promise<IdentityUser | null> {\n const { data, error } = await client.auth.getUser();\n if (error || !data.user) return null;\n return { id: data.user.id, email: data.user.email ?? null };\n}\n","/** Typed errors so a route can map them to 401 / 403 without string matching. */\n\nexport class IdentityError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"IdentityError\";\n }\n}\n\n/** No session — send them to sign in. Maps to 401. */\nexport class UnauthenticatedError extends IdentityError {\n constructor() {\n super(\"Not signed in\");\n this.name = \"UnauthenticatedError\";\n }\n}\n\n/** Signed in, but not allowed to do this here. Maps to 403. */\nexport class ForbiddenError extends IdentityError {\n readonly siteId: string;\n readonly required: string;\n readonly actual: string | null;\n\n constructor(siteId: string, required: string, actual: string | null) {\n super(`Requires ${required} on site \"${siteId}\" (you are ${actual ?? \"not a member\"})`);\n this.name = \"ForbiddenError\";\n this.siteId = siteId;\n this.required = required;\n this.actual = actual;\n }\n}\n","/**\n * Types for the identity layer. All config arrives as arguments — never env.\n */\n\n/**\n * Either cookie-store shape a host can hand us:\n * - @supabase/ssr's CookieMethodsServer: { getAll, setAll }\n * - Next's `await cookies()` store: { getAll, set }\n * `setAll` is preferred when present; otherwise each cookie goes through `set`.\n */\nexport interface CookieStore {\n getAll(): { name: string; value: string }[] | Promise<{ name: string; value: string }[]>;\n setAll?(cookies: { name: string; value: string; options?: Record<string, unknown> }[]): void | Promise<void>;\n set?(name: string, value: string, options?: Record<string, unknown>): unknown;\n}\n\n/** Enough to read the signed-in user with the anon key + their cookies. */\nexport interface IdentityContext {\n url: string;\n anonKey: string;\n cookies: CookieStore;\n}\n\n/** Server-only. The service-role key bypasses RLS; the module checks roles in code first. */\nexport interface ServiceContext {\n url: string;\n serviceRoleKey: string;\n}\n\nexport type SiteRole = \"staff\" | \"owner\" | \"editor\";\n\nexport const ROLE_RANK: Record<SiteRole, number> = { editor: 1, owner: 2, staff: 3 };\n\nexport interface IdentityUser {\n id: string;\n email: string | null;\n}\n\nexport interface SiteMembership {\n userId: string;\n siteId: string;\n role: \"owner\" | \"editor\";\n}\n","/**\n * Role resolution and the request gate.\n *\n * Two selects on the caller's OWN rows — both permitted by RLS with the anon\n * key, so this never needs the service role. Staff wins over any site role.\n */\n\nimport type { IdentityClient } from \"./client\";\nimport { ForbiddenError, UnauthenticatedError } from \"./errors\";\nimport { getCurrentUser } from \"./session\";\nimport { ROLE_RANK, type IdentityUser, type SiteRole } from \"./types\";\n\nexport async function getSiteRole(client: IdentityClient, siteId: string, userId?: string): Promise<SiteRole | null> {\n const uid = userId ?? (await getCurrentUser(client))?.id;\n if (!uid) return null;\n\n const [staff, member] = await Promise.all([\n client.from(\"staff\").select(\"user_id\").eq(\"user_id\", uid).maybeSingle(),\n client.from(\"site_users\").select(\"role\").eq(\"user_id\", uid).eq(\"site_id\", siteId).maybeSingle(),\n ]);\n\n if (staff.error) throw staff.error;\n if (member.error) throw member.error;\n\n if (staff.data) return \"staff\";\n const role = (member.data as { role?: string } | null)?.role;\n return role === \"owner\" || role === \"editor\" ? role : null;\n}\n\nexport function roleAtLeast(actual: SiteRole | null, minimum: SiteRole): boolean {\n return actual !== null && ROLE_RANK[actual] >= ROLE_RANK[minimum];\n}\n\nexport interface RequireOptions {\n /** Default \"editor\" — any member of the site. */\n minimumRole?: SiteRole;\n}\n\n/**\n * The gate. Throws UnauthenticatedError (→ sign in) or ForbiddenError (→ 403);\n * otherwise returns the user and their effective role for this site.\n */\nexport async function requireSiteUser(\n client: IdentityClient,\n siteId: string,\n opts: RequireOptions = {},\n): Promise<{ user: IdentityUser; role: SiteRole }> {\n const user = await getCurrentUser(client);\n if (!user) throw new UnauthenticatedError();\n\n const minimum = opts.minimumRole ?? \"editor\";\n const role = await getSiteRole(client, siteId, user.id);\n if (!roleAtLeast(role, minimum)) throw new ForbiddenError(siteId, minimum, role);\n\n return { user, role: role as SiteRole };\n}\n","/**\n * handleAuthCallback — the emailed magic link lands here with ?code=…&next=…\n *\n * Exchanges the PKCE code for a session (cookies are written through the\n * adapter the client was built with) and says where to send the user. The\n * `next` parameter is only honoured when it is a plain same-origin path —\n * \"//evil.example\" and \"https://…\" fall back, which closes the open-redirect\n * a naive startsWith(\"/\") check leaves open.\n */\n\nimport type { IdentityClient } from \"./client\";\n\nexport type CallbackResult =\n | { ok: true; redirectTo: string }\n | { ok: false; reason: \"missing_code\" | \"exchange_failed\"; message?: string };\n\nexport interface CallbackOptions {\n /** Where to go when `next` is absent or unsafe. Default \"/admin\". */\n fallbackPath?: string;\n}\n\nexport function safeNextPath(next: string | null | undefined, fallback: string): string {\n if (!next) return fallback;\n // Exactly one leading slash, then not another slash or backslash.\n if (!/^\\/(?![/\\\\])/.test(next)) return fallback;\n // No scheme smuggling or CR/LF.\n if (/[\\r\\n]/.test(next) || /^\\/[^?#]*:\\/\\//.test(next)) return fallback;\n return next;\n}\n\nexport async function handleAuthCallback(\n client: IdentityClient,\n requestUrl: string | URL,\n opts: CallbackOptions = {},\n): Promise<CallbackResult> {\n const fallback = opts.fallbackPath ?? \"/admin\";\n const url = typeof requestUrl === \"string\" ? new URL(requestUrl, \"http://placeholder.invalid\") : requestUrl;\n const code = url.searchParams.get(\"code\");\n if (!code) return { ok: false, reason: \"missing_code\" };\n\n const { error } = await client.auth.exchangeCodeForSession(code);\n if (error) return { ok: false, reason: \"exchange_failed\", message: error.message };\n\n return { ok: true, redirectTo: safeNextPath(url.searchParams.get(\"next\"), fallback) };\n}\n","/** signOut — clears this browser's session. The caller redirects afterwards. */\n\nimport type { IdentityClient } from \"./client\";\n\nexport async function signOut(client: IdentityClient): Promise<void> {\n const { error } = await client.auth.signOut({ scope: \"local\" });\n if (error) throw error;\n}\n","/**\n * The only file that touches the service-role key. Invite and remove.\n *\n * Authorization is checked HERE, in code, before the privileged client does\n * anything — RLS doesn't apply to the service role, so this is the guard:\n * staff → may grant owner or editor on any site\n * owner → may grant editor on their own site only\n * editor → may not invite\n *\n * `actor` is the caller's already-resolved identity (from requireSiteUser on\n * the anon client). Its role is re-resolved with the service client so a\n * caller can't lie about it.\n */\n\nimport { createClient, type SupabaseClient } from \"@supabase/supabase-js\";\nimport { ForbiddenError, IdentityError } from \"./errors\";\nimport type { ServiceContext, SiteRole } from \"./types\";\n\nexport type ServiceClient = SupabaseClient;\n\nexport function createServiceClient(ctx: ServiceContext): ServiceClient {\n return createClient(ctx.url, ctx.serviceRoleKey, {\n auth: { persistSession: false, autoRefreshToken: false },\n });\n}\n\nexport interface InviteInput {\n email: string;\n siteId: string;\n role: \"owner\" | \"editor\";\n /** The signed-in user performing the invite. */\n actor: { id: string };\n /** Where the invite email's link lands — the site's /admin/auth/callback. */\n redirectTo: string;\n}\n\nexport interface InviteResult {\n userId: string;\n siteId: string;\n role: \"owner\" | \"editor\";\n /** false when the auth user already existed and only the membership was added. */\n invitedByEmail: boolean;\n}\n\nasync function resolveActorRole(svc: ServiceClient, actorId: string, siteId: string): Promise<SiteRole | null> {\n const staff = await svc.from(\"staff\").select(\"user_id\").eq(\"user_id\", actorId).maybeSingle();\n if (staff.error) throw staff.error;\n if (staff.data) return \"staff\";\n const m = await svc.from(\"site_users\").select(\"role\").eq(\"user_id\", actorId).eq(\"site_id\", siteId).maybeSingle();\n if (m.error) throw m.error;\n const role = (m.data as { role?: string } | null)?.role;\n return role === \"owner\" || role === \"editor\" ? role : null;\n}\n\nfunction assertMayGrant(actorRole: SiteRole | null, siteId: string, granting: \"owner\" | \"editor\"): void {\n if (actorRole === \"staff\") return;\n if (actorRole === \"owner\" && granting === \"editor\") return;\n throw new ForbiddenError(siteId, granting === \"owner\" ? \"staff\" : \"owner\", actorRole);\n}\n\nexport async function inviteUser(ctx: ServiceContext, input: InviteInput): Promise<InviteResult> {\n const svc = createServiceClient(ctx);\n const actorRole = await resolveActorRole(svc, input.actor.id, input.siteId);\n assertMayGrant(actorRole, input.siteId, input.role);\n\n const email = input.email.trim().toLowerCase();\n let userId: string | undefined;\n let invitedByEmail = false;\n\n const invite = await svc.auth.admin.inviteUserByEmail(email, {\n redirectTo: input.redirectTo,\n data: { site_id: input.siteId, role: input.role },\n });\n\n if (!invite.error) {\n userId = invite.data.user.id;\n invitedByEmail = true;\n } else if (/already|exists|registered/i.test(invite.error.message)) {\n // Existing auth user (works for another site, or is staff): just add the row.\n userId = await findUserIdByEmail(svc, email);\n if (!userId) throw new IdentityError(`Could not find existing user ${email}`);\n } else {\n throw new IdentityError(`Invite failed: ${invite.error.message}`);\n }\n\n const upsert = await svc\n .from(\"site_users\")\n .upsert({ user_id: userId, site_id: input.siteId, role: input.role, invited_by: input.actor.id }, { onConflict: \"user_id,site_id\" });\n if (upsert.error) throw upsert.error;\n\n return { userId, siteId: input.siteId, role: input.role, invitedByEmail };\n}\n\nasync function findUserIdByEmail(svc: ServiceClient, email: string): Promise<string | undefined> {\n // listUsers is paged; identity holds tens of users, not thousands.\n for (let page = 1; page <= 20; page++) {\n const { data, error } = await svc.auth.admin.listUsers({ page, perPage: 200 });\n if (error) throw error;\n const hit = data.users.find((u) => u.email?.toLowerCase() === email);\n if (hit) return hit.id;\n if (data.users.length < 200) break;\n }\n return undefined;\n}\n\nexport interface RemoveInput {\n userId: string;\n siteId: string;\n actor: { id: string };\n}\n\n/** Removes the membership only. The auth user survives (they may belong to other sites). */\nexport async function removeSiteUser(ctx: ServiceContext, input: RemoveInput): Promise<void> {\n const svc = createServiceClient(ctx);\n const actorRole = await resolveActorRole(svc, input.actor.id, input.siteId);\n\n const target = await svc.from(\"site_users\").select(\"role\").eq(\"user_id\", input.userId).eq(\"site_id\", input.siteId).maybeSingle();\n if (target.error) throw target.error;\n const targetRole = ((target.data as { role?: string } | null)?.role ?? \"editor\") as \"owner\" | \"editor\";\n assertMayGrant(actorRole, input.siteId, targetRole);\n\n const del = await svc.from(\"site_users\").delete().eq(\"user_id\", input.userId).eq(\"site_id\", input.siteId);\n if (del.error) throw del.error;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/auth/client.ts","../../src/auth/session.ts","../../src/auth/errors.ts","../../src/auth/types.ts","../../src/auth/roles.ts","../../src/auth/callback.ts","../../src/auth/signout.ts","../../src/auth/service.ts","../../src/auth/branding.ts","../../src/auth/members.ts","../../src/auth/profile.ts"],"names":["createServerClient","createClient"],"mappings":";;;;;;;AAuBO,SAAS,qBAAqB,GAAA,EAAsC;AACzE,EAAA,MAAM,QAAQ,GAAA,CAAI,OAAA;AAClB,EAAA,OAAOA,sBAAA,CAAmB,GAAA,CAAI,GAAA,EAAK,GAAA,CAAI,OAAA,EAAS;AAAA,IAC9C,OAAA,EAAS;AAAA,MACP,MAAA,EAAQ,MAAM,KAAA,CAAM,MAAA,EAAO;AAAA,MAC3B,MAAA,EAAQ,CAAC,IAAA,KAAwB;AAI/B,QAAA,IAAI;AACF,UAAA,IAAI,OAAO,KAAA,CAAM,MAAA,KAAW,UAAA,EAAY;AACtC,YAAA,KAAK,KAAA,CAAM,OAAO,IAAI,CAAA;AAAA,UACxB,CAAA,MAAA,IAAW,OAAO,KAAA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c,MAAM,OAAO,CAAA;AAChD,EAAA,OAAO,SAAA,CAAU,IAAI,IAAI,CAAA;AAC3B;AAGA,eAAsB,WAAA,CAAY,QAAwB,QAAA,EAAiC;AACzF,EAAA,IAAI,OAAO,aAAa,QAAA,IAAY,QAAA,CAAS,SAAS,mBAAA,EAAqB,MAAM,IAAI,aAAA,CAAc,kBAAkB,CAAA;AACrH,EAAA,MAAM,EAAE,OAAM,GAAI,MAAM,OAAO,IAAA,CAAK,UAAA,CAAW,EAAE,QAAA,EAAU,CAAA;AAC3D,EAAA,IAAI,KAAA,EAAO,MAAM,IAAI,aAAA,CAAc,MAAM,OAAO,CAAA;AAClD","file":"index.cjs","sourcesContent":["/**\n * createIdentityClient — a Supabase client bound to the request's cookies.\n *\n * Built per request from arguments. A Next 16 route does:\n *\n * const store = await cookies();\n * const client = createIdentityClient({ url, anonKey, cookies: store });\n *\n * No `next` import here. The CookieStore interface accepts either shape a\n * host might hand us: @supabase/ssr's { getAll, setAll }, or Next's\n * cookies() store, which has { getAll, set } — the session cookies are\n * written through whichever exists. (Observed 2026-09-08: with only setAll\n * supported, Next's store silently wrote nothing and every sign-in bounced.)\n */\n\nimport { createServerClient } from \"@supabase/ssr\";\nimport type { SupabaseClient } from \"@supabase/supabase-js\";\nimport type { IdentityContext } from \"./types\";\n\nexport type IdentityClient = SupabaseClient;\n\ntype CookieToSet = { name: string; value: string; options?: Record<string, unknown> };\n\nexport function createIdentityClient(ctx: IdentityContext): IdentityClient {\n const store = ctx.cookies;\n return createServerClient(ctx.url, ctx.anonKey, {\n cookies: {\n getAll: () => store.getAll(),\n setAll: (list: CookieToSet[]) => {\n // Server Components can't set cookies; @supabase/ssr documents that\n // swallowing the failure there is correct — middleware / route\n // handlers refresh the session instead.\n try {\n if (typeof store.setAll === \"function\") {\n void store.setAll(list);\n } else if (typeof store.set === \"function\") {\n for (const c of list) store.set(c.name, c.value, c.options);\n }\n } catch {\n /* read-only cookie store */\n }\n },\n },\n });\n}\n","/**\n * getCurrentUser — who is signed in, verified against the auth server.\n *\n * `getUser()` round-trips to Supabase and validates the JWT; `getSession()`\n * only decodes the cookie and is not trustworthy on the server. Always this.\n */\n\nimport type { IdentityClient } from \"./client\";\nimport type { IdentityUser } from \"./types\";\n\nexport async function getCurrentUser(client: IdentityClient): Promise<IdentityUser | null> {\n const { data, error } = await client.auth.getUser();\n if (error || !data.user) return null;\n return { id: data.user.id, email: data.user.email ?? null };\n}\n","/** Typed errors so a route can map them to 401 / 403 without string matching. */\n\nexport class IdentityError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"IdentityError\";\n }\n}\n\n/** No session — send them to sign in. Maps to 401. */\nexport class UnauthenticatedError extends IdentityError {\n constructor() {\n super(\"Not signed in\");\n this.name = \"UnauthenticatedError\";\n }\n}\n\n/** Signed in, but not allowed to do this here. Maps to 403. */\nexport class ForbiddenError extends IdentityError {\n readonly siteId: string;\n readonly required: string;\n readonly actual: string | null;\n\n constructor(siteId: string, required: string, actual: string | null) {\n super(`Requires ${required} on site \"${siteId}\" (you are ${actual ?? \"not a member\"})`);\n this.name = \"ForbiddenError\";\n this.siteId = siteId;\n this.required = required;\n this.actual = actual;\n }\n}\n","/**\n * Types for the identity layer. All config arrives as arguments — never env.\n */\n\n/**\n * Either cookie-store shape a host can hand us:\n * - @supabase/ssr's CookieMethodsServer: { getAll, setAll }\n * - Next's `await cookies()` store: { getAll, set }\n * `setAll` is preferred when present; otherwise each cookie goes through `set`.\n */\nexport interface CookieStore {\n getAll(): { name: string; value: string }[] | Promise<{ name: string; value: string }[]>;\n setAll?(cookies: { name: string; value: string; options?: Record<string, unknown> }[]): void | Promise<void>;\n set?(name: string, value: string, options?: Record<string, unknown>): unknown;\n}\n\n/** Enough to read the signed-in user with the anon key + their cookies. */\nexport interface IdentityContext {\n url: string;\n anonKey: string;\n cookies: CookieStore;\n}\n\n/** Server-only. The service-role key bypasses RLS; the module checks roles in code first. */\nexport interface ServiceContext {\n url: string;\n serviceRoleKey: string;\n}\n\nexport type SiteRole = \"staff\" | \"owner\" | \"editor\";\n\nexport const ROLE_RANK: Record<SiteRole, number> = { editor: 1, owner: 2, staff: 3 };\n\nexport interface IdentityUser {\n id: string;\n email: string | null;\n}\n\nexport interface SiteMembership {\n userId: string;\n siteId: string;\n role: \"owner\" | \"editor\";\n}\n","/**\n * Role resolution and the request gate.\n *\n * Two selects on the caller's OWN rows — both permitted by RLS with the anon\n * key, so this never needs the service role. Staff wins over any site role.\n */\n\nimport type { IdentityClient } from \"./client\";\nimport { ForbiddenError, UnauthenticatedError } from \"./errors\";\nimport { getCurrentUser } from \"./session\";\nimport { ROLE_RANK, type IdentityUser, type SiteRole } from \"./types\";\n\nexport async function getSiteRole(client: IdentityClient, siteId: string, userId?: string): Promise<SiteRole | null> {\n const uid = userId ?? (await getCurrentUser(client))?.id;\n if (!uid) return null;\n\n const [staff, member] = await Promise.all([\n client.from(\"staff\").select(\"user_id\").eq(\"user_id\", uid).maybeSingle(),\n client.from(\"site_users\").select(\"role\").eq(\"user_id\", uid).eq(\"site_id\", siteId).maybeSingle(),\n ]);\n\n if (staff.error) throw staff.error;\n if (member.error) throw member.error;\n\n if (staff.data) return \"staff\";\n const role = (member.data as { role?: string } | null)?.role;\n return role === \"owner\" || role === \"editor\" ? role : null;\n}\n\nexport function roleAtLeast(actual: SiteRole | null, minimum: SiteRole): boolean {\n return actual !== null && ROLE_RANK[actual] >= ROLE_RANK[minimum];\n}\n\nexport interface RequireOptions {\n /** Default \"editor\" — any member of the site. */\n minimumRole?: SiteRole;\n}\n\n/**\n * The gate. Throws UnauthenticatedError (→ sign in) or ForbiddenError (→ 403);\n * otherwise returns the user and their effective role for this site.\n */\nexport async function requireSiteUser(\n client: IdentityClient,\n siteId: string,\n opts: RequireOptions = {},\n): Promise<{ user: IdentityUser; role: SiteRole }> {\n const user = await getCurrentUser(client);\n if (!user) throw new UnauthenticatedError();\n\n const minimum = opts.minimumRole ?? \"editor\";\n const role = await getSiteRole(client, siteId, user.id);\n if (!roleAtLeast(role, minimum)) throw new ForbiddenError(siteId, minimum, role);\n\n return { user, role: role as SiteRole };\n}\n","/**\n * handleAuthCallback — the emailed magic link lands here with ?code=…&next=…\n *\n * Exchanges the PKCE code for a session (cookies are written through the\n * adapter the client was built with) and says where to send the user. The\n * `next` parameter is only honoured when it is a plain same-origin path —\n * \"//evil.example\" and \"https://…\" fall back, which closes the open-redirect\n * a naive startsWith(\"/\") check leaves open.\n */\n\nimport type { IdentityClient } from \"./client\";\n\nexport type CallbackResult =\n | { ok: true; redirectTo: string }\n | { ok: false; reason: \"missing_code\" | \"exchange_failed\"; message?: string };\n\nexport interface CallbackOptions {\n /** Where to go when `next` is absent or unsafe. Default \"/admin\". */\n fallbackPath?: string;\n}\n\nexport function safeNextPath(next: string | null | undefined, fallback: string): string {\n if (!next) return fallback;\n // Exactly one leading slash, then not another slash or backslash.\n if (!/^\\/(?![/\\\\])/.test(next)) return fallback;\n // No scheme smuggling or CR/LF.\n if (/[\\r\\n]/.test(next) || /^\\/[^?#]*:\\/\\//.test(next)) return fallback;\n return next;\n}\n\nexport async function handleAuthCallback(\n client: IdentityClient,\n requestUrl: string | URL,\n opts: CallbackOptions = {},\n): Promise<CallbackResult> {\n const fallback = opts.fallbackPath ?? \"/admin\";\n const url = typeof requestUrl === \"string\" ? new URL(requestUrl, \"http://placeholder.invalid\") : requestUrl;\n const code = url.searchParams.get(\"code\");\n if (!code) return { ok: false, reason: \"missing_code\" };\n\n const { error } = await client.auth.exchangeCodeForSession(code);\n if (error) return { ok: false, reason: \"exchange_failed\", message: error.message };\n\n return { ok: true, redirectTo: safeNextPath(url.searchParams.get(\"next\"), fallback) };\n}\n","/** signOut — clears this browser's session. The caller redirects afterwards. */\n\nimport type { IdentityClient } from \"./client\";\n\nexport async function signOut(client: IdentityClient): Promise<void> {\n const { error } = await client.auth.signOut({ scope: \"local\" });\n if (error) throw error;\n}\n","/**\n * The only file that touches the service-role key. Invite and remove.\n *\n * Authorization is checked HERE, in code, before the privileged client does\n * anything — RLS doesn't apply to the service role, so this is the guard:\n * staff → may grant owner or editor on any site\n * owner → may grant editor on their own site only\n * editor → may not invite\n *\n * `actor` is the caller's already-resolved identity (from requireSiteUser on\n * the anon client). Its role is re-resolved with the service client so a\n * caller can't lie about it.\n */\n\nimport { createClient, type SupabaseClient } from \"@supabase/supabase-js\";\nimport { ForbiddenError, IdentityError } from \"./errors\";\nimport type { ServiceContext, SiteRole } from \"./types\";\n\nexport type ServiceClient = SupabaseClient;\n\nexport function createServiceClient(ctx: ServiceContext): ServiceClient {\n return createClient(ctx.url, ctx.serviceRoleKey, {\n auth: { persistSession: false, autoRefreshToken: false },\n });\n}\n\nexport interface InviteInput {\n email: string;\n siteId: string;\n role: \"owner\" | \"editor\";\n /** The signed-in user performing the invite. */\n actor: { id: string };\n /** Where the invite email's link lands — the site's /admin/auth/callback. */\n redirectTo: string;\n}\n\nexport interface InviteResult {\n userId: string;\n siteId: string;\n role: \"owner\" | \"editor\";\n /** false when the auth user already existed and only the membership was added. */\n invitedByEmail: boolean;\n}\n\nasync function resolveActorRole(svc: ServiceClient, actorId: string, siteId: string): Promise<SiteRole | null> {\n const staff = await svc.from(\"staff\").select(\"user_id\").eq(\"user_id\", actorId).maybeSingle();\n if (staff.error) throw staff.error;\n if (staff.data) return \"staff\";\n const m = await svc.from(\"site_users\").select(\"role\").eq(\"user_id\", actorId).eq(\"site_id\", siteId).maybeSingle();\n if (m.error) throw m.error;\n const role = (m.data as { role?: string } | null)?.role;\n return role === \"owner\" || role === \"editor\" ? role : null;\n}\n\nfunction assertMayGrant(actorRole: SiteRole | null, siteId: string, granting: \"owner\" | \"editor\"): void {\n if (actorRole === \"staff\") return;\n if (actorRole === \"owner\" && granting === \"editor\") return;\n throw new ForbiddenError(siteId, granting === \"owner\" ? \"staff\" : \"owner\", actorRole);\n}\n\nexport async function inviteUser(ctx: ServiceContext, input: InviteInput): Promise<InviteResult> {\n const svc = createServiceClient(ctx);\n const actorRole = await resolveActorRole(svc, input.actor.id, input.siteId);\n assertMayGrant(actorRole, input.siteId, input.role);\n\n const email = input.email.trim().toLowerCase();\n let userId: string | undefined;\n let invitedByEmail = false;\n\n const invite = await svc.auth.admin.inviteUserByEmail(email, {\n redirectTo: input.redirectTo,\n data: { site_id: input.siteId, role: input.role },\n });\n\n if (!invite.error) {\n userId = invite.data.user.id;\n invitedByEmail = true;\n } else if (/already|exists|registered/i.test(invite.error.message)) {\n // Existing auth user (works for another site, or is staff): just add the row.\n userId = await findUserIdByEmail(svc, email);\n if (!userId) throw new IdentityError(`Could not find existing user ${email}`);\n } else {\n throw new IdentityError(`Invite failed: ${invite.error.message}`);\n }\n\n const upsert = await svc\n .from(\"site_users\")\n .upsert({ user_id: userId, site_id: input.siteId, role: input.role, invited_by: input.actor.id }, { onConflict: \"user_id,site_id\" });\n if (upsert.error) throw upsert.error;\n\n return { userId, siteId: input.siteId, role: input.role, invitedByEmail };\n}\n\nasync function findUserIdByEmail(svc: ServiceClient, email: string): Promise<string | undefined> {\n // listUsers is paged; identity holds tens of users, not thousands.\n for (let page = 1; page <= 20; page++) {\n const { data, error } = await svc.auth.admin.listUsers({ page, perPage: 200 });\n if (error) throw error;\n const hit = data.users.find((u) => u.email?.toLowerCase() === email);\n if (hit) return hit.id;\n if (data.users.length < 200) break;\n }\n return undefined;\n}\n\nexport interface RemoveInput {\n userId: string;\n siteId: string;\n actor: { id: string };\n}\n\n/** Removes the membership only. The auth user survives (they may belong to other sites). */\nexport async function removeSiteUser(ctx: ServiceContext, input: RemoveInput): Promise<void> {\n const svc = createServiceClient(ctx);\n const actorRole = await resolveActorRole(svc, input.actor.id, input.siteId);\n\n const target = await svc.from(\"site_users\").select(\"role\").eq(\"user_id\", input.userId).eq(\"site_id\", input.siteId).maybeSingle();\n if (target.error) throw target.error;\n const targetRole = ((target.data as { role?: string } | null)?.role ?? \"editor\") as \"owner\" | \"editor\";\n assertMayGrant(actorRole, input.siteId, targetRole);\n if (targetRole === \"owner\") await assertNotLastOwner(svc, input.siteId);\n\n const del = await svc.from(\"site_users\").delete().eq(\"user_id\", input.userId).eq(\"site_id\", input.siteId);\n if (del.error) throw del.error;\n}\n\nexport interface RoleInput {\n userId: string;\n siteId: string;\n role: \"owner\" | \"editor\";\n actor: { id: string };\n}\n\n/**\n * Change a member's role. Same grant rules as invite (staff anything; an owner\n * may only make people editors on their own site), plus: the last owner of a\n * site can never be demoted, otherwise nobody could manage it.\n */\nexport async function updateSiteRole(ctx: ServiceContext, input: RoleInput): Promise<void> {\n const svc = createServiceClient(ctx);\n const actorRole = await resolveActorRole(svc, input.actor.id, input.siteId);\n assertMayGrant(actorRole, input.siteId, input.role);\n\n const target = await svc.from(\"site_users\").select(\"role\").eq(\"user_id\", input.userId).eq(\"site_id\", input.siteId).maybeSingle();\n if (target.error) throw target.error;\n const current = (target.data as { role?: string } | null)?.role;\n if (!current) throw new IdentityError(\"That person isn't a member of this site.\");\n // Demoting an owner is an owner-level act — an owner may not do it to a peer.\n if (current === \"owner\") assertMayGrant(actorRole, input.siteId, \"owner\");\n if (current === \"owner\" && input.role === \"editor\") await assertNotLastOwner(svc, input.siteId);\n\n const upd = await svc.from(\"site_users\").update({ role: input.role }).eq(\"user_id\", input.userId).eq(\"site_id\", input.siteId);\n if (upd.error) throw upd.error;\n}\n\nexport async function assertNotLastOwner(svc: ServiceClient, siteId: string): Promise<void> {\n const owners = await svc.from(\"site_users\").select(\"user_id\").eq(\"site_id\", siteId).eq(\"role\", \"owner\");\n if (owners.error) throw owners.error;\n if ((owners.data ?? []).length <= 1) throw new IdentityError(\"A site needs at least one owner — make someone else an owner first.\");\n}\n\nexport interface ResendInput {\n email: string;\n siteId: string;\n actor: { id: string };\n redirectTo: string;\n}\n\n/** Send the invite email again. An already-active user gets no email — they can just sign in. */\nexport async function resendInvite(ctx: ServiceContext, input: ResendInput): Promise<{ resent: boolean; message: string }> {\n const svc = createServiceClient(ctx);\n const actorRole = await resolveActorRole(svc, input.actor.id, input.siteId);\n assertMayGrant(actorRole, input.siteId, \"editor\");\n\n const email = input.email.trim().toLowerCase();\n const again = await svc.auth.admin.inviteUserByEmail(email, { redirectTo: input.redirectTo, data: { site_id: input.siteId } });\n if (!again.error) return { resent: true, message: `Invitation sent again to ${email}.` };\n if (/already|exists|registered|confirmed/i.test(again.error.message)) {\n return { resent: false, message: `${email} has already accepted — they can sign in with an email link any time.` };\n }\n throw new IdentityError(`Resend failed: ${again.error.message}`);\n}\n","/**\n * Per-site branding — the Business tab. Reads and writes public.sites through\n * the signed-in user's client, so RLS decides who may (members read; owners\n * and staff write). Nothing here needs the service role.\n */\n\nimport type { IdentityClient } from \"./client\";\n\nexport interface SiteBranding {\n name: string;\n logoUrl: string | null;\n faviconUrl: string | null;\n businessEmail: string | null;\n businessPhone: string | null;\n}\n\nexport type BrandingPatch = Partial<SiteBranding> & { name: string };\n\ninterface Row {\n name: string;\n logo_url: string | null;\n favicon_url: string | null;\n business_email: string | null;\n business_phone: string | null;\n}\n\nexport async function getSiteBranding(client: IdentityClient, siteId: string): Promise<SiteBranding | null> {\n const { data, error } = await client.from(\"sites\").select(\"name, logo_url, favicon_url, business_email, business_phone\").eq(\"site_id\", siteId).maybeSingle();\n if (error) throw error;\n if (!data) return null;\n const r = data as Row;\n return { name: r.name, logoUrl: r.logo_url, faviconUrl: r.favicon_url, businessEmail: r.business_email, businessPhone: r.business_phone };\n}\n\n/** Upsert the row. RLS refuses anyone but the site's owners or staff. */\nexport async function updateSiteBranding(client: IdentityClient, siteId: string, patch: BrandingPatch): Promise<void> {\n const row = {\n site_id: siteId,\n name: patch.name.trim(),\n logo_url: patch.logoUrl ?? null,\n favicon_url: patch.faviconUrl ?? null,\n business_email: emptyToNull(patch.businessEmail),\n business_phone: emptyToNull(patch.businessPhone),\n };\n const { error } = await client.from(\"sites\").upsert(row, { onConflict: \"site_id\" });\n if (error) throw error;\n}\n\nfunction emptyToNull(v: string | null | undefined): string | null {\n const s = (v ?? \"\").trim();\n return s === \"\" ? null : s;\n}\n","/**\n * The Team roster — public.site_members(site_id), a SECURITY DEFINER function\n * that returns rows only to that site's owners or staff (editors get a 42501).\n * Read with the signed-in user's client; writes are in service.ts.\n */\n\nimport type { IdentityClient } from \"./client\";\nimport { ForbiddenError } from \"./errors\";\n\nexport interface SiteMember {\n userId: string;\n email: string;\n name: string | null;\n avatarUrl: string | null;\n role: \"owner\" | \"editor\";\n /** \"invited\" until the person has signed in once. */\n status: \"invited\" | \"active\";\n invitedAt: string;\n}\n\ninterface Row {\n user_id: string;\n email: string;\n name: string | null;\n avatar_url: string | null;\n role: string;\n status: string;\n invited_at: string;\n}\n\nexport async function listSiteMembers(client: IdentityClient, siteId: string): Promise<SiteMember[]> {\n const { data, error } = await client.rpc(\"site_members\", { p_site_id: siteId });\n if (error) {\n if (error.code === \"42501\" || /not allowed/i.test(error.message)) throw new ForbiddenError(siteId, \"owner\", null);\n throw error;\n }\n return ((data ?? []) as Row[]).map((r) => ({\n userId: r.user_id,\n email: r.email,\n name: r.name,\n avatarUrl: r.avatar_url,\n role: r.role === \"owner\" ? \"owner\" : \"editor\",\n status: r.status === \"active\" ? \"active\" : \"invited\",\n invitedAt: r.invited_at,\n }));\n}\n","/**\n * My Profile and Change Password — the signed-in user editing themselves,\n * through their own client (auth.updateUser). Name and photo live in user\n * metadata; the photo file itself is in the avatars bucket (uploaded by the\n * host, path avatars/{uid}/…).\n */\n\nimport type { IdentityClient } from \"./client\";\nimport { IdentityError } from \"./errors\";\n\nexport const MIN_PASSWORD_LENGTH = 10;\nexport const PASSWORD_TOO_SHORT = \"Use at least 10 characters.\";\n\nexport interface ProfilePatch {\n name?: string | null;\n /** The byline — how the name appears on published posts. Blank means \"same as name\". */\n displayName?: string | null;\n avatarUrl?: string | null;\n}\n\nexport interface Profile {\n name: string | null;\n /** Resolved byline: display_name if set, else name. */\n displayName: string | null;\n avatarUrl: string | null;\n}\n\nexport function profileOf(user: { user_metadata?: Record<string, unknown> | null } | null | undefined): Profile {\n const m = user?.user_metadata ?? {};\n const str = (v: unknown) => (typeof v === \"string\" && v.trim() ? v.trim() : null);\n const name = str(m.name);\n const displayName = str(m.display_name) ?? name;\n const avatarUrl = typeof m.avatar_url === \"string\" && m.avatar_url ? m.avatar_url : null;\n return { name, displayName, avatarUrl };\n}\n\nexport async function updateProfile(client: IdentityClient, patch: ProfilePatch): Promise<Profile> {\n const data: Record<string, unknown> = {};\n if (patch.name !== undefined) data.name = (patch.name ?? \"\").trim().slice(0, 80);\n if (patch.displayName !== undefined) data.display_name = (patch.displayName ?? \"\").trim().slice(0, 80);\n if (patch.avatarUrl !== undefined) data.avatar_url = patch.avatarUrl ?? \"\";\n const { data: res, error } = await client.auth.updateUser({ data });\n if (error) throw new IdentityError(error.message);\n return profileOf(res.user);\n}\n\n/** Sets (or replaces) a password. Magic links keep working alongside it. */\nexport async function setPassword(client: IdentityClient, password: string): Promise<void> {\n if (typeof password !== \"string\" || password.length < MIN_PASSWORD_LENGTH) throw new IdentityError(PASSWORD_TOO_SHORT);\n const { error } = await client.auth.updateUser({ password });\n if (error) throw new IdentityError(error.message);\n}\n"]}
|
package/dist/auth/index.d.cts
CHANGED
|
@@ -167,6 +167,99 @@ interface RemoveInput {
|
|
|
167
167
|
}
|
|
168
168
|
/** Removes the membership only. The auth user survives (they may belong to other sites). */
|
|
169
169
|
declare function removeSiteUser(ctx: ServiceContext, input: RemoveInput): Promise<void>;
|
|
170
|
+
interface RoleInput {
|
|
171
|
+
userId: string;
|
|
172
|
+
siteId: string;
|
|
173
|
+
role: "owner" | "editor";
|
|
174
|
+
actor: {
|
|
175
|
+
id: string;
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Change a member's role. Same grant rules as invite (staff anything; an owner
|
|
180
|
+
* may only make people editors on their own site), plus: the last owner of a
|
|
181
|
+
* site can never be demoted, otherwise nobody could manage it.
|
|
182
|
+
*/
|
|
183
|
+
declare function updateSiteRole(ctx: ServiceContext, input: RoleInput): Promise<void>;
|
|
184
|
+
interface ResendInput {
|
|
185
|
+
email: string;
|
|
186
|
+
siteId: string;
|
|
187
|
+
actor: {
|
|
188
|
+
id: string;
|
|
189
|
+
};
|
|
190
|
+
redirectTo: string;
|
|
191
|
+
}
|
|
192
|
+
/** Send the invite email again. An already-active user gets no email — they can just sign in. */
|
|
193
|
+
declare function resendInvite(ctx: ServiceContext, input: ResendInput): Promise<{
|
|
194
|
+
resent: boolean;
|
|
195
|
+
message: string;
|
|
196
|
+
}>;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Per-site branding — the Business tab. Reads and writes public.sites through
|
|
200
|
+
* the signed-in user's client, so RLS decides who may (members read; owners
|
|
201
|
+
* and staff write). Nothing here needs the service role.
|
|
202
|
+
*/
|
|
203
|
+
|
|
204
|
+
interface SiteBranding {
|
|
205
|
+
name: string;
|
|
206
|
+
logoUrl: string | null;
|
|
207
|
+
faviconUrl: string | null;
|
|
208
|
+
businessEmail: string | null;
|
|
209
|
+
businessPhone: string | null;
|
|
210
|
+
}
|
|
211
|
+
type BrandingPatch = Partial<SiteBranding> & {
|
|
212
|
+
name: string;
|
|
213
|
+
};
|
|
214
|
+
declare function getSiteBranding(client: IdentityClient, siteId: string): Promise<SiteBranding | null>;
|
|
215
|
+
/** Upsert the row. RLS refuses anyone but the site's owners or staff. */
|
|
216
|
+
declare function updateSiteBranding(client: IdentityClient, siteId: string, patch: BrandingPatch): Promise<void>;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The Team roster — public.site_members(site_id), a SECURITY DEFINER function
|
|
220
|
+
* that returns rows only to that site's owners or staff (editors get a 42501).
|
|
221
|
+
* Read with the signed-in user's client; writes are in service.ts.
|
|
222
|
+
*/
|
|
223
|
+
|
|
224
|
+
interface SiteMember {
|
|
225
|
+
userId: string;
|
|
226
|
+
email: string;
|
|
227
|
+
name: string | null;
|
|
228
|
+
avatarUrl: string | null;
|
|
229
|
+
role: "owner" | "editor";
|
|
230
|
+
/** "invited" until the person has signed in once. */
|
|
231
|
+
status: "invited" | "active";
|
|
232
|
+
invitedAt: string;
|
|
233
|
+
}
|
|
234
|
+
declare function listSiteMembers(client: IdentityClient, siteId: string): Promise<SiteMember[]>;
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* My Profile and Change Password — the signed-in user editing themselves,
|
|
238
|
+
* through their own client (auth.updateUser). Name and photo live in user
|
|
239
|
+
* metadata; the photo file itself is in the avatars bucket (uploaded by the
|
|
240
|
+
* host, path avatars/{uid}/…).
|
|
241
|
+
*/
|
|
242
|
+
|
|
243
|
+
declare const MIN_PASSWORD_LENGTH = 10;
|
|
244
|
+
declare const PASSWORD_TOO_SHORT = "Use at least 10 characters.";
|
|
245
|
+
interface ProfilePatch {
|
|
246
|
+
name?: string | null;
|
|
247
|
+
/** The byline — how the name appears on published posts. Blank means "same as name". */
|
|
248
|
+
displayName?: string | null;
|
|
249
|
+
avatarUrl?: string | null;
|
|
250
|
+
}
|
|
251
|
+
interface Profile {
|
|
252
|
+
name: string | null;
|
|
253
|
+
/** Resolved byline: display_name if set, else name. */
|
|
254
|
+
displayName: string | null;
|
|
255
|
+
avatarUrl: string | null;
|
|
256
|
+
}
|
|
257
|
+
declare function profileOf(user: {
|
|
258
|
+
user_metadata?: Record<string, unknown> | null;
|
|
259
|
+
} | null | undefined): Profile;
|
|
260
|
+
declare function updateProfile(client: IdentityClient, patch: ProfilePatch): Promise<Profile>;
|
|
261
|
+
/** Sets (or replaces) a password. Magic links keep working alongside it. */
|
|
262
|
+
declare function setPassword(client: IdentityClient, password: string): Promise<void>;
|
|
170
263
|
|
|
171
264
|
/** Typed errors so a route can map them to 401 / 403 without string matching. */
|
|
172
265
|
declare class IdentityError extends Error {
|
|
@@ -184,4 +277,4 @@ declare class ForbiddenError extends IdentityError {
|
|
|
184
277
|
constructor(siteId: string, required: string, actual: string | null);
|
|
185
278
|
}
|
|
186
279
|
|
|
187
|
-
export { type CallbackOptions, type CallbackResult, type CookieStore, ForbiddenError, type IdentityClient, type IdentityContext, IdentityError, type IdentityUser, type InviteInput, type InviteResult, ROLE_RANK, type RemoveInput, type RequireOptions, type ServiceContext, type SiteMembership, type SiteRole, UnauthenticatedError, createIdentityClient, getCurrentUser, getSiteRole, handleAuthCallback, inviteUser, removeSiteUser, requireSiteUser, roleAtLeast, safeNextPath, signOut };
|
|
280
|
+
export { type BrandingPatch, type CallbackOptions, type CallbackResult, type CookieStore, ForbiddenError, type IdentityClient, type IdentityContext, IdentityError, type IdentityUser, type InviteInput, type InviteResult, MIN_PASSWORD_LENGTH, PASSWORD_TOO_SHORT, type Profile, type ProfilePatch, ROLE_RANK, type RemoveInput, type RequireOptions, type ResendInput, type RoleInput, type ServiceContext, type SiteBranding, type SiteMember, type SiteMembership, type SiteRole, UnauthenticatedError, createIdentityClient, getCurrentUser, getSiteBranding, getSiteRole, handleAuthCallback, inviteUser, listSiteMembers, profileOf, removeSiteUser, requireSiteUser, resendInvite, roleAtLeast, safeNextPath, setPassword, signOut, updateProfile, updateSiteBranding, updateSiteRole };
|
package/dist/auth/index.d.ts
CHANGED
|
@@ -167,6 +167,99 @@ interface RemoveInput {
|
|
|
167
167
|
}
|
|
168
168
|
/** Removes the membership only. The auth user survives (they may belong to other sites). */
|
|
169
169
|
declare function removeSiteUser(ctx: ServiceContext, input: RemoveInput): Promise<void>;
|
|
170
|
+
interface RoleInput {
|
|
171
|
+
userId: string;
|
|
172
|
+
siteId: string;
|
|
173
|
+
role: "owner" | "editor";
|
|
174
|
+
actor: {
|
|
175
|
+
id: string;
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Change a member's role. Same grant rules as invite (staff anything; an owner
|
|
180
|
+
* may only make people editors on their own site), plus: the last owner of a
|
|
181
|
+
* site can never be demoted, otherwise nobody could manage it.
|
|
182
|
+
*/
|
|
183
|
+
declare function updateSiteRole(ctx: ServiceContext, input: RoleInput): Promise<void>;
|
|
184
|
+
interface ResendInput {
|
|
185
|
+
email: string;
|
|
186
|
+
siteId: string;
|
|
187
|
+
actor: {
|
|
188
|
+
id: string;
|
|
189
|
+
};
|
|
190
|
+
redirectTo: string;
|
|
191
|
+
}
|
|
192
|
+
/** Send the invite email again. An already-active user gets no email — they can just sign in. */
|
|
193
|
+
declare function resendInvite(ctx: ServiceContext, input: ResendInput): Promise<{
|
|
194
|
+
resent: boolean;
|
|
195
|
+
message: string;
|
|
196
|
+
}>;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Per-site branding — the Business tab. Reads and writes public.sites through
|
|
200
|
+
* the signed-in user's client, so RLS decides who may (members read; owners
|
|
201
|
+
* and staff write). Nothing here needs the service role.
|
|
202
|
+
*/
|
|
203
|
+
|
|
204
|
+
interface SiteBranding {
|
|
205
|
+
name: string;
|
|
206
|
+
logoUrl: string | null;
|
|
207
|
+
faviconUrl: string | null;
|
|
208
|
+
businessEmail: string | null;
|
|
209
|
+
businessPhone: string | null;
|
|
210
|
+
}
|
|
211
|
+
type BrandingPatch = Partial<SiteBranding> & {
|
|
212
|
+
name: string;
|
|
213
|
+
};
|
|
214
|
+
declare function getSiteBranding(client: IdentityClient, siteId: string): Promise<SiteBranding | null>;
|
|
215
|
+
/** Upsert the row. RLS refuses anyone but the site's owners or staff. */
|
|
216
|
+
declare function updateSiteBranding(client: IdentityClient, siteId: string, patch: BrandingPatch): Promise<void>;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The Team roster — public.site_members(site_id), a SECURITY DEFINER function
|
|
220
|
+
* that returns rows only to that site's owners or staff (editors get a 42501).
|
|
221
|
+
* Read with the signed-in user's client; writes are in service.ts.
|
|
222
|
+
*/
|
|
223
|
+
|
|
224
|
+
interface SiteMember {
|
|
225
|
+
userId: string;
|
|
226
|
+
email: string;
|
|
227
|
+
name: string | null;
|
|
228
|
+
avatarUrl: string | null;
|
|
229
|
+
role: "owner" | "editor";
|
|
230
|
+
/** "invited" until the person has signed in once. */
|
|
231
|
+
status: "invited" | "active";
|
|
232
|
+
invitedAt: string;
|
|
233
|
+
}
|
|
234
|
+
declare function listSiteMembers(client: IdentityClient, siteId: string): Promise<SiteMember[]>;
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* My Profile and Change Password — the signed-in user editing themselves,
|
|
238
|
+
* through their own client (auth.updateUser). Name and photo live in user
|
|
239
|
+
* metadata; the photo file itself is in the avatars bucket (uploaded by the
|
|
240
|
+
* host, path avatars/{uid}/…).
|
|
241
|
+
*/
|
|
242
|
+
|
|
243
|
+
declare const MIN_PASSWORD_LENGTH = 10;
|
|
244
|
+
declare const PASSWORD_TOO_SHORT = "Use at least 10 characters.";
|
|
245
|
+
interface ProfilePatch {
|
|
246
|
+
name?: string | null;
|
|
247
|
+
/** The byline — how the name appears on published posts. Blank means "same as name". */
|
|
248
|
+
displayName?: string | null;
|
|
249
|
+
avatarUrl?: string | null;
|
|
250
|
+
}
|
|
251
|
+
interface Profile {
|
|
252
|
+
name: string | null;
|
|
253
|
+
/** Resolved byline: display_name if set, else name. */
|
|
254
|
+
displayName: string | null;
|
|
255
|
+
avatarUrl: string | null;
|
|
256
|
+
}
|
|
257
|
+
declare function profileOf(user: {
|
|
258
|
+
user_metadata?: Record<string, unknown> | null;
|
|
259
|
+
} | null | undefined): Profile;
|
|
260
|
+
declare function updateProfile(client: IdentityClient, patch: ProfilePatch): Promise<Profile>;
|
|
261
|
+
/** Sets (or replaces) a password. Magic links keep working alongside it. */
|
|
262
|
+
declare function setPassword(client: IdentityClient, password: string): Promise<void>;
|
|
170
263
|
|
|
171
264
|
/** Typed errors so a route can map them to 401 / 403 without string matching. */
|
|
172
265
|
declare class IdentityError extends Error {
|
|
@@ -184,4 +277,4 @@ declare class ForbiddenError extends IdentityError {
|
|
|
184
277
|
constructor(siteId: string, required: string, actual: string | null);
|
|
185
278
|
}
|
|
186
279
|
|
|
187
|
-
export { type CallbackOptions, type CallbackResult, type CookieStore, ForbiddenError, type IdentityClient, type IdentityContext, IdentityError, type IdentityUser, type InviteInput, type InviteResult, ROLE_RANK, type RemoveInput, type RequireOptions, type ServiceContext, type SiteMembership, type SiteRole, UnauthenticatedError, createIdentityClient, getCurrentUser, getSiteRole, handleAuthCallback, inviteUser, removeSiteUser, requireSiteUser, roleAtLeast, safeNextPath, signOut };
|
|
280
|
+
export { type BrandingPatch, type CallbackOptions, type CallbackResult, type CookieStore, ForbiddenError, type IdentityClient, type IdentityContext, IdentityError, type IdentityUser, type InviteInput, type InviteResult, MIN_PASSWORD_LENGTH, PASSWORD_TOO_SHORT, type Profile, type ProfilePatch, ROLE_RANK, type RemoveInput, type RequireOptions, type ResendInput, type RoleInput, type ServiceContext, type SiteBranding, type SiteMember, type SiteMembership, type SiteRole, UnauthenticatedError, createIdentityClient, getCurrentUser, getSiteBranding, getSiteRole, handleAuthCallback, inviteUser, listSiteMembers, profileOf, removeSiteUser, requireSiteUser, resendInvite, roleAtLeast, safeNextPath, setPassword, signOut, updateProfile, updateSiteBranding, updateSiteRole };
|
package/dist/auth/index.js
CHANGED
|
@@ -163,10 +163,110 @@ async function removeSiteUser(ctx, input) {
|
|
|
163
163
|
if (target.error) throw target.error;
|
|
164
164
|
const targetRole = target.data?.role ?? "editor";
|
|
165
165
|
assertMayGrant(actorRole, input.siteId, targetRole);
|
|
166
|
+
if (targetRole === "owner") await assertNotLastOwner(svc, input.siteId);
|
|
166
167
|
const del = await svc.from("site_users").delete().eq("user_id", input.userId).eq("site_id", input.siteId);
|
|
167
168
|
if (del.error) throw del.error;
|
|
168
169
|
}
|
|
170
|
+
async function updateSiteRole(ctx, input) {
|
|
171
|
+
const svc = createServiceClient(ctx);
|
|
172
|
+
const actorRole = await resolveActorRole(svc, input.actor.id, input.siteId);
|
|
173
|
+
assertMayGrant(actorRole, input.siteId, input.role);
|
|
174
|
+
const target = await svc.from("site_users").select("role").eq("user_id", input.userId).eq("site_id", input.siteId).maybeSingle();
|
|
175
|
+
if (target.error) throw target.error;
|
|
176
|
+
const current = target.data?.role;
|
|
177
|
+
if (!current) throw new IdentityError("That person isn't a member of this site.");
|
|
178
|
+
if (current === "owner") assertMayGrant(actorRole, input.siteId, "owner");
|
|
179
|
+
if (current === "owner" && input.role === "editor") await assertNotLastOwner(svc, input.siteId);
|
|
180
|
+
const upd = await svc.from("site_users").update({ role: input.role }).eq("user_id", input.userId).eq("site_id", input.siteId);
|
|
181
|
+
if (upd.error) throw upd.error;
|
|
182
|
+
}
|
|
183
|
+
async function assertNotLastOwner(svc, siteId) {
|
|
184
|
+
const owners = await svc.from("site_users").select("user_id").eq("site_id", siteId).eq("role", "owner");
|
|
185
|
+
if (owners.error) throw owners.error;
|
|
186
|
+
if ((owners.data ?? []).length <= 1) throw new IdentityError("A site needs at least one owner \u2014 make someone else an owner first.");
|
|
187
|
+
}
|
|
188
|
+
async function resendInvite(ctx, input) {
|
|
189
|
+
const svc = createServiceClient(ctx);
|
|
190
|
+
const actorRole = await resolveActorRole(svc, input.actor.id, input.siteId);
|
|
191
|
+
assertMayGrant(actorRole, input.siteId, "editor");
|
|
192
|
+
const email = input.email.trim().toLowerCase();
|
|
193
|
+
const again = await svc.auth.admin.inviteUserByEmail(email, { redirectTo: input.redirectTo, data: { site_id: input.siteId } });
|
|
194
|
+
if (!again.error) return { resent: true, message: `Invitation sent again to ${email}.` };
|
|
195
|
+
if (/already|exists|registered|confirmed/i.test(again.error.message)) {
|
|
196
|
+
return { resent: false, message: `${email} has already accepted \u2014 they can sign in with an email link any time.` };
|
|
197
|
+
}
|
|
198
|
+
throw new IdentityError(`Resend failed: ${again.error.message}`);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/auth/branding.ts
|
|
202
|
+
async function getSiteBranding(client, siteId) {
|
|
203
|
+
const { data, error } = await client.from("sites").select("name, logo_url, favicon_url, business_email, business_phone").eq("site_id", siteId).maybeSingle();
|
|
204
|
+
if (error) throw error;
|
|
205
|
+
if (!data) return null;
|
|
206
|
+
const r = data;
|
|
207
|
+
return { name: r.name, logoUrl: r.logo_url, faviconUrl: r.favicon_url, businessEmail: r.business_email, businessPhone: r.business_phone };
|
|
208
|
+
}
|
|
209
|
+
async function updateSiteBranding(client, siteId, patch) {
|
|
210
|
+
const row = {
|
|
211
|
+
site_id: siteId,
|
|
212
|
+
name: patch.name.trim(),
|
|
213
|
+
logo_url: patch.logoUrl ?? null,
|
|
214
|
+
favicon_url: patch.faviconUrl ?? null,
|
|
215
|
+
business_email: emptyToNull(patch.businessEmail),
|
|
216
|
+
business_phone: emptyToNull(patch.businessPhone)
|
|
217
|
+
};
|
|
218
|
+
const { error } = await client.from("sites").upsert(row, { onConflict: "site_id" });
|
|
219
|
+
if (error) throw error;
|
|
220
|
+
}
|
|
221
|
+
function emptyToNull(v) {
|
|
222
|
+
const s = (v ?? "").trim();
|
|
223
|
+
return s === "" ? null : s;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// src/auth/members.ts
|
|
227
|
+
async function listSiteMembers(client, siteId) {
|
|
228
|
+
const { data, error } = await client.rpc("site_members", { p_site_id: siteId });
|
|
229
|
+
if (error) {
|
|
230
|
+
if (error.code === "42501" || /not allowed/i.test(error.message)) throw new ForbiddenError(siteId, "owner", null);
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
return (data ?? []).map((r) => ({
|
|
234
|
+
userId: r.user_id,
|
|
235
|
+
email: r.email,
|
|
236
|
+
name: r.name,
|
|
237
|
+
avatarUrl: r.avatar_url,
|
|
238
|
+
role: r.role === "owner" ? "owner" : "editor",
|
|
239
|
+
status: r.status === "active" ? "active" : "invited",
|
|
240
|
+
invitedAt: r.invited_at
|
|
241
|
+
}));
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// src/auth/profile.ts
|
|
245
|
+
var MIN_PASSWORD_LENGTH = 10;
|
|
246
|
+
var PASSWORD_TOO_SHORT = "Use at least 10 characters.";
|
|
247
|
+
function profileOf(user) {
|
|
248
|
+
const m = user?.user_metadata ?? {};
|
|
249
|
+
const str = (v) => typeof v === "string" && v.trim() ? v.trim() : null;
|
|
250
|
+
const name = str(m.name);
|
|
251
|
+
const displayName = str(m.display_name) ?? name;
|
|
252
|
+
const avatarUrl = typeof m.avatar_url === "string" && m.avatar_url ? m.avatar_url : null;
|
|
253
|
+
return { name, displayName, avatarUrl };
|
|
254
|
+
}
|
|
255
|
+
async function updateProfile(client, patch) {
|
|
256
|
+
const data = {};
|
|
257
|
+
if (patch.name !== void 0) data.name = (patch.name ?? "").trim().slice(0, 80);
|
|
258
|
+
if (patch.displayName !== void 0) data.display_name = (patch.displayName ?? "").trim().slice(0, 80);
|
|
259
|
+
if (patch.avatarUrl !== void 0) data.avatar_url = patch.avatarUrl ?? "";
|
|
260
|
+
const { data: res, error } = await client.auth.updateUser({ data });
|
|
261
|
+
if (error) throw new IdentityError(error.message);
|
|
262
|
+
return profileOf(res.user);
|
|
263
|
+
}
|
|
264
|
+
async function setPassword(client, password) {
|
|
265
|
+
if (typeof password !== "string" || password.length < MIN_PASSWORD_LENGTH) throw new IdentityError(PASSWORD_TOO_SHORT);
|
|
266
|
+
const { error } = await client.auth.updateUser({ password });
|
|
267
|
+
if (error) throw new IdentityError(error.message);
|
|
268
|
+
}
|
|
169
269
|
|
|
170
|
-
export { ForbiddenError, IdentityError, ROLE_RANK, UnauthenticatedError, createIdentityClient, getCurrentUser, getSiteRole, handleAuthCallback, inviteUser, removeSiteUser, requireSiteUser, roleAtLeast, safeNextPath, signOut };
|
|
270
|
+
export { ForbiddenError, IdentityError, MIN_PASSWORD_LENGTH, PASSWORD_TOO_SHORT, ROLE_RANK, UnauthenticatedError, createIdentityClient, getCurrentUser, getSiteBranding, getSiteRole, handleAuthCallback, inviteUser, listSiteMembers, profileOf, removeSiteUser, requireSiteUser, resendInvite, roleAtLeast, safeNextPath, setPassword, signOut, updateProfile, updateSiteBranding, updateSiteRole };
|
|
171
271
|
//# sourceMappingURL=index.js.map
|
|
172
272
|
//# sourceMappingURL=index.js.map
|