@kaappu/react 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kaappu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,134 @@
1
+ # @kaappu/react
2
+
3
+ > Drop-in React components for [Kaappu Identity](https://kaappu.org) — authentication, authorization, and AI-aware access control.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@kaappu/react.svg?style=flat-square)](https://www.npmjs.com/package/@kaappu/react)
6
+ [![license](https://img.shields.io/npm/l/@kaappu/react.svg?style=flat-square)](./LICENSE)
7
+
8
+ ## What's in the box
9
+
10
+ - **`<KaappuProvider />`** — wraps your app, manages session state, handles silent token refresh
11
+ - **`<LoginPanel />`** — full sign-in UI: OAuth (Google / GitHub / Microsoft / Apple), passkey, email + password, magic link, OTP, phone
12
+ - **`<RegisterPanel />`** — full sign-up UI: OAuth providers, password rules, confirm password, friendly error messages, optional email verification
13
+ - **`<Authorize />`** — conditionally render based on permission or role
14
+ - **`<LoggedIn />` / `<LoggedOut />`** — conditional render based on session state
15
+ - **`<ProfileBadge />`** — user avatar with sign-out menu
16
+ - **`<AccountView />`** — drop-in account settings page
17
+ - **`useKaappu()`** — hook returning `{ isLoaded, isSignedIn, user, hasPermission, signOut, getToken, ... }`
18
+
19
+ All components are theme-aware, accessible, and work with both light and dark color schemes out of the box.
20
+
21
+ ## Install
22
+
23
+ ```bash
24
+ npm install @kaappu/react
25
+ # or
26
+ pnpm add @kaappu/react
27
+ # or
28
+ yarn add @kaappu/react
29
+ ```
30
+
31
+ Peer dependencies: `react >= 18`, `react-dom >= 18`.
32
+
33
+ ## Quick start
34
+
35
+ ```jsx
36
+ import { KaappuProvider, LoginPanel, LoggedIn, LoggedOut, useKaappu } from '@kaappu/react'
37
+
38
+ // 1. Wrap your app
39
+ function App() {
40
+ return (
41
+ <KaappuProvider
42
+ publishableKey="pk_live_..."
43
+ baseUrl="https://id.your-domain.com/igai"
44
+ >
45
+ <YourApp />
46
+ </KaappuProvider>
47
+ )
48
+ }
49
+
50
+ // 2. Use the components anywhere
51
+ function Header() {
52
+ const { user, signOut } = useKaappu()
53
+ return (
54
+ <header>
55
+ <LoggedOut>
56
+ <a href="/sign-in">Sign in</a>
57
+ </LoggedOut>
58
+ <LoggedIn>
59
+ <span>Hi, {user?.email}</span>
60
+ <button onClick={signOut}>Sign out</button>
61
+ </LoggedIn>
62
+ </header>
63
+ )
64
+ }
65
+
66
+ // 3. Drop the auth panels onto a page
67
+ function SignInPage() {
68
+ return <LoginPanel onSuccess={(token, user) => window.location.href = '/'} />
69
+ }
70
+ ```
71
+
72
+ ## Authorization
73
+
74
+ ```jsx
75
+ import { Authorize } from '@kaappu/react'
76
+
77
+ // Permission-gated content
78
+ <Authorize
79
+ permission="billing:manage"
80
+ fallback={<p>Upgrade to access billing.</p>}
81
+ >
82
+ <BillingPanel />
83
+ </Authorize>
84
+
85
+ // Role-gated content
86
+ <Authorize role="admin">
87
+ <AdminTools />
88
+ </Authorize>
89
+
90
+ // Wildcard permission "*" grants access to everything (built-in admin shortcut)
91
+ ```
92
+
93
+ ## Standalone mode (no provider)
94
+
95
+ If you don't want to wrap your app in `<KaappuProvider>`, you can use `<LoginPanel />` and `<RegisterPanel />` as standalone components:
96
+
97
+ ```jsx
98
+ <LoginPanel
99
+ authUrl="https://id.your-domain.com/api/auth"
100
+ accountId="your-tenant-id"
101
+ onSuccess={(token, user) => { /* persist token, redirect */ }}
102
+ />
103
+ ```
104
+
105
+ ## Theming
106
+
107
+ Pass an `appearance` prop to any component to override colors and typography:
108
+
109
+ ```jsx
110
+ import { LoginPanel, createTheme } from '@kaappu/react'
111
+
112
+ const theme = createTheme({
113
+ colorScheme: 'light',
114
+ variables: {
115
+ primaryColor: '#0ea5e9',
116
+ borderRadius: '0.5rem',
117
+ fontFamily: 'Inter, system-ui, sans-serif',
118
+ },
119
+ })
120
+
121
+ <LoginPanel appearance={theme} />
122
+ ```
123
+
124
+ Themes use CSS custom properties under the hood (`--k-primary`, `--k-card-bg`, `--k-text`, etc.) so you can also override them directly with stylesheets.
125
+
126
+ ## What this SDK is for
127
+
128
+ `@kaappu/react` is the React client for the Kaappu Identity platform — a closed-loop security control plane for AI-era applications. The SDK lets you drop production-grade authentication, authorization, and identity management into a React app in minutes, with the same primitives Kaappu uses on its own website (kaappu.org).
129
+
130
+ Learn more at [kaappu.org](https://kaappu.org) — including the Security Control Loop white paper and customer case studies.
131
+
132
+ ## License
133
+
134
+ [MIT](./LICENSE) © Kaappu
@@ -0,0 +1,354 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React$1 from 'react';
3
+ export { AuthResponse, KaappuApiClient, KaappuSession, KaappuTenantConfig, KaappuUser, checkAllPermissions, checkAnyPermission, checkPermission, isPermission } from '@kaappu/core';
4
+
5
+ /** Tenant config returned by GET /api/v1/accounts/config?pk=... */
6
+ interface KaappuTenantConfig {
7
+ accountId: string;
8
+ authMethods: {
9
+ password: boolean;
10
+ magicLink: boolean;
11
+ emailOtp: boolean;
12
+ phoneOtp: boolean;
13
+ passkeys: boolean;
14
+ google: boolean;
15
+ github: boolean;
16
+ microsoft: boolean;
17
+ };
18
+ customProviders: Array<{
19
+ id: string;
20
+ name: string;
21
+ discoveryUrl: string;
22
+ }>;
23
+ branding: {
24
+ logoUrl: string;
25
+ primaryColor: string;
26
+ name: string;
27
+ };
28
+ botProtection: {
29
+ enabled: boolean;
30
+ siteKey: string;
31
+ };
32
+ policy: {
33
+ mfaRequired: boolean;
34
+ requireEmailVerification: boolean;
35
+ };
36
+ }
37
+ /** Authenticated user shape */
38
+ interface KaappuUser {
39
+ id: string;
40
+ email: string;
41
+ firstName?: string;
42
+ lastName?: string;
43
+ avatarUrl?: string;
44
+ emailVerified?: boolean;
45
+ mfaEnabled?: boolean;
46
+ accountId: string;
47
+ sessionId: string;
48
+ roles?: string[];
49
+ permissions?: string[];
50
+ }
51
+ /** Auth state exposed via useKaappu() */
52
+ interface KaappuContextValue {
53
+ /** Whether the provider has finished loading (config + session check) */
54
+ isLoaded: boolean;
55
+ /** Whether a user is signed in */
56
+ isSignedIn: boolean;
57
+ /** The signed-in user, or null */
58
+ user: KaappuUser | null;
59
+ /** Current access token (may be refreshed transparently) */
60
+ accessToken: string | null;
61
+ /** Tenant configuration fetched at init */
62
+ tenantConfig: KaappuTenantConfig | null;
63
+ /** Sign out — clears session + tokens */
64
+ signOut: () => Promise<void>;
65
+ /** Get the current access token (refreshes if needed) */
66
+ getToken: () => Promise<string | null>;
67
+ /** Check if user has a permission (client-side, from token claims) */
68
+ hasPermission: (permission: string) => boolean;
69
+ }
70
+ /** KaappuProvider props */
71
+ interface KaappuProviderProps {
72
+ /** The publishable key for your Kaappu account (pk_live_xxx) */
73
+ publishableKey: string;
74
+ /** Base URL of the Kaappu API (e.g. https://api.kaappu.com or http://localhost:9091) */
75
+ baseUrl?: string;
76
+ /** Default: '/sign-in' */
77
+ signInUrl?: string;
78
+ /** Default: '/sign-up' */
79
+ signUpUrl?: string;
80
+ children: React.ReactNode;
81
+ }
82
+ /** Appearance customization — passed to any SDK component */
83
+ interface KaappuAppearance {
84
+ /**
85
+ * Color scheme. 'auto' follows the OS prefers-color-scheme setting.
86
+ * Default: 'dark'
87
+ */
88
+ colorScheme?: 'dark' | 'light' | 'auto';
89
+ /** Override CSS custom properties */
90
+ variables?: {
91
+ primaryColor?: string;
92
+ primaryForeground?: string;
93
+ backgroundColor?: string;
94
+ cardBackground?: string;
95
+ textColor?: string;
96
+ mutedColor?: string;
97
+ borderColor?: string;
98
+ borderRadius?: string;
99
+ fontFamily?: string;
100
+ };
101
+ /** Class names for individual elements (slots) */
102
+ elements?: {
103
+ card?: string;
104
+ title?: string;
105
+ subtitle?: string;
106
+ input?: string;
107
+ button?: string;
108
+ divider?: string;
109
+ oauthButton?: string;
110
+ footer?: string;
111
+ badge?: string;
112
+ dropdown?: string;
113
+ dropdownItem?: string;
114
+ };
115
+ }
116
+ /** ProfileBadge props */
117
+ interface ProfileBadgeProps$1 {
118
+ afterSignOutUrl?: string;
119
+ userProfileUrl?: string;
120
+ appearance?: KaappuAppearance;
121
+ className?: string;
122
+ }
123
+ /** AccountView props */
124
+ interface AccountViewProps$1 {
125
+ appearance?: KaappuAppearance;
126
+ className?: string;
127
+ }
128
+ /** LoginPanel props */
129
+ interface LoginPanelProps {
130
+ /** Called with accessToken on successful sign-in */
131
+ onSuccess?: (token: string, user: KaappuUser) => void;
132
+ /** Redirect URL after sign-in (used by kaappuPipeline integration) */
133
+ redirectUrl?: string;
134
+ /** Override the logo displayed inside the card (URL or React node) */
135
+ logoUrl?: string;
136
+ /** Appearance overrides */
137
+ appearance?: KaappuAppearance;
138
+ /** Custom CSS class for the card wrapper */
139
+ className?: string;
140
+ /** Override OAuth redirect base URL (e.g., for WordPress proxy). If set, OAuth goes to {oauthProxyUrl}/{provider} instead of {baseUrl}/oauth/{provider} */
141
+ oauthProxyUrl?: string;
142
+ /** List of OAuth providers to show (e.g., ['google']). If set, only these are shown. */
143
+ allowedProviders?: string[];
144
+ }
145
+ /** RegisterPanel props */
146
+ interface RegisterPanelProps {
147
+ /** Called with accessToken on successful registration */
148
+ onSuccess?: (token: string, user: KaappuUser) => void;
149
+ /** Redirect URL after registration */
150
+ redirectUrl?: string;
151
+ /** Override the logo displayed inside the card (URL or data URI) */
152
+ logoUrl?: string;
153
+ /** Appearance overrides */
154
+ appearance?: KaappuAppearance;
155
+ className?: string;
156
+ }
157
+
158
+ declare function KaappuProvider({ publishableKey, baseUrl, signInUrl, signUpUrl, children, }: KaappuProviderProps): react_jsx_runtime.JSX.Element;
159
+
160
+ declare function useKaappu(): KaappuContextValue;
161
+
162
+ /**
163
+ * <LoginPanel /> — Drop-in sign-in component.
164
+ *
165
+ * Renders the full Kaappu authentication UI: OAuth providers, passkey,
166
+ * email/password, magic link, OTP, and phone tabs.
167
+ *
168
+ * All auth calls go through the configured middleware (Next.js API routes
169
+ * at /api/auth/*) — never directly to igai-connector.
170
+ *
171
+ * @example
172
+ * // Inside <KaappuProvider baseUrl="/api/auth">
173
+ * <LoginPanel onSuccess={(token, user) => router.push('/')} />
174
+ *
175
+ * // Or standalone without KaappuProvider:
176
+ * <LoginPanel
177
+ * authUrl="https://myapp.com/api/auth"
178
+ * accountId="kaappu_org"
179
+ * onSuccess={(token, user) => { ... }}
180
+ * />
181
+ */
182
+ declare function LoginPanel({ onSuccess, redirectUrl, logoUrl, appearance, className, oauthProxyUrl, allowedProviders, authUrl: authUrlProp, accountId: accountIdProp, signUpPath, signUpLabel, signUpPrompt, }: LoginPanelProps & {
183
+ authUrl?: string;
184
+ accountId?: string;
185
+ signUpPath?: string;
186
+ signUpLabel?: string;
187
+ signUpPrompt?: string;
188
+ }): react_jsx_runtime.JSX.Element;
189
+
190
+ /**
191
+ * <RegisterPanel /> — Drop-in sign-up component.
192
+ *
193
+ * Renders the full Kaappu registration UI: OAuth providers, name/email/
194
+ * password form with confirm-password and live password rule indicators,
195
+ * friendly error messages, and an optional email-verification step.
196
+ *
197
+ * Designed for visual parity with <LoginPanel /> so apps can wire both
198
+ * components into a single onboarding flow.
199
+ *
200
+ * @example
201
+ * // Inside <KaappuProvider baseUrl="/api/auth">
202
+ * <RegisterPanel onSuccess={(token, user) => router.push('/')} />
203
+ *
204
+ * // Standalone (no provider needed):
205
+ * <RegisterPanel
206
+ * authUrl="https://myapp.com/api/auth"
207
+ * accountId="acme"
208
+ * onSuccess={(token, user) => { ... }}
209
+ * />
210
+ */
211
+ declare function RegisterPanel({ onSuccess, redirectUrl, logoUrl, appearance, className, authUrl: authUrlProp, accountId: accountIdProp, signInPath, signInLabel, signInPrompt, allowedProviders, oauthProxyUrl, }: RegisterPanelProps & {
212
+ authUrl?: string;
213
+ accountId?: string;
214
+ signInPath?: string;
215
+ signInLabel?: string;
216
+ signInPrompt?: string;
217
+ allowedProviders?: string[];
218
+ oauthProxyUrl?: string;
219
+ }): react_jsx_runtime.JSX.Element;
220
+
221
+ interface ProfileBadgeProps {
222
+ /** URL to redirect after sign-out (default: '/sign-in') */
223
+ afterSignOutUrl?: string;
224
+ /** URL for "Manage account" link (default: '/account') */
225
+ userProfileUrl?: string;
226
+ /** Appearance overrides */
227
+ appearance?: KaappuAppearance;
228
+ /** Custom CSS class for the trigger button */
229
+ className?: string;
230
+ }
231
+ /**
232
+ * <ProfileBadge /> — compact avatar + dropdown menu.
233
+ * Shows user's initials (or avatar image) and name.
234
+ * Click opens a dropdown: Manage account, Sign out.
235
+ *
236
+ * @example
237
+ * <ProfileBadge afterSignOutUrl="/sign-in" userProfileUrl="/dashboard/profile" />
238
+ */
239
+ declare function ProfileBadge({ afterSignOutUrl, userProfileUrl, appearance, className, }: ProfileBadgeProps): react_jsx_runtime.JSX.Element | null;
240
+
241
+ interface AccountViewProps {
242
+ /** Appearance overrides */
243
+ appearance?: KaappuAppearance;
244
+ /** Custom CSS class for the wrapper */
245
+ className?: string;
246
+ }
247
+ /**
248
+ * <AccountView /> — full embeddable profile + security management component.
249
+ *
250
+ * Tabs:
251
+ * - Profile — display name editing
252
+ * - Security — TOTP MFA enrollment/status, password change
253
+ * - Sessions — active session list with individual revoke
254
+ * - Passkeys — registered passkey list with delete
255
+ *
256
+ * @example
257
+ * // app/account/page.tsx
258
+ * import { AccountView } from '@kaappu/react'
259
+ * export default function AccountPage() {
260
+ * return <AccountView />
261
+ * }
262
+ */
263
+ declare function AccountView({ appearance, className }: AccountViewProps): react_jsx_runtime.JSX.Element | null;
264
+
265
+ interface LoggedInProps {
266
+ children: React$1.ReactNode;
267
+ /** Rendered while auth state is loading */
268
+ fallback?: React$1.ReactNode;
269
+ }
270
+ /**
271
+ * Renders children only when the user is signed in.
272
+ *
273
+ * @example
274
+ * <LoggedIn fallback={<Spinner />}>
275
+ * <UserMenu />
276
+ * </LoggedIn>
277
+ */
278
+ declare function LoggedIn({ children, fallback }: LoggedInProps): react_jsx_runtime.JSX.Element | null;
279
+ interface LoggedOutProps {
280
+ children: React$1.ReactNode;
281
+ /** Rendered while auth state is loading */
282
+ fallback?: React$1.ReactNode;
283
+ }
284
+ /**
285
+ * Renders children only when the user is NOT signed in.
286
+ *
287
+ * @example
288
+ * <LoggedOut>
289
+ * <a href="/sign-in">Sign in</a>
290
+ * </LoggedOut>
291
+ */
292
+ declare function LoggedOut({ children, fallback }: LoggedOutProps): react_jsx_runtime.JSX.Element | null;
293
+ interface AuthorizeProps {
294
+ children: React$1.ReactNode;
295
+ /**
296
+ * Required permission string (e.g. "billing:manage", "users:read").
297
+ * Uses client-side permission list from the user's session.
298
+ * The wildcard permission "*" grants access to everything.
299
+ */
300
+ permission?: string;
301
+ /**
302
+ * Required role name (e.g. "admin", "owner").
303
+ * Checked against user.roles[].
304
+ */
305
+ role?: string;
306
+ /** Rendered when the user lacks the required permission/role */
307
+ fallback?: React$1.ReactNode;
308
+ /** Rendered while auth state is loading */
309
+ loading?: React$1.ReactNode;
310
+ }
311
+ /**
312
+ * Conditionally renders children based on the user's permissions or role.
313
+ * Combines with <LoggedIn> semantics — unauthenticated users see fallback.
314
+ *
315
+ * @example
316
+ * // Permission-gated
317
+ * <Authorize permission="billing:manage" fallback={<p>Upgrade to access billing.</p>}>
318
+ * <BillingPanel />
319
+ * </Authorize>
320
+ *
321
+ * // Role-gated
322
+ * <Authorize role="admin">
323
+ * <AdminTools />
324
+ * </Authorize>
325
+ *
326
+ * // Combine both (user must have BOTH)
327
+ * <Authorize role="admin" permission="users:delete">
328
+ * <DangerZone />
329
+ * </Authorize>
330
+ */
331
+ declare function Authorize({ children, permission, role, fallback, loading, }: AuthorizeProps): react_jsx_runtime.JSX.Element;
332
+
333
+ /**
334
+ * Resolves the active color scheme.
335
+ * 'auto' reads prefers-color-scheme at call time (SSR-safe — defaults to dark).
336
+ */
337
+ declare function resolveColorScheme(scheme?: 'dark' | 'light' | 'auto'): 'dark' | 'light';
338
+ /**
339
+ * Build the CSS variables object to apply as `style` on the root element.
340
+ * Merges theme defaults → branding primaryColor → appearance overrides.
341
+ */
342
+ declare function buildThemeVars(appearance?: KaappuAppearance, brandingColor?: string): React.CSSProperties;
343
+ /**
344
+ * createTheme() — convenience helper for customers who want to define a
345
+ * theme object once and reuse it across multiple components.
346
+ *
347
+ * @example
348
+ * const theme = createTheme({ colorScheme: 'light', variables: { primaryColor: '#0ea5e9' } })
349
+ * <LoginPanel appearance={theme} />
350
+ * <ProfileBadge appearance={theme} />
351
+ */
352
+ declare function createTheme(appearance: KaappuAppearance): KaappuAppearance;
353
+
354
+ export { AccountView, type AccountViewProps$1 as AccountViewProps, Authorize, type KaappuAppearance, type KaappuContextValue, KaappuProvider, type KaappuProviderProps, LoggedIn, LoggedOut, LoginPanel, type LoginPanelProps, ProfileBadge, type ProfileBadgeProps$1 as ProfileBadgeProps, RegisterPanel, type RegisterPanelProps, buildThemeVars, createTheme, resolveColorScheme, useKaappu };