@plutocms/supabase 0.7.1 → 0.7.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.7.2](https://github.com/plutocms/supabase/compare/v0.7.1...v0.7.2) (2026-09-13)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * **permissions:** make requireAdmin safe on a zero-migration database ([#58](https://github.com/plutocms/supabase/issues/58)) ([a83f6d0](https://github.com/plutocms/supabase/commit/a83f6d0bfa3ae150df5b90a06fcd29fe437ad9b7))
9
+
3
10
  ## [0.7.1](https://github.com/plutocms/supabase/compare/v0.7.0...v0.7.1) (2026-09-13)
4
11
 
5
12
 
@@ -30,6 +30,13 @@ const connectionForm = ref({
30
30
  })
31
31
 
32
32
  const isRunning = ref(false)
33
+ // True only while waitForServerAndRefresh is polling through a dev-server
34
+ // restart. useMigrations().status reads as null on every failed poll in
35
+ // that window (see its own doc comment), which would otherwise leave the
36
+ // page blank below the title — the toasts alone are easy to miss. The
37
+ // template shows a persistent banner instead, keyed on this flag, so the
38
+ // page always explains what is happening instead of going quiet.
39
+ const isReconnecting = ref(false)
33
40
  const lastRun = ref<Awaited<ReturnType<typeof runMigrations>> | null>(null)
34
41
  // Kept only in this page's own memory, only to show the manual .env step if
35
42
  // persisting fails — see EnvPersistWarning.vue. Never sent anywhere new.
@@ -118,8 +125,12 @@ async function applyMigrations(useForm: boolean) {
118
125
  color: 'info',
119
126
  })
120
127
 
128
+ isReconnecting.value = true
129
+
121
130
  const recovered = await waitForServerAndRefresh()
122
131
 
132
+ isReconnecting.value = false
133
+
123
134
  toast.add(
124
135
  recovered
125
136
  ? {
@@ -163,7 +174,19 @@ async function applyMigrations(useForm: boolean) {
163
174
  <AdminView>
164
175
  <h1 class="text-3xl font-bold lg:text-4xl">Migrations</h1>
165
176
 
166
- <div v-if="fetchStatus === 'pending'" class="flex items-center gap-x-2">
177
+ <UAlert
178
+ v-if="isReconnecting"
179
+ color="info"
180
+ variant="outline"
181
+ icon="lucide:refresh-cw"
182
+ title="Dev server restarting"
183
+ description="Saving a new connection string restarts the dev server. Reconnecting — this page will update on its own once it's back."
184
+ />
185
+
186
+ <div
187
+ v-else-if="fetchStatus === 'pending'"
188
+ class="flex items-center gap-x-2"
189
+ >
167
190
  <Icon name="svg-spinners:ring-resize" />
168
191
  <span>Loading migration status…</span>
169
192
  </div>
@@ -340,5 +363,25 @@ async function applyMigrations(useForm: boolean) {
340
363
  </div>
341
364
  </UCard>
342
365
  </template>
366
+
367
+ <UAlert
368
+ v-else
369
+ color="error"
370
+ variant="outline"
371
+ icon="lucide:circle-x"
372
+ title="Could not load migration status"
373
+ description="This isn't the dev-server-restart case above — something else went wrong reading migration status. Check the server console, then try again."
374
+ >
375
+ <template #actions>
376
+ <UButton
377
+ color="error"
378
+ variant="outline"
379
+ icon="lucide:refresh-cw"
380
+ @click="refresh()"
381
+ >
382
+ Retry
383
+ </UButton>
384
+ </template>
385
+ </UAlert>
343
386
  </AdminView>
344
387
  </template>
@@ -8,19 +8,11 @@ const route = useRoute()
8
8
  const { isLoggedIn, logout, allowedUnauthenticatedPaths } = await useAuth()
9
9
  const toast = useToast()
10
10
 
11
- const { load: loadPermissions, clear: clearPermissions } = usePlutoPermissions()
12
-
13
- watch(
14
- isLoggedIn,
15
- (loggedIn) => {
16
- if (loggedIn) {
17
- loadPermissions()
18
- } else {
19
- clearPermissions()
20
- }
21
- },
22
- { immediate: true }
23
- )
11
+ // Loading/clearing permissions on login/logout lives in
12
+ // app/plugins/pluto-permissions-sync.ts now, not here — that plugin runs
13
+ // on every page, not just this one, so a signed-in admin browsing a public
14
+ // page directly (no prior visit to /admin/** this session) still gets
15
+ // their capabilities loaded.
24
16
 
25
17
  const visibility = useDocumentVisibility()
26
18
 
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Loads (or clears) the current user's capabilities whenever their session
3
+ * appears or disappears — app-wide, not page-scoped.
4
+ *
5
+ * This used to live as a `watch` inside `app/pages/admin.vue`. That only
6
+ * ran while `admin.vue` was the mounted page, so a signed-in admin loading
7
+ * a public page directly (a fresh navigation or a page reload, with no
8
+ * earlier visit to `/admin/**` in the same app instance) never triggered a
9
+ * load at all — `usePlutoPermissions().granted` stayed empty, and every
10
+ * `can()` check for that admin read as `false` on that page, even though
11
+ * the same admin's capability-gated UI worked correctly inside `/admin/**`.
12
+ * `NavbarAdminActions.vue`'s "Edit product" quick-link (shown while
13
+ * viewing a live `/product/**` page) is exactly the kind of capability-
14
+ * gated UI that lives outside `/admin/**` by design, so it needs this to
15
+ * fire on every page, not just admin ones.
16
+ *
17
+ * `useSupabaseSession()` (not `useAuth()`) on purpose: it is the same
18
+ * primitive `useAuth()`'s own `isLoggedIn` is built from
19
+ * (`computed(() => !!supabaseSession.value)`), and it is genuinely
20
+ * app-wide reactive state already (managed by `@nuxtjs/supabase`'s own
21
+ * plugin), so reading it directly here needs no extra composable surface.
22
+ */
23
+ export default defineNuxtPlugin(() => {
24
+ const session = useSupabaseSession()
25
+ const { load, clear } = usePlutoPermissions()
26
+
27
+ watch(
28
+ session,
29
+ (current) => {
30
+ if (current) {
31
+ load()
32
+ } else {
33
+ clear()
34
+ }
35
+ },
36
+ { immediate: true }
37
+ )
38
+ })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@plutocms/supabase",
3
3
  "type": "module",
4
- "version": "0.7.1",
4
+ "version": "0.7.2",
5
5
  "trustedDependencies": [
6
6
  "@parcel/watcher",
7
7
  "@plutocms/pluto",
@@ -4,25 +4,42 @@ import { serverSupabaseClient, serverSupabaseUser } from '#supabase/server'
4
4
  /**
5
5
  * Guards a server route so only an admin can call it.
6
6
  *
7
- * Calls `public.is_admin()` directly — deliberately NOT through
8
- * `requireCapability`/`public.has_capability()`. `has_capability()` is
9
- * defined by `004_roles_and_capabilities.sql`; a site that has upgraded
10
- * this package but not yet applied that migration has no such function in
11
- * its database, and every `requireCapability` call would throw. If
12
- * `requireAdmin` routed through it too, that would 403 the one route
13
- * (`/api/migrations/*`) an admin needs to actually apply the migration —
14
- * a deadlock with no escape through the UI. `public.is_admin()` has existed
15
- * since `002_admin_hardening.sql` and is only ever `create or replace`d,
16
- * never dropped, so it is always safe to call regardless of which layer
17
- * migrations have been applied. This is also why the migrations routes
18
- * call `requireAdmin` specifically, and not a named capability like
19
- * `system:migrate` migrations are a bootstrapping concern and must never
20
- * depend on the capability system migrations themselves create.
7
+ * Queries `public.profiles.is_admin` directly — deliberately NOT
8
+ * `public.is_admin()` or `public.has_capability()`. Both of those are
9
+ * defined by migrations (`002_admin_hardening.sql`,
10
+ * `004_roles_and_capabilities.sql` respectively); a genuinely fresh site
11
+ * that has never applied any migration beyond the original baseline
12
+ * schema an expected, normal state, not a broken one — has neither
13
+ * function. `/api/migrations/*` (the only routes that call `requireAdmin`
14
+ * today) are the one place this actually bites: they are the routes that
15
+ * apply a pending migration, so they must work with zero migrations
16
+ * applied, not just with `002`+ already in place. An earlier version of
17
+ * this function called `public.is_admin()` on the theory that it "always
18
+ * exists" true for a site that has applied at least `002`, false for a
19
+ * fresh one that has not, which is exactly the case that matters here.
20
+ * `profiles.is_admin` (the column) is part of the original baseline
21
+ * schema itself, before any of this project's layered migrations existed,
22
+ * so querying it directly has no migration dependency at all.
23
+ *
24
+ * The trade-off: a user granted the `admin` role only through
25
+ * `user_roles` (see `permissions-storage/SKILL.md`'s "Granting a role by
26
+ * hand"), with `profiles.is_admin` left `false`, cannot pass this check —
27
+ * only RLS and `has_capability()`-gated routes recognize a role-only
28
+ * admin. That is an acceptable, narrow limitation: `user_roles` cannot
29
+ * exist before `004` has been applied, so a role-only admin can only
30
+ * exist on a site that has already applied every current migration, at
31
+ * which point there is nothing left for that admin to need
32
+ * `/admin/migrations` for. If a future migration ever needs applying by
33
+ * a role-only admin, grant them `profiles.is_admin = true` too.
34
+ *
35
+ * `serverSupabaseUser` (from `@nuxtjs/supabase`, backed by
36
+ * `client.auth.getClaims()`) returns decoded JWT claims, not a Supabase
37
+ * `User` row — the claims object has no `id` field, only `sub`. Querying
38
+ * `.eq('id', user.id)` silently matches zero rows and looks exactly like
39
+ * "not an admin" even for a real admin. Always read `user.sub`.
21
40
  *
22
41
  * Throws a 401 if there is no logged-in user, or a 403 if the user is not
23
- * an admin. Returns the user's claims on success. See capability-guard.ts
24
- * for the same user.sub vs user.id note — this function has the same
25
- * shape but never reads either field either.
42
+ * an admin. Returns the user's claims on success.
26
43
  */
27
44
  export async function requireAdmin(event: H3Event) {
28
45
  const user = await serverSupabaseUser(event)
@@ -32,9 +49,21 @@ export async function requireAdmin(event: H3Event) {
32
49
  }
33
50
 
34
51
  const client = await serverSupabaseClient<Database>(event)
35
- const { data, error } = await client.rpc('is_admin')
52
+ const { data: profile, error } = await client
53
+ .from('profiles')
54
+ .select('is_admin')
55
+ .eq('id', user.sub)
56
+ .single()
57
+
58
+ if (error) {
59
+ // Logged, never returned: the 403 below must not tell an untrusted
60
+ // caller whether it hit a real error (a missing column, a connection
61
+ // problem) or a genuine non-admin. Server logs are where that
62
+ // distinction has to live instead.
63
+ console.error('requireAdmin: profiles lookup failed:', error.message)
64
+ }
36
65
 
37
- if (error || data !== true) {
66
+ if (error || !profile?.is_admin) {
38
67
  throw createError({ statusCode: 403, statusMessage: 'Your account is not an admin.' })
39
68
  }
40
69
 
Binary file
Binary file