@nexys/user-management-web 0.0.26
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/README.md +61 -0
- package/components.json +21 -0
- package/package.json +66 -0
- package/src/components/audit/audit-log-table.test.tsx +91 -0
- package/src/components/audit/audit-log-table.tsx +226 -0
- package/src/components/auth/accept-invitation-card.tsx +100 -0
- package/src/components/auth/forgot-password-form.tsx +92 -0
- package/src/components/auth/reset-password-form.tsx +143 -0
- package/src/components/auth/sign-in-form.test.tsx +140 -0
- package/src/components/auth/sign-in-form.tsx +509 -0
- package/src/components/auth/sign-up-form.tsx +123 -0
- package/src/components/layout/app-shell.tsx +189 -0
- package/src/components/layout/auth-shell.tsx +28 -0
- package/src/components/layout/theme-toggle.tsx +55 -0
- package/src/components/layout/user-menu.tsx +84 -0
- package/src/components/primitives/empty-state.tsx +40 -0
- package/src/components/primitives/page-header.tsx +27 -0
- package/src/components/primitives/spinner.tsx +28 -0
- package/src/components/profile/api-tokens-card.tsx +235 -0
- package/src/components/profile/change-password-form.tsx +158 -0
- package/src/components/profile/passkeys-card.tsx +180 -0
- package/src/components/profile/profile-card.tsx +33 -0
- package/src/components/profile/two-factor-card.tsx +226 -0
- package/src/components/projects/project-consents.tsx +572 -0
- package/src/components/projects/project-create-dialog.tsx +209 -0
- package/src/components/projects/project-detail-tabs.tsx +63 -0
- package/src/components/projects/project-email-templates.tsx +282 -0
- package/src/components/projects/project-notices.tsx +458 -0
- package/src/components/projects/project-settings.tsx +1133 -0
- package/src/components/projects/project-tenants.tsx +107 -0
- package/src/components/projects/projects-table.test.tsx +95 -0
- package/src/components/projects/projects-table.tsx +240 -0
- package/src/components/roles/role-badge.tsx +91 -0
- package/src/components/roles/role-select.tsx +76 -0
- package/src/components/tenants/tenant-create-dialog.tsx +163 -0
- package/src/components/tenants/tenant-detail-tabs.tsx +51 -0
- package/src/components/tenants/tenant-members.tsx +578 -0
- package/src/components/tenants/tenant-roles.test.tsx +91 -0
- package/src/components/tenants/tenant-roles.tsx +414 -0
- package/src/components/tenants/tenant-settings.tsx +385 -0
- package/src/components/tenants/tenants-table.test.tsx +71 -0
- package/src/components/tenants/tenants-table.tsx +245 -0
- package/src/components/ui/alert.tsx +52 -0
- package/src/components/ui/avatar.tsx +46 -0
- package/src/components/ui/badge.tsx +32 -0
- package/src/components/ui/button.tsx +64 -0
- package/src/components/ui/card.tsx +65 -0
- package/src/components/ui/dialog.tsx +99 -0
- package/src/components/ui/dropdown-menu.tsx +97 -0
- package/src/components/ui/field.tsx +44 -0
- package/src/components/ui/input.tsx +23 -0
- package/src/components/ui/label.tsx +19 -0
- package/src/components/ui/select.tsx +87 -0
- package/src/components/ui/separator.tsx +22 -0
- package/src/components/ui/skeleton.tsx +13 -0
- package/src/components/ui/switch.tsx +29 -0
- package/src/components/ui/table.tsx +87 -0
- package/src/components/ui/tabs.tsx +51 -0
- package/src/components/ui/textarea.tsx +22 -0
- package/src/components/ui/tooltip.tsx +27 -0
- package/src/components/users/user-create-dialog.tsx +267 -0
- package/src/components/users/user-delete-dialog.tsx +88 -0
- package/src/components/users/user-detail.tsx +843 -0
- package/src/components/users/users-table.test.tsx +97 -0
- package/src/components/users/users-table.tsx +382 -0
- package/src/context/auth-context.tsx +124 -0
- package/src/context/theme-context.test.tsx +78 -0
- package/src/context/theme-context.tsx +110 -0
- package/src/hooks/use-call.ts +64 -0
- package/src/index.ts +159 -0
- package/src/lib/error-message.ts +16 -0
- package/src/lib/utils.test.ts +61 -0
- package/src/lib/utils.ts +79 -0
- package/src/lib/webauthn.ts +36 -0
- package/src/styles.css +107 -0
- package/src/test-setup.ts +5 -0
- package/tailwind.preset.cjs +15 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
useEffect,
|
|
6
|
+
useMemo,
|
|
7
|
+
useState,
|
|
8
|
+
type ReactNode,
|
|
9
|
+
} from "react";
|
|
10
|
+
|
|
11
|
+
export type Theme = "light" | "dark" | "system";
|
|
12
|
+
|
|
13
|
+
export interface ThemeContextValue {
|
|
14
|
+
/** The user's preference, including "system". */
|
|
15
|
+
theme: Theme;
|
|
16
|
+
/** What is actually applied right now. */
|
|
17
|
+
resolvedTheme: "light" | "dark";
|
|
18
|
+
setTheme: (theme: Theme) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Default localStorage key — keep in sync with any pre-paint script in index.html. */
|
|
22
|
+
export const THEME_STORAGE_KEY = "um.theme";
|
|
23
|
+
|
|
24
|
+
const Ctx = createContext<ThemeContextValue | null>(null);
|
|
25
|
+
|
|
26
|
+
const isTheme = (v: unknown): v is Theme =>
|
|
27
|
+
v === "light" || v === "dark" || v === "system";
|
|
28
|
+
|
|
29
|
+
const prefersDarkQuery = () =>
|
|
30
|
+
typeof window !== "undefined" && window.matchMedia
|
|
31
|
+
? window.matchMedia("(prefers-color-scheme: dark)")
|
|
32
|
+
: null;
|
|
33
|
+
|
|
34
|
+
const readStored = (storageKey: string): Theme | null => {
|
|
35
|
+
try {
|
|
36
|
+
const v = window.localStorage.getItem(storageKey);
|
|
37
|
+
return isTheme(v) ? v : null;
|
|
38
|
+
} catch {
|
|
39
|
+
return null; // storage unavailable (SSR, privacy mode)
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export interface ThemeProviderProps {
|
|
44
|
+
children: ReactNode;
|
|
45
|
+
/** Used when no preference is stored yet. Default "system". */
|
|
46
|
+
defaultTheme?: Theme;
|
|
47
|
+
/** localStorage key for persistence. Default {@link THEME_STORAGE_KEY}. */
|
|
48
|
+
storageKey?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Applies the `.dark` class from styles.css to <html> and persists the
|
|
53
|
+
* user's choice. "system" follows `prefers-color-scheme` live.
|
|
54
|
+
*/
|
|
55
|
+
export const ThemeProvider = ({
|
|
56
|
+
children,
|
|
57
|
+
defaultTheme = "system",
|
|
58
|
+
storageKey = THEME_STORAGE_KEY,
|
|
59
|
+
}: ThemeProviderProps) => {
|
|
60
|
+
const [theme, setThemeState] = useState<Theme>(
|
|
61
|
+
() => readStored(storageKey) ?? defaultTheme
|
|
62
|
+
);
|
|
63
|
+
const [systemDark, setSystemDark] = useState<boolean>(
|
|
64
|
+
() => prefersDarkQuery()?.matches ?? false
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
// Follow OS-level changes while in "system" mode.
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
const mql = prefersDarkQuery();
|
|
70
|
+
if (!mql) return;
|
|
71
|
+
const onChange = (e: MediaQueryListEvent) => setSystemDark(e.matches);
|
|
72
|
+
mql.addEventListener("change", onChange);
|
|
73
|
+
return () => mql.removeEventListener("change", onChange);
|
|
74
|
+
}, []);
|
|
75
|
+
|
|
76
|
+
const resolvedTheme: "light" | "dark" =
|
|
77
|
+
theme === "system" ? (systemDark ? "dark" : "light") : theme;
|
|
78
|
+
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
const root = document.documentElement;
|
|
81
|
+
root.classList.toggle("dark", resolvedTheme === "dark");
|
|
82
|
+
// Native form controls, scrollbars, etc.
|
|
83
|
+
root.style.colorScheme = resolvedTheme;
|
|
84
|
+
}, [resolvedTheme]);
|
|
85
|
+
|
|
86
|
+
const setTheme = useCallback(
|
|
87
|
+
(next: Theme) => {
|
|
88
|
+
setThemeState(next);
|
|
89
|
+
try {
|
|
90
|
+
window.localStorage.setItem(storageKey, next);
|
|
91
|
+
} catch {
|
|
92
|
+
// storage unavailable — theme still applies for this session
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
[storageKey]
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const value = useMemo<ThemeContextValue>(
|
|
99
|
+
() => ({ theme, resolvedTheme, setTheme }),
|
|
100
|
+
[theme, resolvedTheme, setTheme]
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
return <Ctx.Provider value={value}>{children}</Ctx.Provider>;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export const useTheme = (): ThemeContextValue => {
|
|
107
|
+
const v = useContext(Ctx);
|
|
108
|
+
if (!v) throw new Error("useTheme must be used inside <ThemeProvider>");
|
|
109
|
+
return v;
|
|
110
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ApiError,
|
|
3
|
+
CallArgs,
|
|
4
|
+
NetworkError,
|
|
5
|
+
ProcedureInput,
|
|
6
|
+
ProcedureName,
|
|
7
|
+
ProcedureOutput,
|
|
8
|
+
} from "@nexys/user-management-client";
|
|
9
|
+
import { useCallback, useEffect, useState } from "react";
|
|
10
|
+
|
|
11
|
+
import { useUmClient } from "../context/auth-context.js";
|
|
12
|
+
|
|
13
|
+
export interface CallResource<T, E> {
|
|
14
|
+
data: T | undefined;
|
|
15
|
+
error: E | null;
|
|
16
|
+
loading: boolean;
|
|
17
|
+
refresh: () => Promise<void>;
|
|
18
|
+
/** Mutate local state without round-tripping the server — useful for optimistic updates. */
|
|
19
|
+
setData: (next: T | undefined) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* React wrapper around one API procedure. Fetches on mount, exposes
|
|
24
|
+
* { data, error, loading, refresh, setData } and re-fetches when
|
|
25
|
+
* `input` changes (shallow stringify equality).
|
|
26
|
+
*
|
|
27
|
+
* const tokens = useCall("listApiTokens");
|
|
28
|
+
* const users = useCall("adminListUsers", { status: "active" });
|
|
29
|
+
*/
|
|
30
|
+
export const useCall = <K extends ProcedureName>(
|
|
31
|
+
procedure: K,
|
|
32
|
+
...args: CallArgs<ProcedureInput<K>>
|
|
33
|
+
): CallResource<ProcedureOutput<K>, ApiError | NetworkError> => {
|
|
34
|
+
const client = useUmClient();
|
|
35
|
+
const [data, setData] = useState<ProcedureOutput<K> | undefined>(undefined);
|
|
36
|
+
const [error, setError] = useState<ApiError | NetworkError | null>(null);
|
|
37
|
+
const [loading, setLoading] = useState(true);
|
|
38
|
+
|
|
39
|
+
// Stable equality on a JSON snapshot of input so callers don't have
|
|
40
|
+
// to memoise the object literal at the call site.
|
|
41
|
+
const key = JSON.stringify(args[0] ?? null);
|
|
42
|
+
|
|
43
|
+
const refresh = useCallback(async () => {
|
|
44
|
+
setLoading(true);
|
|
45
|
+
setError(null);
|
|
46
|
+
const call = client[procedure] as (
|
|
47
|
+
...a: CallArgs<ProcedureInput<K>>
|
|
48
|
+
) => ReturnType<(typeof client)[K]>;
|
|
49
|
+
const res = await call(...args);
|
|
50
|
+
if (res.ok) {
|
|
51
|
+
setData(res.data as ProcedureOutput<K>);
|
|
52
|
+
} else {
|
|
53
|
+
setError(res.error);
|
|
54
|
+
}
|
|
55
|
+
setLoading(false);
|
|
56
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
57
|
+
}, [client, procedure, key]);
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
void refresh();
|
|
61
|
+
}, [refresh]);
|
|
62
|
+
|
|
63
|
+
return { data, error, loading, refresh, setData };
|
|
64
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// Re-export the wire client surface — apps only depend on one package.
|
|
2
|
+
export * from "@nexys/user-management-client";
|
|
3
|
+
|
|
4
|
+
// Context & hooks
|
|
5
|
+
export {
|
|
6
|
+
AuthProvider,
|
|
7
|
+
useAuth,
|
|
8
|
+
useUmClient,
|
|
9
|
+
type AuthContextValue,
|
|
10
|
+
type AuthProviderProps,
|
|
11
|
+
type AuthSnapshot,
|
|
12
|
+
} from "./context/auth-context.js";
|
|
13
|
+
export { useCall, type CallResource } from "./hooks/use-call.js";
|
|
14
|
+
export {
|
|
15
|
+
THEME_STORAGE_KEY,
|
|
16
|
+
ThemeProvider,
|
|
17
|
+
useTheme,
|
|
18
|
+
type Theme,
|
|
19
|
+
type ThemeContextValue,
|
|
20
|
+
type ThemeProviderProps,
|
|
21
|
+
} from "./context/theme-context.js";
|
|
22
|
+
|
|
23
|
+
// Utilities
|
|
24
|
+
export { errorMessage } from "./lib/error-message.js";
|
|
25
|
+
export {
|
|
26
|
+
cn,
|
|
27
|
+
formatDateTime,
|
|
28
|
+
formatRelative,
|
|
29
|
+
initialsOf,
|
|
30
|
+
parseUserAgent,
|
|
31
|
+
} from "./lib/utils.js";
|
|
32
|
+
|
|
33
|
+
// shadcn primitives
|
|
34
|
+
export { Alert, AlertDescription, AlertTitle } from "./components/ui/alert.js";
|
|
35
|
+
export { Avatar, AvatarFallback, AvatarImage } from "./components/ui/avatar.js";
|
|
36
|
+
export { Badge, badgeVariants } from "./components/ui/badge.js";
|
|
37
|
+
export { Button, buttonVariants, type ButtonProps } from "./components/ui/button.js";
|
|
38
|
+
export {
|
|
39
|
+
Card,
|
|
40
|
+
CardContent,
|
|
41
|
+
CardDescription,
|
|
42
|
+
CardFooter,
|
|
43
|
+
CardHeader,
|
|
44
|
+
CardTitle,
|
|
45
|
+
} from "./components/ui/card.js";
|
|
46
|
+
export {
|
|
47
|
+
Dialog,
|
|
48
|
+
DialogClose,
|
|
49
|
+
DialogContent,
|
|
50
|
+
DialogDescription,
|
|
51
|
+
DialogFooter,
|
|
52
|
+
DialogHeader,
|
|
53
|
+
DialogOverlay,
|
|
54
|
+
DialogPortal,
|
|
55
|
+
DialogTitle,
|
|
56
|
+
DialogTrigger,
|
|
57
|
+
} from "./components/ui/dialog.js";
|
|
58
|
+
export {
|
|
59
|
+
DropdownMenu,
|
|
60
|
+
DropdownMenuCheckboxItem,
|
|
61
|
+
DropdownMenuContent,
|
|
62
|
+
DropdownMenuGroup,
|
|
63
|
+
DropdownMenuItem,
|
|
64
|
+
DropdownMenuLabel,
|
|
65
|
+
DropdownMenuPortal,
|
|
66
|
+
DropdownMenuSeparator,
|
|
67
|
+
DropdownMenuTrigger,
|
|
68
|
+
} from "./components/ui/dropdown-menu.js";
|
|
69
|
+
export { Field, type FieldProps } from "./components/ui/field.js";
|
|
70
|
+
export { Input } from "./components/ui/input.js";
|
|
71
|
+
export { Label } from "./components/ui/label.js";
|
|
72
|
+
export {
|
|
73
|
+
Select,
|
|
74
|
+
SelectContent,
|
|
75
|
+
SelectGroup,
|
|
76
|
+
SelectItem,
|
|
77
|
+
SelectTrigger,
|
|
78
|
+
SelectValue,
|
|
79
|
+
} from "./components/ui/select.js";
|
|
80
|
+
export { Separator } from "./components/ui/separator.js";
|
|
81
|
+
export { Skeleton } from "./components/ui/skeleton.js";
|
|
82
|
+
export { Switch } from "./components/ui/switch.js";
|
|
83
|
+
export {
|
|
84
|
+
Table,
|
|
85
|
+
TableBody,
|
|
86
|
+
TableCell,
|
|
87
|
+
TableEmpty,
|
|
88
|
+
TableHead,
|
|
89
|
+
TableHeader,
|
|
90
|
+
TableRow,
|
|
91
|
+
} from "./components/ui/table.js";
|
|
92
|
+
export { Tabs, TabsContent, TabsList, TabsTrigger } from "./components/ui/tabs.js";
|
|
93
|
+
export {
|
|
94
|
+
Tooltip,
|
|
95
|
+
TooltipContent,
|
|
96
|
+
TooltipProvider,
|
|
97
|
+
TooltipTrigger,
|
|
98
|
+
} from "./components/ui/tooltip.js";
|
|
99
|
+
|
|
100
|
+
// primitives
|
|
101
|
+
export { EmptyState, type EmptyStateProps } from "./components/primitives/empty-state.js";
|
|
102
|
+
export { PageHeader, type PageHeaderProps } from "./components/primitives/page-header.js";
|
|
103
|
+
export { FullPageSpinner, Spinner } from "./components/primitives/spinner.js";
|
|
104
|
+
|
|
105
|
+
// auth
|
|
106
|
+
export { AcceptInvitationCard, type AcceptInvitationCardProps } from "./components/auth/accept-invitation-card.js";
|
|
107
|
+
export { ForgotPasswordForm, type ForgotPasswordFormProps } from "./components/auth/forgot-password-form.js";
|
|
108
|
+
export { ResetPasswordForm, type ResetPasswordFormProps } from "./components/auth/reset-password-form.js";
|
|
109
|
+
export { SignInForm, type SignInFormProps } from "./components/auth/sign-in-form.js";
|
|
110
|
+
export { SignUpForm, type SignUpFormProps } from "./components/auth/sign-up-form.js";
|
|
111
|
+
|
|
112
|
+
// profile
|
|
113
|
+
export { ApiTokensCard } from "./components/profile/api-tokens-card.js";
|
|
114
|
+
export { ChangePasswordForm } from "./components/profile/change-password-form.js";
|
|
115
|
+
export { PasskeysCard } from "./components/profile/passkeys-card.js";
|
|
116
|
+
export { ProfileCard } from "./components/profile/profile-card.js";
|
|
117
|
+
export { TwoFactorCard } from "./components/profile/two-factor-card.js";
|
|
118
|
+
|
|
119
|
+
// webauthn browser glue
|
|
120
|
+
export { createPasskey, getPasskeyAssertion, webauthnSupported } from "./lib/webauthn.js";
|
|
121
|
+
|
|
122
|
+
// roles
|
|
123
|
+
export { StatusBadge, TenantRoleBadge, UserRoleBadge } from "./components/roles/role-badge.js";
|
|
124
|
+
export {
|
|
125
|
+
TenantRoleSelect,
|
|
126
|
+
UserRoleSelect,
|
|
127
|
+
type TenantRoleSelectProps,
|
|
128
|
+
type UserRoleSelectProps,
|
|
129
|
+
} from "./components/roles/role-select.js";
|
|
130
|
+
|
|
131
|
+
// users
|
|
132
|
+
export { UsersTable, type UsersTableProps } from "./components/users/users-table.js";
|
|
133
|
+
export { UserCreateDialog, type UserCreateDialogProps } from "./components/users/user-create-dialog.js";
|
|
134
|
+
export { UserDeleteDialog, type UserDeleteDialogProps } from "./components/users/user-delete-dialog.js";
|
|
135
|
+
export { UserDetailView, type UserDetailViewProps } from "./components/users/user-detail.js";
|
|
136
|
+
|
|
137
|
+
// audit
|
|
138
|
+
export { AuditLogTable } from "./components/audit/audit-log-table.js";
|
|
139
|
+
|
|
140
|
+
// projects (products)
|
|
141
|
+
export { ProjectCreateDialog, type ProjectCreateDialogProps } from "./components/projects/project-create-dialog.js";
|
|
142
|
+
export { ProjectDetailTabs, type ProjectDetailTabsProps } from "./components/projects/project-detail-tabs.js";
|
|
143
|
+
export { ProjectSettings, type ProjectSettingsProps } from "./components/projects/project-settings.js";
|
|
144
|
+
export { ProjectTenants, type ProjectTenantsProps } from "./components/projects/project-tenants.js";
|
|
145
|
+
export { ProjectsTable } from "./components/projects/projects-table.js";
|
|
146
|
+
|
|
147
|
+
// tenants
|
|
148
|
+
export { TenantCreateDialog, type TenantCreateDialogProps } from "./components/tenants/tenant-create-dialog.js";
|
|
149
|
+
export { TenantDetailTabs, type TenantDetailTabsProps } from "./components/tenants/tenant-detail-tabs.js";
|
|
150
|
+
export { TenantMembers, type TenantMembersProps } from "./components/tenants/tenant-members.js";
|
|
151
|
+
export { TenantRoles, type TenantRolesProps } from "./components/tenants/tenant-roles.js";
|
|
152
|
+
export { TenantSettings, type TenantSettingsProps } from "./components/tenants/tenant-settings.js";
|
|
153
|
+
export { TenantsTable } from "./components/tenants/tenants-table.js";
|
|
154
|
+
|
|
155
|
+
// layout
|
|
156
|
+
export { AppShell, type AppShellProps, type NavItem, type NavSection } from "./components/layout/app-shell.js";
|
|
157
|
+
export { AuthShell, type AuthShellProps } from "./components/layout/auth-shell.js";
|
|
158
|
+
export { UserMenu, type UserMenuItem, type UserMenuProps } from "./components/layout/user-menu.js";
|
|
159
|
+
export { ThemeToggle, type ThemeToggleProps } from "./components/layout/theme-toggle.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ApiError, NetworkError } from "@nexys/user-management-client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Discriminated unions from the client are { kind: "http" | "network" }.
|
|
5
|
+
* This pulls a human string out of either without callers having to
|
|
6
|
+
* pattern-match in every component.
|
|
7
|
+
*/
|
|
8
|
+
export const errorMessage = (
|
|
9
|
+
error: ApiError | NetworkError | (ApiError | NetworkError),
|
|
10
|
+
fallback = "Something went wrong."
|
|
11
|
+
): string => {
|
|
12
|
+
if (error.kind === "network") {
|
|
13
|
+
return "Network error — please check your connection and try again.";
|
|
14
|
+
}
|
|
15
|
+
return error.message || fallback;
|
|
16
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { cn, formatRelative, initialsOf, parseUserAgent } from "./utils.js";
|
|
4
|
+
|
|
5
|
+
describe("cn", () => {
|
|
6
|
+
test("merges conflicting tailwind classes, last wins", () => {
|
|
7
|
+
expect(cn("p-2 text-sm", "p-4")).toBe("text-sm p-4");
|
|
8
|
+
});
|
|
9
|
+
test("drops falsy entries", () => {
|
|
10
|
+
expect(cn("a", false && "b", null, undefined, "c")).toBe("a c");
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe("initialsOf", () => {
|
|
15
|
+
test("uses first + last name initials when available", () => {
|
|
16
|
+
expect(initialsOf("Ada Lovelace")).toBe("AL");
|
|
17
|
+
});
|
|
18
|
+
test("falls back to two-letter slice for a single token", () => {
|
|
19
|
+
expect(initialsOf("zoë")).toBe("ZO");
|
|
20
|
+
});
|
|
21
|
+
test("falls back to the email when name is empty", () => {
|
|
22
|
+
expect(initialsOf(null, "noah@example.com")).toBe("NO");
|
|
23
|
+
});
|
|
24
|
+
test("renders ? when nothing is known", () => {
|
|
25
|
+
expect(initialsOf()).toBe("?");
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("formatRelative", () => {
|
|
30
|
+
test("returns an em-dash for null/undefined/empty/garbage", () => {
|
|
31
|
+
expect(formatRelative(null)).toBe("—");
|
|
32
|
+
expect(formatRelative(undefined)).toBe("—");
|
|
33
|
+
expect(formatRelative("")).toBe("—");
|
|
34
|
+
expect(formatRelative("not-a-date")).toBe("—");
|
|
35
|
+
});
|
|
36
|
+
test("renders relative time against a fixed now", () => {
|
|
37
|
+
const now = new Date("2026-05-26T12:00:00Z");
|
|
38
|
+
const past = new Date("2026-05-26T11:00:00Z");
|
|
39
|
+
// English "1 hour ago" / "in 1 hour"; we only assert it's non-empty
|
|
40
|
+
// and is not the em-dash — locale formatting varies in test env.
|
|
41
|
+
const out = formatRelative(past.toISOString(), now);
|
|
42
|
+
expect(out).not.toBe("—");
|
|
43
|
+
expect(out.length).toBeGreaterThan(2);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("parseUserAgent", () => {
|
|
48
|
+
test("classifies a modern Chrome on macOS string", () => {
|
|
49
|
+
const ua =
|
|
50
|
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36";
|
|
51
|
+
expect(parseUserAgent(ua)).toEqual({ browser: "Chrome", os: "macOS" });
|
|
52
|
+
});
|
|
53
|
+
test("classifies Safari on iOS", () => {
|
|
54
|
+
const ua =
|
|
55
|
+
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1";
|
|
56
|
+
expect(parseUserAgent(ua)).toEqual({ browser: "Safari", os: "iOS" });
|
|
57
|
+
});
|
|
58
|
+
test("returns Unknown for nullish input", () => {
|
|
59
|
+
expect(parseUserAgent(null)).toEqual({ browser: "Unknown", os: "Unknown" });
|
|
60
|
+
});
|
|
61
|
+
});
|
package/src/lib/utils.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { clsx, type ClassValue } from "clsx";
|
|
2
|
+
import { twMerge } from "tailwind-merge";
|
|
3
|
+
|
|
4
|
+
export const cn = (...inputs: ClassValue[]): string => twMerge(clsx(inputs));
|
|
5
|
+
|
|
6
|
+
export const initialsOf = (name?: string | null, email?: string | null): string => {
|
|
7
|
+
const source = (name ?? email ?? "").trim();
|
|
8
|
+
if (!source) return "?";
|
|
9
|
+
const parts = source.split(/\s+/);
|
|
10
|
+
if (parts.length === 1) return source.slice(0, 2).toUpperCase();
|
|
11
|
+
return ((parts[0]?.[0] ?? "") + (parts[parts.length - 1]?.[0] ?? "")).toUpperCase();
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const formatRelative = (
|
|
15
|
+
iso: string | null | undefined,
|
|
16
|
+
now: Date = new Date()
|
|
17
|
+
): string => {
|
|
18
|
+
if (!iso) return "—";
|
|
19
|
+
const d = new Date(iso);
|
|
20
|
+
if (Number.isNaN(d.getTime())) return "—";
|
|
21
|
+
const diff = (d.getTime() - now.getTime()) / 1000;
|
|
22
|
+
const abs = Math.abs(diff);
|
|
23
|
+
const units: Array<[Intl.RelativeTimeFormatUnit, number]> = [
|
|
24
|
+
["year", 31536000],
|
|
25
|
+
["month", 2592000],
|
|
26
|
+
["week", 604800],
|
|
27
|
+
["day", 86400],
|
|
28
|
+
["hour", 3600],
|
|
29
|
+
["minute", 60],
|
|
30
|
+
["second", 1],
|
|
31
|
+
];
|
|
32
|
+
const rtf = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" });
|
|
33
|
+
for (const [unit, secs] of units) {
|
|
34
|
+
if (abs >= secs || unit === "second") {
|
|
35
|
+
return rtf.format(Math.round(diff / secs), unit);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return "—";
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const formatDateTime = (iso: string | null | undefined): string => {
|
|
42
|
+
if (!iso) return "—";
|
|
43
|
+
const d = new Date(iso);
|
|
44
|
+
if (Number.isNaN(d.getTime())) return "—";
|
|
45
|
+
return d.toLocaleString(undefined, {
|
|
46
|
+
year: "numeric",
|
|
47
|
+
month: "short",
|
|
48
|
+
day: "numeric",
|
|
49
|
+
hour: "2-digit",
|
|
50
|
+
minute: "2-digit",
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const parseUserAgent = (
|
|
55
|
+
ua: string | null | undefined
|
|
56
|
+
): { browser: string; os: string } => {
|
|
57
|
+
if (!ua) return { browser: "Unknown", os: "Unknown" };
|
|
58
|
+
const browsers: Array<[RegExp, string]> = [
|
|
59
|
+
[/Edg\//, "Edge"],
|
|
60
|
+
[/OPR\//, "Opera"],
|
|
61
|
+
[/Firefox\//, "Firefox"],
|
|
62
|
+
[/Chrome\//, "Chrome"],
|
|
63
|
+
[/Safari\//, "Safari"],
|
|
64
|
+
];
|
|
65
|
+
const oss: Array<[RegExp, string]> = [
|
|
66
|
+
// Check iOS / Android before macOS / Linux — iOS UA strings include
|
|
67
|
+
// "Mac OS X" and Android UAs include "Linux", so the broader checks
|
|
68
|
+
// win if they run first.
|
|
69
|
+
[/iPhone|iPad|iPod|iOS/, "iOS"],
|
|
70
|
+
[/Android/, "Android"],
|
|
71
|
+
[/Windows NT/, "Windows"],
|
|
72
|
+
[/Mac OS X/, "macOS"],
|
|
73
|
+
[/Linux/, "Linux"],
|
|
74
|
+
];
|
|
75
|
+
return {
|
|
76
|
+
browser: browsers.find(([r]) => r.test(ua))?.[1] ?? "Unknown",
|
|
77
|
+
os: oss.find(([r]) => r.test(ua))?.[1] ?? "Unknown",
|
|
78
|
+
};
|
|
79
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebAuthn browser glue.
|
|
3
|
+
*
|
|
4
|
+
* The ceremony (base64url ⇄ ArrayBuffer + `navigator.credentials`) now
|
|
5
|
+
* lives in the framework-agnostic client SDK so non-React consumers can
|
|
6
|
+
* use it too. This module re-exports it under the kit's historical names
|
|
7
|
+
* for backward compatibility — prefer the `passkeys(client)` facade from
|
|
8
|
+
* `@nexys/user-management-client` for new code.
|
|
9
|
+
*/
|
|
10
|
+
export {
|
|
11
|
+
browserSupportsPasskeys as webauthnSupported,
|
|
12
|
+
createPasskeyCredential as createPasskey,
|
|
13
|
+
getPasskeyAssertion,
|
|
14
|
+
} from "@nexys/user-management-client";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Translate WebAuthn DOMExceptions into sentences a person can act on.
|
|
18
|
+
* The browser's own wording for `InvalidStateError` ("The user
|
|
19
|
+
* attempted to register an authenticator that contains one of the
|
|
20
|
+
* credentials already registered with the relying party") reads like a
|
|
21
|
+
* server bug when it's really the device refusing a duplicate.
|
|
22
|
+
*/
|
|
23
|
+
export const webauthnErrorMessage = (err: unknown, fallback: string): string => {
|
|
24
|
+
const name = err instanceof Error ? err.name : "";
|
|
25
|
+
if (name === "InvalidStateError") {
|
|
26
|
+
return (
|
|
27
|
+
"This device already holds a passkey for this site — possibly for " +
|
|
28
|
+
"another account. Try signing in with it instead, or remove it from " +
|
|
29
|
+
"your device's password manager before registering a new one."
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
if (name === "NotAllowedError" || name === "AbortError") {
|
|
33
|
+
return "The passkey prompt was cancelled or timed out.";
|
|
34
|
+
}
|
|
35
|
+
return err instanceof Error ? err.message : fallback;
|
|
36
|
+
};
|
package/src/styles.css
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @nexys/user-management-web design tokens.
|
|
3
|
+
*
|
|
4
|
+
* Import once at the app entry: `import "@nexys/user-management-web/styles.css";`
|
|
5
|
+
*
|
|
6
|
+
* Tailwind v4 reads the @theme block below; consumers either also
|
|
7
|
+
* register the package's `src/**` glob (see ./tailwind.preset.cjs) or
|
|
8
|
+
* use @source in their own CSS so utility classes used inside the
|
|
9
|
+
* library are emitted.
|
|
10
|
+
*/
|
|
11
|
+
@import "tailwindcss";
|
|
12
|
+
|
|
13
|
+
@custom-variant dark (&:is(.dark *));
|
|
14
|
+
|
|
15
|
+
@theme inline {
|
|
16
|
+
--color-background: hsl(var(--background));
|
|
17
|
+
--color-foreground: hsl(var(--foreground));
|
|
18
|
+
--color-card: hsl(var(--card));
|
|
19
|
+
--color-card-foreground: hsl(var(--card-foreground));
|
|
20
|
+
--color-popover: hsl(var(--popover));
|
|
21
|
+
--color-popover-foreground: hsl(var(--popover-foreground));
|
|
22
|
+
--color-primary: hsl(var(--primary));
|
|
23
|
+
--color-primary-foreground: hsl(var(--primary-foreground));
|
|
24
|
+
--color-secondary: hsl(var(--secondary));
|
|
25
|
+
--color-secondary-foreground: hsl(var(--secondary-foreground));
|
|
26
|
+
--color-muted: hsl(var(--muted));
|
|
27
|
+
--color-muted-foreground: hsl(var(--muted-foreground));
|
|
28
|
+
--color-accent: hsl(var(--accent));
|
|
29
|
+
--color-accent-foreground: hsl(var(--accent-foreground));
|
|
30
|
+
--color-destructive: hsl(var(--destructive));
|
|
31
|
+
--color-destructive-foreground: hsl(var(--destructive-foreground));
|
|
32
|
+
--color-success: hsl(var(--success));
|
|
33
|
+
--color-success-foreground: hsl(var(--success-foreground));
|
|
34
|
+
--color-warning: hsl(var(--warning));
|
|
35
|
+
--color-warning-foreground: hsl(var(--warning-foreground));
|
|
36
|
+
--color-border: hsl(var(--border));
|
|
37
|
+
--color-input: hsl(var(--input));
|
|
38
|
+
--color-ring: hsl(var(--ring));
|
|
39
|
+
|
|
40
|
+
--radius-lg: var(--radius);
|
|
41
|
+
--radius-md: calc(var(--radius) - 2px);
|
|
42
|
+
--radius-sm: calc(var(--radius) - 4px);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@layer base {
|
|
46
|
+
:root {
|
|
47
|
+
--background: 60 9% 98%;
|
|
48
|
+
--foreground: 20 14.3% 4.1%;
|
|
49
|
+
--card: 0 0% 100%;
|
|
50
|
+
--card-foreground: 20 14.3% 4.1%;
|
|
51
|
+
--popover: 0 0% 100%;
|
|
52
|
+
--popover-foreground: 20 14.3% 4.1%;
|
|
53
|
+
--primary: 24 9.8% 10%;
|
|
54
|
+
--primary-foreground: 60 9.1% 97.8%;
|
|
55
|
+
--secondary: 60 4.8% 95.9%;
|
|
56
|
+
--secondary-foreground: 24 9.8% 10%;
|
|
57
|
+
--muted: 60 4.8% 95.9%;
|
|
58
|
+
--muted-foreground: 25 5.3% 44.7%;
|
|
59
|
+
--accent: 60 4.8% 95.9%;
|
|
60
|
+
--accent-foreground: 24 9.8% 10%;
|
|
61
|
+
--destructive: 0 84.2% 60.2%;
|
|
62
|
+
--destructive-foreground: 60 9.1% 97.8%;
|
|
63
|
+
--success: 142 71% 45%;
|
|
64
|
+
--success-foreground: 60 9.1% 97.8%;
|
|
65
|
+
--warning: 38 92% 50%;
|
|
66
|
+
--warning-foreground: 24 9.8% 10%;
|
|
67
|
+
--border: 20 5.9% 90%;
|
|
68
|
+
--input: 20 5.9% 90%;
|
|
69
|
+
--ring: 24 5.7% 32.9%;
|
|
70
|
+
--radius: 0.6rem;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.dark {
|
|
74
|
+
--background: 20 14.3% 4.1%;
|
|
75
|
+
--foreground: 60 9.1% 97.8%;
|
|
76
|
+
--card: 20 14.3% 6%;
|
|
77
|
+
--card-foreground: 60 9.1% 97.8%;
|
|
78
|
+
--popover: 20 14.3% 6%;
|
|
79
|
+
--popover-foreground: 60 9.1% 97.8%;
|
|
80
|
+
--primary: 60 9.1% 97.8%;
|
|
81
|
+
--primary-foreground: 24 9.8% 10%;
|
|
82
|
+
--secondary: 12 6.5% 15.1%;
|
|
83
|
+
--secondary-foreground: 60 9.1% 97.8%;
|
|
84
|
+
--muted: 12 6.5% 15.1%;
|
|
85
|
+
--muted-foreground: 24 5.4% 63.9%;
|
|
86
|
+
--accent: 12 6.5% 15.1%;
|
|
87
|
+
--accent-foreground: 60 9.1% 97.8%;
|
|
88
|
+
--destructive: 0 62.8% 50%;
|
|
89
|
+
--destructive-foreground: 60 9.1% 97.8%;
|
|
90
|
+
--success: 142 65% 50%;
|
|
91
|
+
--success-foreground: 20 14.3% 4.1%;
|
|
92
|
+
--warning: 38 92% 55%;
|
|
93
|
+
--warning-foreground: 20 14.3% 4.1%;
|
|
94
|
+
--border: 12 6.5% 18%;
|
|
95
|
+
--input: 12 6.5% 18%;
|
|
96
|
+
--ring: 24 5.7% 60%;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
* {
|
|
100
|
+
border-color: hsl(var(--border));
|
|
101
|
+
}
|
|
102
|
+
body {
|
|
103
|
+
background-color: hsl(var(--background));
|
|
104
|
+
color: hsl(var(--foreground));
|
|
105
|
+
font-feature-settings: "rlig" 1, "calt" 1, "ss01" 1, "cv11" 1;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tailwind v4 preset for @nexys/user-management-web consumers.
|
|
3
|
+
* v4 reads design tokens from @theme in ./src/styles.css; this preset
|
|
4
|
+
* just adds the package source to the content glob so utility classes
|
|
5
|
+
* used in the library are emitted by the consumer build.
|
|
6
|
+
*/
|
|
7
|
+
module.exports = {
|
|
8
|
+
content: [
|
|
9
|
+
require.resolve("@nexys/user-management-web/package.json").replace(
|
|
10
|
+
/package\.json$/,
|
|
11
|
+
"src/**/*.{ts,tsx}"
|
|
12
|
+
),
|
|
13
|
+
],
|
|
14
|
+
darkMode: ["class"],
|
|
15
|
+
};
|