@nexys/user-management-web 0.1.3 → 0.1.4

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@nexys/user-management-web",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Modern, slick React + Tailwind + shadcn UI kit for the Nexys user-management service. Built on @nexys/user-management-client.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@hookform/resolvers": "^3.10.0",
36
- "@nexys/user-management-client": "^0.1.3",
36
+ "@nexys/user-management-client": "^0.1.4",
37
37
  "@radix-ui/react-avatar": "^1.1.4",
38
38
  "@radix-ui/react-dialog": "^1.1.14",
39
39
  "@radix-ui/react-dropdown-menu": "^2.1.7",
@@ -0,0 +1,149 @@
1
+ import { type OAuthAppRow } from "@nexys/user-management-client";
2
+ import { AlertCircle, AppWindow, ShieldOff } from "lucide-react";
3
+ import { useState } from "react";
4
+
5
+ import { useUmClient } from "../../context/auth-context.js";
6
+ import { useCall } from "../../hooks/use-call.js";
7
+ import { errorMessage } from "../../lib/error-message.js";
8
+ import { formatRelative } from "../../lib/utils.js";
9
+ import { EmptyState } from "../primitives/empty-state.js";
10
+ import { Alert, AlertDescription } from "../ui/alert.js";
11
+ import { Button } from "../ui/button.js";
12
+ import {
13
+ Card,
14
+ CardContent,
15
+ CardDescription,
16
+ CardHeader,
17
+ CardTitle,
18
+ } from "../ui/card.js";
19
+ import {
20
+ Table,
21
+ TableBody,
22
+ TableCell,
23
+ TableHead,
24
+ TableHeader,
25
+ TableRow,
26
+ } from "../ui/table.js";
27
+
28
+ /** The resource as a reader knows it: host and path, without the scheme. */
29
+ const resourceLabel = (resource: string): string => {
30
+ try {
31
+ const url = new URL(resource);
32
+ return `${url.host}${url.pathname === "/" ? "" : url.pathname}`;
33
+ } catch {
34
+ return resource;
35
+ }
36
+ };
37
+
38
+ /**
39
+ * Connected apps: the OAuth clients (CLIs, mobile apps, MCP clients) the
40
+ * caller allowed into one of their tenants. Disconnecting one revokes its
41
+ * grant: its refresh token stops working at once, and its last access token
42
+ * within 15 minutes (access tokens are not checked against the grant).
43
+ */
44
+ export const ConnectedAppsCard = () => {
45
+ const client = useUmClient();
46
+ const { data, loading, error, refresh } = useCall("listOAuthApps");
47
+ const tenants = useCall("myTenants");
48
+ const [busy, setBusy] = useState<string | null>(null);
49
+ const [actionError, setActionError] = useState<string | null>(null);
50
+
51
+ const apps = data?.apps ?? [];
52
+ const tenantName = (organizationId: string): string =>
53
+ tenants.data?.memberships.find((m) => m.organizationId === organizationId)
54
+ ?.organizationName ?? organizationId;
55
+
56
+ const disconnect = async (app: OAuthAppRow) => {
57
+ setBusy(app.id);
58
+ setActionError(null);
59
+ const res = await client.revokeOAuthApp({ id: app.id });
60
+ setBusy(null);
61
+ if (res.ok) {
62
+ await refresh();
63
+ } else {
64
+ setActionError(errorMessage(res.error, "Couldn't disconnect the app."));
65
+ }
66
+ };
67
+
68
+ return (
69
+ <Card>
70
+ <CardHeader>
71
+ <CardTitle className="flex items-center gap-2">
72
+ <AppWindow className="size-4 text-muted-foreground" />
73
+ Connected apps
74
+ </CardTitle>
75
+ <CardDescription>
76
+ Apps you signed in to with OAuth, such as a CLI, a mobile app or an
77
+ MCP client. Each one acts as you in the tenant you chose. Disconnect
78
+ an app to sign it out: it stops working within 15 minutes.
79
+ </CardDescription>
80
+ </CardHeader>
81
+ <CardContent className="grid gap-4">
82
+ {error ? (
83
+ <Alert variant="destructive">
84
+ <AlertCircle />
85
+ <AlertDescription>{errorMessage(error)}</AlertDescription>
86
+ </Alert>
87
+ ) : null}
88
+ {actionError ? (
89
+ <Alert variant="destructive">
90
+ <AlertCircle />
91
+ <AlertDescription>{actionError}</AlertDescription>
92
+ </Alert>
93
+ ) : null}
94
+
95
+ {loading ? null : apps.length === 0 ? (
96
+ <EmptyState
97
+ icon={AppWindow}
98
+ title="No connected apps"
99
+ description="Apps you sign in to with OAuth show up here."
100
+ />
101
+ ) : (
102
+ <div className="overflow-hidden rounded-lg border">
103
+ <Table>
104
+ <TableHeader>
105
+ <TableRow>
106
+ <TableHead>App</TableHead>
107
+ <TableHead>Tenant</TableHead>
108
+ <TableHead>Access to</TableHead>
109
+ <TableHead>Connected</TableHead>
110
+ <TableHead>Last used</TableHead>
111
+ <TableHead className="text-right">Actions</TableHead>
112
+ </TableRow>
113
+ </TableHeader>
114
+ <TableBody>
115
+ {apps.map((app) => (
116
+ <TableRow key={app.id}>
117
+ <TableCell className="font-medium">{app.clientName}</TableCell>
118
+ <TableCell>{tenantName(app.organizationId)}</TableCell>
119
+ <TableCell className="font-mono text-xs text-muted-foreground">
120
+ {resourceLabel(app.resource)}
121
+ </TableCell>
122
+ <TableCell className="text-sm text-muted-foreground">
123
+ {formatRelative(app.grantedAt)}
124
+ </TableCell>
125
+ <TableCell className="text-sm text-muted-foreground">
126
+ {formatRelative(app.lastUsedAt)}
127
+ </TableCell>
128
+ <TableCell className="text-right">
129
+ <Button
130
+ variant="ghost"
131
+ size="sm"
132
+ onClick={() => disconnect(app)}
133
+ loading={busy === app.id}
134
+ aria-label={`Disconnect ${app.clientName}`}
135
+ >
136
+ <ShieldOff className="text-destructive" />
137
+ Disconnect
138
+ </Button>
139
+ </TableCell>
140
+ </TableRow>
141
+ ))}
142
+ </TableBody>
143
+ </Table>
144
+ </div>
145
+ )}
146
+ </CardContent>
147
+ </Card>
148
+ );
149
+ };
@@ -1,9 +1,11 @@
1
1
 
2
+ import { useAuth } from "../../context/auth-context.js";
2
3
  import { useCall } from "../../hooks/use-call.js";
3
4
  import { Tabs, TabsContent, TabsList, TabsTrigger } from "../ui/tabs.js";
4
5
  import { ProjectConsents } from "./project-consents.js";
5
6
  import { ProjectEmailTemplates } from "./project-email-templates.js";
6
7
  import { ProjectNotices } from "./project-notices.js";
8
+ import { ProjectOAuth } from "./project-oauth.js";
7
9
  import { ProjectServiceAccounts } from "./project-service-accounts.js";
8
10
  import { ProjectSettings } from "./project-settings.js";
9
11
  import { ProjectTenants } from "./project-tenants.js";
@@ -25,6 +27,9 @@ export const ProjectDetailTabs = ({
25
27
  }: ProjectDetailTabsProps) => {
26
28
  // The email tab opens on the product's default locale.
27
29
  const { data } = useCall("getProject", { projectId });
30
+ // The OAuth settings API is superadmin only.
31
+ const { hasUserRole } = useAuth();
32
+ const showOAuth = canManage && hasUserRole("superadmin");
28
33
 
29
34
  return (
30
35
  <Tabs defaultValue="tenants">
@@ -36,6 +41,7 @@ export const ProjectDetailTabs = ({
36
41
  {canManage ? (
37
42
  <TabsTrigger value="service-accounts">Service accounts</TabsTrigger>
38
43
  ) : null}
44
+ {showOAuth ? <TabsTrigger value="oauth">OAuth</TabsTrigger> : null}
39
45
  <TabsTrigger value="settings">Settings</TabsTrigger>
40
46
  </TabsList>
41
47
  <TabsContent value="tenants" className="mt-4">
@@ -59,6 +65,11 @@ export const ProjectDetailTabs = ({
59
65
  <ProjectServiceAccounts projectId={projectId} />
60
66
  </TabsContent>
61
67
  ) : null}
68
+ {showOAuth ? (
69
+ <TabsContent value="oauth" className="mt-4">
70
+ <ProjectOAuth projectId={projectId} />
71
+ </TabsContent>
72
+ ) : null}
62
73
  <TabsContent value="settings" className="mt-4">
63
74
  <ProjectSettings
64
75
  projectId={projectId}
@@ -0,0 +1,256 @@
1
+ import { AlertCircle, Check, Save, Trash2 } from "lucide-react";
2
+ import { useState, type FormEvent } from "react";
3
+
4
+ import { useUmClient } from "../../context/auth-context.js";
5
+ import { useCall } from "../../hooks/use-call.js";
6
+ import { errorMessage } from "../../lib/error-message.js";
7
+ import { Alert, AlertDescription } from "../ui/alert.js";
8
+ import { Badge } from "../ui/badge.js";
9
+ import { Button } from "../ui/button.js";
10
+ import {
11
+ Card,
12
+ CardContent,
13
+ CardDescription,
14
+ CardHeader,
15
+ CardTitle,
16
+ } from "../ui/card.js";
17
+ import {
18
+ Dialog,
19
+ DialogClose,
20
+ DialogContent,
21
+ DialogDescription,
22
+ DialogFooter,
23
+ DialogHeader,
24
+ DialogTitle,
25
+ DialogTrigger,
26
+ } from "../ui/dialog.js";
27
+ import { Field } from "../ui/field.js";
28
+ import { Input } from "../ui/input.js";
29
+ import { Skeleton } from "../ui/skeleton.js";
30
+ import { Textarea } from "../ui/textarea.js";
31
+
32
+ export interface ProjectOAuthProps {
33
+ projectId: string;
34
+ }
35
+
36
+ /**
37
+ * The product's OAuth settings: where this service sends the browser to
38
+ * approve a client (the product's consent page), and the resources it
39
+ * issues tokens for. Superadmin only, like the API behind it.
40
+ */
41
+ export const ProjectOAuth = ({ projectId }: ProjectOAuthProps) => {
42
+ const { data, loading, error, refresh } = useCall("getProjectOAuth", { projectId });
43
+
44
+ if (error) {
45
+ return (
46
+ <Alert variant="destructive">
47
+ <AlertCircle />
48
+ <AlertDescription>{errorMessage(error)}</AlertDescription>
49
+ </Alert>
50
+ );
51
+ }
52
+ if (loading && data === undefined) {
53
+ return <Skeleton className="h-64 w-full" />;
54
+ }
55
+
56
+ return (
57
+ <div className="grid gap-6">
58
+ <Card>
59
+ <CardHeader>
60
+ <CardTitle className="flex items-center gap-2">
61
+ OAuth sign-in
62
+ {data ? <Badge>On</Badge> : <Badge variant="outline">Off</Badge>}
63
+ </CardTitle>
64
+ <CardDescription>
65
+ Lets this product&apos;s apps and MCP clients sign in with OAuth
66
+ (authorization code with PKCE). A client registers with this
67
+ service, the user approves it on the product&apos;s consent page
68
+ and picks a tenant, and the client gets a short-lived access token
69
+ for one of the resources below and a refresh token.
70
+ </CardDescription>
71
+ </CardHeader>
72
+ <CardContent>
73
+ <OAuthForm
74
+ key={data?.updatedAt ?? "off"}
75
+ projectId={projectId}
76
+ consentUrl={data?.consentUrl ?? ""}
77
+ resources={data?.resources ?? []}
78
+ enabled={!!data}
79
+ onSaved={refresh}
80
+ />
81
+ </CardContent>
82
+ </Card>
83
+ {data ? (
84
+ <TurnOffCard projectId={projectId} onTurnedOff={refresh} />
85
+ ) : null}
86
+ </div>
87
+ );
88
+ };
89
+
90
+ const OAuthForm = ({
91
+ projectId,
92
+ consentUrl: initialConsentUrl,
93
+ resources: initialResources,
94
+ enabled,
95
+ onSaved,
96
+ }: {
97
+ projectId: string;
98
+ consentUrl: string;
99
+ resources: string[];
100
+ enabled: boolean;
101
+ onSaved: () => void;
102
+ }) => {
103
+ const client = useUmClient();
104
+ const [consentUrl, setConsentUrl] = useState(initialConsentUrl);
105
+ const [text, setText] = useState(initialResources.join("\n"));
106
+ const [saving, setSaving] = useState(false);
107
+ const [saved, setSaved] = useState(false);
108
+ const [error, setError] = useState<string | null>(null);
109
+
110
+ const resources = parseLines(text);
111
+ const dirty =
112
+ consentUrl.trim() !== initialConsentUrl ||
113
+ resources.join("\n") !== initialResources.join("\n");
114
+ const complete = consentUrl.trim() !== "" && resources.length > 0;
115
+
116
+ const submit = async (e: FormEvent) => {
117
+ e.preventDefault();
118
+ setSaving(true);
119
+ setError(null);
120
+ setSaved(false);
121
+ const res = await client.setProjectOAuth({
122
+ projectId,
123
+ consentUrl: consentUrl.trim(),
124
+ resources,
125
+ });
126
+ setSaving(false);
127
+ if (res.ok) {
128
+ setSaved(true);
129
+ setTimeout(() => setSaved(false), 2000);
130
+ onSaved();
131
+ } else {
132
+ setError(errorMessage(res.error, "Couldn't save the OAuth settings."));
133
+ }
134
+ };
135
+
136
+ return (
137
+ <form onSubmit={submit} className="grid gap-4">
138
+ {error ? (
139
+ <Alert variant="destructive">
140
+ <AlertCircle />
141
+ <AlertDescription>{error}</AlertDescription>
142
+ </Alert>
143
+ ) : null}
144
+ <Field
145
+ label="Consent page"
146
+ htmlFor="project-oauth-consent-url"
147
+ description="The product's own page that asks the user to approve a client. This service sends the browser there with ?request=<id>. https, or http on localhost."
148
+ >
149
+ <Input
150
+ id="project-oauth-consent-url"
151
+ value={consentUrl}
152
+ onChange={(e) => setConsentUrl(e.target.value)}
153
+ placeholder="https://app.example.com/oauth/consent"
154
+ className="font-mono"
155
+ />
156
+ </Field>
157
+ <Field
158
+ label="Resources"
159
+ htmlFor="project-oauth-resources"
160
+ description="One URL per line, 1 to 10 (RFC 8707). Each access token is issued for one of them, and the product's server accepts only tokens for its own. A resource belongs to one product."
161
+ >
162
+ <Textarea
163
+ id="project-oauth-resources"
164
+ value={text}
165
+ onChange={(e) => setText(e.target.value)}
166
+ rows={3}
167
+ placeholder={"https://app.example.com/api\nhttps://app.example.com/mcp"}
168
+ className="font-mono"
169
+ />
170
+ </Field>
171
+ <div className="flex items-center gap-3">
172
+ <Button type="submit" loading={saving} disabled={!dirty || !complete}>
173
+ {saved ? <Check /> : <Save />}
174
+ {saved ? "Saved" : enabled ? "Save changes" : "Turn on OAuth"}
175
+ </Button>
176
+ </div>
177
+ </form>
178
+ );
179
+ };
180
+
181
+ const TurnOffCard = ({
182
+ projectId,
183
+ onTurnedOff,
184
+ }: {
185
+ projectId: string;
186
+ onTurnedOff: () => void;
187
+ }) => {
188
+ const client = useUmClient();
189
+ const [busy, setBusy] = useState(false);
190
+ const [error, setError] = useState<string | null>(null);
191
+
192
+ const turnOff = async () => {
193
+ setBusy(true);
194
+ setError(null);
195
+ const res = await client.deleteProjectOAuth({ projectId });
196
+ setBusy(false);
197
+ if (res.ok) {
198
+ onTurnedOff();
199
+ } else {
200
+ setError(errorMessage(res.error, "Couldn't turn off OAuth."));
201
+ }
202
+ };
203
+
204
+ return (
205
+ <Card className="border-destructive/50">
206
+ <CardHeader>
207
+ <CardTitle className="text-destructive">Turn off OAuth</CardTitle>
208
+ <CardDescription>
209
+ New sign-ins are refused. Apps already signed in keep working
210
+ until their users sign out or revoke them.
211
+ </CardDescription>
212
+ </CardHeader>
213
+ <CardContent>
214
+ {error ? (
215
+ <Alert variant="destructive" className="mb-4">
216
+ <AlertCircle />
217
+ <AlertDescription>{error}</AlertDescription>
218
+ </Alert>
219
+ ) : null}
220
+ <Dialog>
221
+ <DialogTrigger asChild>
222
+ <Button variant="destructive">
223
+ <Trash2 /> Turn off OAuth
224
+ </Button>
225
+ </DialogTrigger>
226
+ <DialogContent>
227
+ <DialogHeader>
228
+ <DialogTitle>Turn off OAuth?</DialogTitle>
229
+ <DialogDescription>
230
+ The consent page and resources are removed, and apps (CLI,
231
+ mobile app, MCP clients) cannot start a new sign-in to this
232
+ product until you turn it back on. Apps already signed in keep
233
+ working until their users sign out or revoke them.
234
+ </DialogDescription>
235
+ </DialogHeader>
236
+ <DialogFooter>
237
+ <DialogClose asChild>
238
+ <Button variant="outline">Cancel</Button>
239
+ </DialogClose>
240
+ <Button variant="destructive" onClick={turnOff} loading={busy}>
241
+ Turn off
242
+ </Button>
243
+ </DialogFooter>
244
+ </DialogContent>
245
+ </Dialog>
246
+ </CardContent>
247
+ </Card>
248
+ );
249
+ };
250
+
251
+ /** One entry per line, trimmed, blanks dropped. */
252
+ const parseLines = (text: string): string[] =>
253
+ text
254
+ .split("\n")
255
+ .map((l) => l.trim())
256
+ .filter((l) => l.length > 0);
package/src/index.ts CHANGED
@@ -111,6 +111,7 @@ export { SignUpForm, type SignUpFormProps } from "./components/auth/sign-up-form
111
111
 
112
112
  // profile
113
113
  export { ApiTokensCard } from "./components/profile/api-tokens-card.js";
114
+ export { ConnectedAppsCard } from "./components/profile/connected-apps-card.js";
114
115
  export { ChangePasswordForm } from "./components/profile/change-password-form.js";
115
116
  export { PasskeysCard } from "./components/profile/passkeys-card.js";
116
117
  export { ProfileCard } from "./components/profile/profile-card.js";
@@ -140,6 +141,7 @@ export { AuditLogTable } from "./components/audit/audit-log-table.js";
140
141
  // projects (products)
141
142
  export { ProjectCreateDialog, type ProjectCreateDialogProps } from "./components/projects/project-create-dialog.js";
142
143
  export { ProjectDetailTabs, type ProjectDetailTabsProps } from "./components/projects/project-detail-tabs.js";
144
+ export { ProjectOAuth, type ProjectOAuthProps } from "./components/projects/project-oauth.js";
143
145
  export { ProjectServiceAccounts, type ProjectServiceAccountsProps } from "./components/projects/project-service-accounts.js";
144
146
  export { ProjectSettings, type ProjectSettingsProps } from "./components/projects/project-settings.js";
145
147
  export { ProjectTenants, type ProjectTenantsProps } from "./components/projects/project-tenants.js";