@plutocms/supabase 0.18.1 → 0.20.0

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.
@@ -0,0 +1,480 @@
1
+ <script setup lang="ts">
2
+ // The Users page (`/admin/users`, the Users item of the admin menu). It lists
3
+ // the staff (users with a role). An admin invites users, changes their roles,
4
+ // resends an invite, copies an invite link and revokes access. It needs
5
+ // `users:read`: without it, the page shows an alert and loads nothing. The API
6
+ // routes do their own check. See the users-and-roles skill.
7
+ import type { DropdownMenuItem, TableColumn } from '@nuxt/ui'
8
+ import type {
9
+ AccessCapabilities,
10
+ AccessRole,
11
+ AccessUser,
12
+ } from '../../../shared/types/access'
13
+ import {
14
+ isAdminUser,
15
+ roleLabelOf,
16
+ userDisplayName,
17
+ userStatus,
18
+ } from '../../utils/access-ui'
19
+
20
+ // The modals and the slideover load on demand. They stay out of the page chunk.
21
+ const InviteModal = defineAsyncComponent(
22
+ () => import('../../components/access/InviteModal.vue')
23
+ )
24
+ const EditRolesSlideover = defineAsyncComponent(
25
+ () => import('../../components/access/EditRolesSlideover.vue')
26
+ )
27
+ const RevokeModal = defineAsyncComponent(
28
+ () => import('../../components/access/RevokeModal.vue')
29
+ )
30
+ const LinkModal = defineAsyncComponent(
31
+ () => import('../../components/access/LinkModal.vue')
32
+ )
33
+
34
+ const { t, d, locale } = usePlutoI18n()
35
+ const toast = useToast()
36
+ const overlay = useOverlay()
37
+ const errorText = usePlutoErrorText()
38
+ const permissions = usePlutoPermissions()
39
+ const auth = usePlutoAuth()
40
+
41
+ useHead({
42
+ title: () => t('supabase.access.users.title'),
43
+ })
44
+
45
+ const allowed = computed(() => permissions.can('users:read'))
46
+
47
+ const users = ref<AccessUser[]>([])
48
+ const roles = ref<AccessRole[]>([])
49
+ const status = ref<'loading' | 'ready' | 'error'>('loading')
50
+ const loadError = ref('')
51
+ const capabilities = ref<AccessCapabilities | null>(null)
52
+
53
+ const isAdmin = computed(() => permissions.isAdmin.value)
54
+ const selfId = computed(() => auth.user.value?.id ?? '')
55
+
56
+ async function load() {
57
+ // Keep the table on screen while it reloads after a change.
58
+ if (status.value !== 'ready') {
59
+ status.value = 'loading'
60
+ }
61
+
62
+ try {
63
+ const [usersResponse, rolesResponse] = await Promise.all([
64
+ $fetch<{ users: AccessUser[] }>('/api/_pluto/supabase/access/users'),
65
+ $fetch<{ roles: AccessRole[] }>('/api/_pluto/supabase/access/roles'),
66
+ ])
67
+
68
+ users.value = usersResponse.users
69
+ roles.value = rolesResponse.roles
70
+ status.value = 'ready'
71
+ } catch (error) {
72
+ loadError.value = errorText(error)
73
+ status.value = 'error'
74
+ }
75
+ }
76
+
77
+ async function loadCapabilities() {
78
+ try {
79
+ capabilities.value = await $fetch<AccessCapabilities>(
80
+ '/api/_pluto/supabase/access/capabilities'
81
+ )
82
+ } catch {
83
+ // The invite works without it. Only the "Copy invite link" action hides.
84
+ capabilities.value = null
85
+ }
86
+ }
87
+
88
+ watch(
89
+ allowed,
90
+ (ok) => {
91
+ if (ok) {
92
+ load()
93
+ }
94
+ },
95
+ { immediate: true }
96
+ )
97
+
98
+ // Only an admin can read this, so ask after the permissions are known.
99
+ watch(
100
+ isAdmin,
101
+ (admin) => {
102
+ if (admin && !capabilities.value) {
103
+ void loadCapabilities()
104
+ }
105
+ },
106
+ { immediate: true }
107
+ )
108
+
109
+ const columns = computed<TableColumn<AccessUser>[]>(() => [
110
+ { id: 'user', header: t('supabase.access.users.columns.user') },
111
+ { id: 'roles', header: t('supabase.access.users.columns.roles') },
112
+ { id: 'status', header: t('supabase.access.users.columns.status') },
113
+ { id: 'lastSignIn', header: t('supabase.access.users.columns.lastSignIn') },
114
+ { id: 'actions', header: '' },
115
+ ])
116
+
117
+ function dateText(
118
+ value: string | null,
119
+ options: Intl.DateTimeFormatOptions
120
+ ): string {
121
+ return value ? d(new Date(value), options) : ''
122
+ }
123
+
124
+ // --- Invite ---
125
+ const inviteMounted = ref(false)
126
+ const inviteOpen = ref(false)
127
+
128
+ function openInvite() {
129
+ inviteMounted.value = true
130
+ inviteOpen.value = true
131
+ }
132
+
133
+ // --- Edit roles ---
134
+ const editMounted = ref(false)
135
+ const editOpen = ref(false)
136
+ const editUser = ref<AccessUser | null>(null)
137
+
138
+ function openEdit(user: AccessUser) {
139
+ editUser.value = user
140
+ editMounted.value = true
141
+ editOpen.value = true
142
+ }
143
+
144
+ // --- Revoke ---
145
+ async function openRevoke(user: AccessUser) {
146
+ const modal = overlay.create(RevokeModal)
147
+ const revoked = await modal.open({ user, self: user.id === selfId.value })
148
+
149
+ if (revoked) {
150
+ await load()
151
+ }
152
+ }
153
+
154
+ // --- Invite link ---
155
+ async function showLink(link: string) {
156
+ await overlay.create(LinkModal).open({ link })
157
+ }
158
+
159
+ async function copyLink(user: AccessUser) {
160
+ if (!user.email) {
161
+ return
162
+ }
163
+
164
+ try {
165
+ const result = await $fetch<{ link: string | null }>(
166
+ '/api/_pluto/supabase/access/users/invite-link',
167
+ {
168
+ method: 'POST',
169
+ body: { email: user.email, roleKeys: user.roles },
170
+ }
171
+ )
172
+
173
+ // A confirmed account gets no link: it signs in with its own password.
174
+ if (!result.link) {
175
+ toast.add({
176
+ title: t('supabase.access.toasts.noLinkConfirmed'),
177
+ color: 'info',
178
+ icon: 'lucide:info',
179
+ })
180
+ return
181
+ }
182
+
183
+ await showLink(result.link)
184
+ } catch (error) {
185
+ toast.add({
186
+ title: t('supabase.access.toasts.failed'),
187
+ description: errorText(error),
188
+ color: 'error',
189
+ icon: 'lucide:circle-x',
190
+ })
191
+ }
192
+ }
193
+
194
+ // --- Resend ---
195
+ async function resend(user: AccessUser) {
196
+ try {
197
+ const result = await $fetch<{ emailSent?: boolean; link?: string }>(
198
+ `/api/_pluto/supabase/access/users/${user.id}/resend`,
199
+ { method: 'POST', body: { locale: locale.value } }
200
+ )
201
+
202
+ if (result.emailSent === false && result.link) {
203
+ toast.add({
204
+ title: t('supabase.access.toasts.resentNoEmail'),
205
+ color: 'warning',
206
+ icon: 'lucide:triangle-alert',
207
+ })
208
+ await showLink(result.link)
209
+
210
+ return
211
+ }
212
+
213
+ toast.add({
214
+ title: t('supabase.access.toasts.resent', { email: user.email ?? '' }),
215
+ color: 'success',
216
+ icon: 'lucide:circle-check',
217
+ })
218
+ } catch (error) {
219
+ toast.add({
220
+ title: t('supabase.access.toasts.failed'),
221
+ description: errorText(error),
222
+ color: 'error',
223
+ icon: 'lucide:circle-x',
224
+ })
225
+ }
226
+ }
227
+
228
+ function menuItems(user: AccessUser): DropdownMenuItem[][] {
229
+ const pending = userStatus(user) === 'invited'
230
+ const main: DropdownMenuItem[] = [
231
+ {
232
+ label: t('supabase.access.users.menu.editRoles'),
233
+ icon: 'lucide:shield',
234
+ onSelect: () => openEdit(user),
235
+ },
236
+ ]
237
+
238
+ if (pending) {
239
+ main.push({
240
+ label: t('supabase.access.users.menu.resend'),
241
+ icon: 'lucide:send',
242
+ onSelect: () => resend(user),
243
+ })
244
+
245
+ if (capabilities.value?.serviceKey && user.roles.length > 0) {
246
+ main.push({
247
+ label: t('supabase.access.users.menu.copyLink'),
248
+ icon: 'lucide:link',
249
+ onSelect: () => copyLink(user),
250
+ })
251
+ }
252
+ }
253
+
254
+ return [
255
+ main,
256
+ [
257
+ {
258
+ label: t('supabase.access.users.menu.revoke'),
259
+ icon: 'lucide:user-x',
260
+ color: 'error',
261
+ onSelect: () => openRevoke(user),
262
+ },
263
+ ],
264
+ ]
265
+ }
266
+ </script>
267
+
268
+ <template>
269
+ <main>
270
+ <AdminView>
271
+ <!-- The section (the Users item of the menu) and its tabs, like Settings. -->
272
+ <h1 class="text-3xl font-bold lg:text-4xl">
273
+ {{ t('supabase.access.users.title') }}
274
+ </h1>
275
+
276
+ <PlutoAdminNavTabs />
277
+
278
+ <div class="flex flex-wrap items-start justify-between gap-4">
279
+ <div>
280
+ <h2 class="text-xl font-semibold">
281
+ {{ t('supabase.access.users.title') }}
282
+ </h2>
283
+ <p class="text-muted text-sm">
284
+ {{ t('supabase.access.users.description') }}
285
+ </p>
286
+ </div>
287
+
288
+ <UButton v-if="isAdmin" icon="lucide:user-plus" @click="openInvite">
289
+ {{ t('supabase.access.users.invite') }}
290
+ </UButton>
291
+ </div>
292
+
293
+ <UAlert
294
+ v-if="!allowed"
295
+ :title="t('pluto.settings.noPermission.title')"
296
+ :description="
297
+ t('pluto.settings.noPermission.description', {
298
+ page: t('supabase.access.users.title'),
299
+ })
300
+ "
301
+ icon="lucide:lock"
302
+ color="warning"
303
+ />
304
+
305
+ <!-- Loading -->
306
+ <div
307
+ v-else-if="status === 'loading'"
308
+ class="flex flex-col gap-y-3"
309
+ aria-busy="true"
310
+ >
311
+ <USkeleton v-for="row in 4" :key="row" class="h-12 w-full" />
312
+ </div>
313
+
314
+ <!-- Error -->
315
+ <UAlert
316
+ v-else-if="status === 'error'"
317
+ :title="t('supabase.access.users.error.title')"
318
+ :description="loadError"
319
+ :actions="[
320
+ {
321
+ label: t('pluto.common.tryAgain'),
322
+ color: 'neutral',
323
+ variant: 'outline',
324
+ onClick: load,
325
+ },
326
+ ]"
327
+ color="error"
328
+ variant="subtle"
329
+ icon="lucide:circle-x"
330
+ />
331
+
332
+ <!-- Empty -->
333
+ <UEmpty
334
+ v-else-if="users.length === 0"
335
+ :title="t('supabase.access.users.empty.title')"
336
+ :description="t('supabase.access.users.empty.description')"
337
+ :actions="
338
+ isAdmin
339
+ ? [
340
+ {
341
+ label: t('supabase.access.users.invite'),
342
+ icon: 'lucide:user-plus',
343
+ onClick: openInvite,
344
+ },
345
+ ]
346
+ : []
347
+ "
348
+ icon="lucide:users"
349
+ />
350
+
351
+ <!-- The list -->
352
+ <UTable
353
+ v-else
354
+ :data="users"
355
+ :columns="columns"
356
+ class="border-default rounded-lg border"
357
+ >
358
+ <template #user-cell="{ row }">
359
+ <div class="flex min-w-0 flex-col">
360
+ <span class="flex items-center gap-2">
361
+ <span class="text-highlighted truncate font-medium">{{
362
+ userDisplayName(row.original)
363
+ }}</span>
364
+ <UBadge
365
+ v-if="row.original.id === selfId"
366
+ :label="t('supabase.access.users.you')"
367
+ color="primary"
368
+ variant="subtle"
369
+ size="sm"
370
+ />
371
+ </span>
372
+ <span
373
+ v-if="row.original.email"
374
+ class="text-muted truncate text-sm"
375
+ >{{ row.original.email }}</span
376
+ >
377
+ </div>
378
+ </template>
379
+
380
+ <template #roles-cell="{ row }">
381
+ <div class="flex flex-wrap gap-1">
382
+ <UBadge
383
+ v-if="isAdminUser(row.original)"
384
+ :label="t('supabase.access.users.administrator')"
385
+ color="primary"
386
+ variant="subtle"
387
+ />
388
+ <template v-else>
389
+ <UBadge
390
+ v-for="key in row.original.roles"
391
+ :key="key"
392
+ :label="roleLabelOf(roles, key)"
393
+ color="neutral"
394
+ variant="subtle"
395
+ />
396
+ </template>
397
+ </div>
398
+ </template>
399
+
400
+ <template #status-cell="{ row }">
401
+ <div class="flex flex-col items-start gap-1">
402
+ <UBadge
403
+ v-if="userStatus(row.original) === 'active'"
404
+ :label="t('supabase.access.users.status.active')"
405
+ color="success"
406
+ variant="subtle"
407
+ />
408
+ <template v-else>
409
+ <UBadge
410
+ :label="t('supabase.access.users.status.invited')"
411
+ color="warning"
412
+ variant="subtle"
413
+ />
414
+ <span v-if="row.original.invited_at" class="text-muted text-xs">
415
+ {{
416
+ t('supabase.access.users.status.invitedOn', {
417
+ date: dateText(row.original.invited_at, {
418
+ dateStyle: 'medium',
419
+ }),
420
+ })
421
+ }}
422
+ </span>
423
+ </template>
424
+ </div>
425
+ </template>
426
+
427
+ <template #lastSignIn-cell="{ row }">
428
+ <span v-if="row.original.last_sign_in_at" class="text-sm">
429
+ {{
430
+ dateText(row.original.last_sign_in_at, {
431
+ dateStyle: 'medium',
432
+ timeStyle: 'short',
433
+ })
434
+ }}
435
+ </span>
436
+ <span v-else class="text-muted text-sm">{{
437
+ t('supabase.access.users.neverSignedIn')
438
+ }}</span>
439
+ </template>
440
+
441
+ <template #actions-cell="{ row }">
442
+ <div v-if="isAdmin" class="flex justify-end">
443
+ <UDropdownMenu
444
+ :items="menuItems(row.original)"
445
+ :content="{ align: 'end' }"
446
+ >
447
+ <UButton
448
+ :aria-label="
449
+ t('supabase.access.users.actionsLabel', {
450
+ name: userDisplayName(row.original),
451
+ })
452
+ "
453
+ icon="lucide:ellipsis"
454
+ color="neutral"
455
+ variant="ghost"
456
+ />
457
+ </UDropdownMenu>
458
+ </div>
459
+ </template>
460
+ </UTable>
461
+
462
+ <!-- Lazy: the chunk loads on the first open. -->
463
+ <InviteModal
464
+ v-if="inviteMounted"
465
+ v-model:open="inviteOpen"
466
+ :roles="roles"
467
+ :capabilities="capabilities"
468
+ @done="load"
469
+ />
470
+ <EditRolesSlideover
471
+ v-if="editMounted"
472
+ v-model:open="editOpen"
473
+ :user="editUser"
474
+ :roles="roles"
475
+ :self="editUser?.id === selfId"
476
+ @saved="load"
477
+ />
478
+ </AdminView>
479
+ </main>
480
+ </template>
@@ -163,25 +163,33 @@ export default defineNuxtPlugin(() => {
163
163
  component: SettingsGeneralPanel,
164
164
  },
165
165
  ],
166
- // The Users and Roles tabs of Settings. The page files are in
167
- // app/pages/admin/settings/. Users shows for `users:read` (an admin always
168
- // has it). Roles is admin only: '*' is the admin-only capability.
169
- settingsPages: [
166
+ // The Users item of the admin menu, between Media (900) and Settings
167
+ // (1000). A click opens the users. Its children are Users and Roles. The
168
+ // item shows for `users:read` (an admin always has it). Roles is admin
169
+ // only: without it, the item is a plain link to the users. The pages are
170
+ // app/pages/admin/users.vue and roles.vue.
171
+ nav: [
170
172
  {
171
173
  id: 'users',
172
- order: 10,
174
+ order: 950,
173
175
  label: 'supabase.access.users.title',
174
176
  icon: 'lucide:users',
175
- to: '/admin/settings/users',
176
- capability: 'users:read',
177
- },
178
- {
179
- id: 'roles',
180
- order: 20,
181
- label: 'supabase.access.roles.title',
182
- icon: 'lucide:shield',
183
- to: '/admin/settings/roles',
184
- capability: '*',
177
+ to: '/admin/users',
178
+ match: ['/admin/roles'],
179
+ enabled: () => usePlutoPermissions().can('users:read'),
180
+ children: [
181
+ {
182
+ label: 'supabase.access.users.title',
183
+ to: '/admin/users',
184
+ icon: 'lucide:users',
185
+ },
186
+ {
187
+ label: 'supabase.access.roles.title',
188
+ to: '/admin/roles',
189
+ icon: 'lucide:shield',
190
+ enabled: () => usePlutoPermissions().isAdmin.value,
191
+ },
192
+ ],
185
193
  },
186
194
  ],
187
195
  // A guide on the Email tab: the sign-in emails come from Supabase.
package/nuxt.config.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import type { PlutoMigrationFile } from './shared/types/migrations'
2
2
 
3
- // Point these at a local checkout (e.g. `../pluto/packages/core`, `../utils`) to test
4
- // unpublished changes; unset, they resolve to the published npm packages.
3
+ // Point this at a local checkout (e.g. `../pluto/packages/core`) to test
4
+ // unpublished changes; unset, it resolves to the published npm package.
5
5
  const coreLayer = process.env.PLUTO_CORE_PATH || '@plutocms/core'
6
- const utilsLayer = process.env.PLUTO_UTILS_PATH || '@plutocms/utils'
7
6
 
8
7
  // The host of the Supabase project, at build time. `@nuxt/image` may only read
9
8
  // remote images of the hosts in `image.domains` (the media files are in Storage).
@@ -16,7 +15,7 @@ function supabaseHost(): string {
16
15
  }
17
16
 
18
17
  export default defineNuxtConfig({
19
- extends: [[coreLayer, { install: true }], utilsLayer],
18
+ extends: [[coreLayer, { install: true }]],
20
19
 
21
20
  // No @nuxt/eslint: a module of a layer runs in every app that extends it. The repo
22
21
  // lints with @nuxt/eslint-config (eslint.config.mjs).
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@plutocms/supabase",
3
3
  "type": "module",
4
- "version": "0.18.1",
4
+ "version": "0.20.0",
5
5
  "description": "Supabase backend layer for Pluto",
6
6
  "author": "Victor Ribeiro",
7
7
  "homepage": "https://github.com/plutocms/supabase#readme",
@@ -29,8 +29,7 @@
29
29
  "dependencies": {
30
30
  "@nuxt/ui": "^4.12.0",
31
31
  "@nuxtjs/supabase": "^2.1.0",
32
- "@plutocms/core": ">=0.38.0 <1.0.0",
33
- "@plutocms/utils": ">=0.3.0 <1.0.0",
32
+ "@plutocms/core": ">=0.41.0 <1.0.0",
34
33
  "@supabase/supabase-js": "^2.117.3",
35
34
  "postgres": "^3.4.9",
36
35
  "supabase": "^2.121.0",