@reflagged/shell 1.1.0 → 1.3.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.
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@reflagged/shell",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Shared app shell for Reflagged services — OIDC auth, SSO, app switcher",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "files": [
8
8
  "src",
9
+ "!src/**/*.test.ts",
9
10
  "tsconfig.json",
10
11
  "README.md"
11
12
  ],
@@ -25,16 +26,19 @@
25
26
  "./api/oidc-callback": "./src/lib/api/oidc-callback.ts",
26
27
  "./api/oidc-signout": "./src/lib/api/oidc-signout.ts",
27
28
  "./api/shell-info": "./src/lib/api/shell-info.ts",
28
- "./package.json": "./package.json"
29
+ "./package.json": "./package.json",
30
+ "./components/ServiceIcon": "./src/components/ServiceIcon.tsx"
29
31
  },
30
32
  "scripts": {
31
- "typecheck": "tsc --noEmit"
33
+ "typecheck": "tsc --noEmit",
34
+ "test": "vitest run"
32
35
  },
33
36
  "dependencies": {
34
37
  "jose": "^5.10.0",
35
38
  "oauth4webapi": "^3.6.0"
36
39
  },
37
40
  "peerDependencies": {
41
+ "lucide-react": ">=0.400.0",
38
42
  "next": "^15.4.0",
39
43
  "payload": "^3.79.0",
40
44
  "react": "^19.2.0",
@@ -43,10 +47,17 @@
43
47
  "devDependencies": {
44
48
  "@types/react": "^19.2.0",
45
49
  "@types/react-dom": "^19.2.0",
50
+ "lucide-react": "^0.500.0",
46
51
  "next": "^15.4.0",
47
52
  "payload": "^3.79.0",
48
53
  "react": "^19.2.0",
49
54
  "react-dom": "^19.2.0",
50
- "typescript": "^5.7.0"
55
+ "typescript": "^5.7.0",
56
+ "vitest": "^3.2.0"
57
+ },
58
+ "peerDependenciesMeta": {
59
+ "lucide-react": {
60
+ "optional": false
61
+ }
51
62
  }
52
63
  }
@@ -3,6 +3,7 @@
3
3
  import { useEffect, useRef, useState } from 'react'
4
4
  import { createPortal } from 'react-dom'
5
5
  import { loadAppConfig } from '../config'
6
+ import { ServiceIcon } from './ServiceIcon'
6
7
 
7
8
  type Booking = {
8
9
  id: string
@@ -11,6 +12,12 @@ type Booking = {
11
12
  status: string
12
13
  url: string | null
13
14
  iconUrl: string | null
15
+ /**
16
+ * Lucide name of the service icon, from rflgd-base. Optional: an older
17
+ * platform does not send it, and then the initial stands in — the way it
18
+ * always did.
19
+ */
20
+ icon?: string | null
14
21
  }
15
22
 
16
23
  type Org = {
@@ -391,7 +398,7 @@ export function BrandSwitcher() {
391
398
  fontWeight: 600,
392
399
  }}
393
400
  >
394
- {b.label.slice(0, 1).toUpperCase()}
401
+ <ServiceIcon name={b.icon} label={b.label} />
395
402
  </div>
396
403
  <div style={{ flex: 1, minWidth: 0 }}>
397
404
  <div style={{ fontWeight: 500, fontSize: 13.5 }}>{b.label}</div>
@@ -0,0 +1,66 @@
1
+ 'use client'
2
+
3
+ import {
4
+ AudioLines,
5
+ Bot,
6
+ Box,
7
+ Brain,
8
+ Contact,
9
+ Factory,
10
+ ListMusic,
11
+ MailPlus,
12
+ MessageSquareText,
13
+ PenTool,
14
+ ShieldCheck,
15
+ Users,
16
+ type LucideIcon,
17
+ } from 'lucide-react'
18
+
19
+ /**
20
+ * The icon a service shows in the app switcher.
21
+ *
22
+ * Which icon a service gets is rflgd-base's decision — it sends the lucide
23
+ * name in `/api/shell/me` (`serviceIconName()`), so switcher, launchpad and
24
+ * catalog show the same mark. This file only holds the components to render
25
+ * those names with.
26
+ *
27
+ * Listed one by one rather than looked up in lucide's `icons` object, and that
28
+ * is the whole reason this file exists: an index lookup defeats tree shaking,
29
+ * and all ~1500 icons would land in the bundle of every app using this
30
+ * package. Ten repositories depend on it.
31
+ *
32
+ * The cost is a list that can fall behind the platform's. It degrades to
33
+ * exactly today's behaviour — a service whose name is not here shows its
34
+ * initial, as every service did before. Nothing breaks, it just looks
35
+ * unfinished, and adding the icon here is a one-line fix.
36
+ */
37
+ const ICONS: Record<string, LucideIcon> = {
38
+ AudioLines,
39
+ Bot,
40
+ Box,
41
+ Brain,
42
+ Contact,
43
+ Factory,
44
+ ListMusic,
45
+ MailPlus,
46
+ MessageSquareText,
47
+ PenTool,
48
+ ShieldCheck,
49
+ Users,
50
+ }
51
+
52
+ export function ServiceIcon({
53
+ name,
54
+ label,
55
+ size = 18,
56
+ }: {
57
+ /** Lucide name from the platform, e.g. 'Brain'. */
58
+ name?: string | null
59
+ /** Fallback when the name is missing or not bundled here. */
60
+ label: string
61
+ size?: number
62
+ }) {
63
+ const Icon = name ? ICONS[name] : undefined
64
+ if (!Icon) return <>{label.slice(0, 1).toUpperCase()}</>
65
+ return <Icon size={size} strokeWidth={1.75} aria-hidden />
66
+ }
@@ -5,46 +5,112 @@ import { loadAppConfig } from '../../config'
5
5
  import { OIDC_COOKIE } from './oidc-config'
6
6
  import { verifySessionCookie } from './oidc-cookie'
7
7
 
8
- export const nextauthStrategy: AuthStrategy = {
9
- name: 'rflgd-oidc',
10
- authenticate: async ({ payload, headers }) => {
11
- const secret = process.env.AUTH_SECRET
12
- if (!secret) return { user: null }
13
-
14
- const cookieHeader = headers.get('cookie') ?? ''
15
- const cookieStart = `${OIDC_COOKIE}=`
16
- const segment = cookieHeader
17
- .split(';')
18
- .map((c) => c.trim())
19
- .find((c) => c.startsWith(cookieStart))
20
- if (!segment) return { user: null }
21
-
22
- const token = segment.slice(cookieStart.length)
23
- const session = await verifySessionCookie(secret, token)
24
- if (!session?.email) return { user: null }
25
-
26
- const existing = await payload.find({
27
- collection: 'users',
28
- where: { email: { equals: session.email } },
29
- limit: 1,
30
- overrideAccess: true,
31
- })
32
-
33
- let user = existing.docs[0]
34
- if (!user) {
35
- const config = loadAppConfig()
36
- const isFirst = (await payload.count({ collection: 'users', overrideAccess: true })).totalDocs === 0
37
- user = await payload.create({
8
+ /** What the platform knows about a person signing in for the first time. */
9
+ export type NewUserContext = {
10
+ /** Role in the current workspace ('admin' | 'member'), null without claims. */
11
+ orgRole: string | null
12
+ /** Role on the platform itself, null without claims. */
13
+ platformRole: string | null
14
+ email: string
15
+ /** True when this account is the first in the instance. */
16
+ isFirstUser: boolean
17
+ }
18
+
19
+ /** Maps the platform's view of a person onto one of this app's own roles. */
20
+ export type RoleForNewUser = (ctx: NewUserContext) => string
21
+
22
+ /**
23
+ * The role a newly created account receives.
24
+ *
25
+ * Without a callback this is exactly what it always was: the first account
26
+ * administers, everyone after it gets RFLGD_DEFAULT_USER_ROLE. With a
27
+ * callback the decision belongs entirely to the app, `isFirstUser` included —
28
+ * which is why it is in the context. An app that forgets that branch can end
29
+ * up with an instance that has no administrator.
30
+ *
31
+ * The return value is not checked. Which roles exist is the app's knowledge;
32
+ * an unknown value is rejected by Payload against the field configuration and
33
+ * the account is not created. That is the same path on which this package's
34
+ * own default 'user' once locked instances out.
35
+ */
36
+ export function resolveNewUserRole(
37
+ ctx: NewUserContext,
38
+ roleForNewUser?: RoleForNewUser,
39
+ ): string {
40
+ if (roleForNewUser) return roleForNewUser(ctx)
41
+ return ctx.isFirstUser ? 'admin' : loadAppConfig().defaultRole
42
+ }
43
+
44
+ /**
45
+ * Payload auth strategy for the platform's single sign-on.
46
+ *
47
+ * Creates an account on first sign-in when none exists for the address. The
48
+ * role it gets is `resolveNewUserRole`'s decision — see there.
49
+ */
50
+ export function createOidcStrategy(options?: {
51
+ roleForNewUser?: RoleForNewUser
52
+ }): AuthStrategy {
53
+ return {
54
+ name: 'rflgd-oidc',
55
+ authenticate: async ({ payload, headers }) => {
56
+ const secret = process.env.AUTH_SECRET
57
+ if (!secret) return { user: null }
58
+
59
+ const cookieHeader = headers.get('cookie') ?? ''
60
+ const cookieStart = `${OIDC_COOKIE}=`
61
+ const segment = cookieHeader
62
+ .split(';')
63
+ .map((c) => c.trim())
64
+ .find((c) => c.startsWith(cookieStart))
65
+ if (!segment) return { user: null }
66
+
67
+ const token = segment.slice(cookieStart.length)
68
+ const session = await verifySessionCookie(secret, token)
69
+ if (!session?.email) return { user: null }
70
+
71
+ const existing = await payload.find({
38
72
  collection: 'users',
39
- data: {
40
- email: session.email,
41
- password: randomBytes(32).toString('hex'),
42
- role: isFirst ? 'admin' : config.defaultRole,
43
- },
73
+ where: { email: { equals: session.email } },
74
+ limit: 1,
44
75
  overrideAccess: true,
45
76
  })
46
- }
47
77
 
48
- return { user: { ...user, collection: 'users' } }
49
- },
78
+ let user = existing.docs[0]
79
+ if (!user) {
80
+ const isFirstUser =
81
+ (await payload.count({ collection: 'users', overrideAccess: true })).totalDocs === 0
82
+ const role = resolveNewUserRole(
83
+ {
84
+ orgRole: session.orgRole ?? null,
85
+ platformRole: session.platformRole ?? null,
86
+ email: session.email,
87
+ isFirstUser,
88
+ },
89
+ options?.roleForNewUser,
90
+ )
91
+ user = await payload.create({
92
+ collection: 'users',
93
+ data: {
94
+ email: session.email,
95
+ password: randomBytes(32).toString('hex'),
96
+ // `role` comes from the environment or the app and is therefore a
97
+ // `string`. The host application narrows `users.role` to the
98
+ // options of its own collection — this package cannot know that
99
+ // type. The value is checked by Payload against the field
100
+ // configuration on write; an unknown one is rejected there.
101
+ role: role as never,
102
+ },
103
+ overrideAccess: true,
104
+ })
105
+ }
106
+
107
+ return { user: { ...user, collection: 'users' } }
108
+ },
109
+ }
50
110
  }
111
+
112
+ /**
113
+ * The strategy without options — the export every consumer used before this
114
+ * package learned about role translation. Kept so nothing has to change.
115
+ */
116
+ export const nextauthStrategy: AuthStrategy = createOidcStrategy()