@kontrolia/react 1.0.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 KontrolIA Auth Contributors
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,37 @@
1
+ # @kontrolia/react
2
+
3
+ Bindings de React para [KontrolIA Auth](https://github.com/rauldolores/auth): `<AuthProvider>`, `<AuthGuard>`, `<GuestGuard>`, `<RequireRole>`, `<RequirePermission>` y el hook `useAuth()`.
4
+
5
+ ## Instalación
6
+
7
+ ```bash
8
+ npm install @kontrolia/react @kontrolia/auth
9
+ ```
10
+
11
+ ## Uso
12
+
13
+ ```tsx
14
+ import { AuthProvider, AuthGuard, useAuth } from "@kontrolia/react";
15
+
16
+ function App() {
17
+ return (
18
+ <AuthProvider config={{ supabaseUrl, supabaseAnonKey }}>
19
+ <AuthGuard fallback={<LoginPage />}>
20
+ <Dashboard />
21
+ </AuthGuard>
22
+ </AuthProvider>
23
+ );
24
+ }
25
+
26
+ function Dashboard() {
27
+ const { user, organization, hasPermission, logout } = useAuth();
28
+ if (!hasPermission("facturacion.facturas.crear")) return null;
29
+ // ...
30
+ }
31
+ ```
32
+
33
+ `<RequirePermission>` / `<RequireRole>` renderizan condicionalmente según los claims del JWT (roles/permisos de la organización activa), sin round-trip al servidor.
34
+
35
+ ## Documentación
36
+
37
+ Ver la [guía completa](https://github.com/rauldolores/auth) del monorepo.
@@ -0,0 +1,20 @@
1
+ import { type KontroliaClient, type KontroliaClientConfig } from "@kontrolia/auth";
2
+ import { type PermissionChecker } from "@kontrolia/permissions";
3
+ import type { KontroliaOrganization, KontroliaUser } from "@kontrolia/shared";
4
+ export interface KontroliaAuthState {
5
+ client: KontroliaClient;
6
+ isLoading: boolean;
7
+ isAuthenticated: boolean;
8
+ user: KontroliaUser | null;
9
+ organization: KontroliaOrganization | null;
10
+ roles: string[];
11
+ permissions: string[];
12
+ checker: PermissionChecker;
13
+ }
14
+ export interface AuthProviderProps {
15
+ config: KontroliaClientConfig;
16
+ children: React.ReactNode;
17
+ }
18
+ export declare function AuthProvider({ config, children }: AuthProviderProps): import("react").JSX.Element;
19
+ export declare function useKontroliaAuthContext(): KontroliaAuthState;
20
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,qBAAqB,EAAyB,MAAM,iBAAiB,CAAC;AAC1G,OAAO,EAAE,KAAK,iBAAiB,EAA2B,MAAM,wBAAwB,CAAC;AACzF,OAAO,KAAK,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAG9E,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,eAAe,CAAC;IACxB,SAAS,EAAE,OAAO,CAAC;IACnB,eAAe,EAAE,OAAO,CAAC;IACzB,IAAI,EAAE,aAAa,GAAG,IAAI,CAAC;IAC3B,YAAY,EAAE,qBAAqB,GAAG,IAAI,CAAC;IAC3C,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,OAAO,EAAE,iBAAiB,CAAC;CAC5B;AAID,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,qBAAqB,CAAC;IAC9B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAED,wBAAgB,YAAY,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,iBAAiB,+BAsDnE;AAED,wBAAgB,uBAAuB,IAAI,kBAAkB,CAM5D"}
@@ -0,0 +1,58 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { createKontroliaClient } from "@kontrolia/auth";
4
+ import { createPermissionChecker } from "@kontrolia/permissions";
5
+ import { createContext, useContext, useEffect, useMemo, useState } from "react";
6
+ const KontroliaAuthContext = createContext(null);
7
+ export function AuthProvider({ config, children }) {
8
+ const client = useMemo(() => createKontroliaClient(config), [config.supabaseUrl, config.supabaseAnonKey]);
9
+ const [state, setState] = useState({
10
+ isLoading: true,
11
+ isAuthenticated: false,
12
+ user: null,
13
+ organization: null,
14
+ roles: [],
15
+ permissions: [],
16
+ });
17
+ useEffect(() => {
18
+ let cancelled = false;
19
+ async function loadInitial() {
20
+ const [user, organization, roles, permissions] = await Promise.all([
21
+ client.getUser(),
22
+ client.getOrganization(),
23
+ client.getRoles(),
24
+ client.getPermissions(),
25
+ ]);
26
+ if (cancelled)
27
+ return;
28
+ setState({ isLoading: false, isAuthenticated: user !== null, user, organization, roles, permissions });
29
+ }
30
+ void loadInitial();
31
+ const unsubscribe = client.onAuthStateChange((next) => {
32
+ if (cancelled)
33
+ return;
34
+ setState({
35
+ isLoading: false,
36
+ isAuthenticated: next.user !== null,
37
+ user: next.user,
38
+ organization: next.organization,
39
+ roles: next.roles,
40
+ permissions: next.permissions,
41
+ });
42
+ });
43
+ return () => {
44
+ cancelled = true;
45
+ unsubscribe();
46
+ };
47
+ }, [client]);
48
+ const checker = useMemo(() => createPermissionChecker({ roles: state.roles, permissions: state.permissions }), [state.roles, state.permissions]);
49
+ const value = { ...state, client, checker };
50
+ return _jsx(KontroliaAuthContext.Provider, { value: value, children: children });
51
+ }
52
+ export function useKontroliaAuthContext() {
53
+ const context = useContext(KontroliaAuthContext);
54
+ if (!context) {
55
+ throw new Error("useAuth() must be used within an <AuthProvider>");
56
+ }
57
+ return context;
58
+ }
@@ -0,0 +1,23 @@
1
+ import type { EvaluationMode } from "@kontrolia/permissions";
2
+ export interface GuardProps {
3
+ children: React.ReactNode;
4
+ /** Rendered while the initial session is being resolved. */
5
+ loading?: React.ReactNode;
6
+ /** Rendered when the guard's condition is not met. */
7
+ fallback?: React.ReactNode;
8
+ }
9
+ /** Renders children only for an authenticated user. */
10
+ export declare function AuthGuard({ children, loading, fallback }: GuardProps): import("react").JSX.Element;
11
+ /** Renders children only for a guest (unauthenticated) — typical for login/register pages. */
12
+ export declare function GuestGuard({ children, loading, fallback }: GuardProps): import("react").JSX.Element;
13
+ export interface RequireRoleProps extends GuardProps {
14
+ role: string | string[];
15
+ mode?: EvaluationMode;
16
+ }
17
+ export declare function RequireRole({ role, mode, children, loading, fallback }: RequireRoleProps): import("react").JSX.Element;
18
+ export interface RequirePermissionProps extends GuardProps {
19
+ permission: string | string[];
20
+ mode?: EvaluationMode;
21
+ }
22
+ export declare function RequirePermission({ permission, mode, children, loading, fallback, }: RequirePermissionProps): import("react").JSX.Element;
23
+ //# sourceMappingURL=guards.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../src/guards.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAG7D,MAAM,WAAW,UAAU;IACzB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,4DAA4D;IAC5D,OAAO,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,sDAAsD;IACtD,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC5B;AAED,uDAAuD;AACvD,wBAAgB,SAAS,CAAC,EAAE,QAAQ,EAAE,OAAc,EAAE,QAAe,EAAE,EAAE,UAAU,+BAIlF;AAED,8FAA8F;AAC9F,wBAAgB,UAAU,CAAC,EAAE,QAAQ,EAAE,OAAc,EAAE,QAAe,EAAE,EAAE,UAAU,+BAInF;AAED,MAAM,WAAW,gBAAiB,SAAQ,UAAU;IAClD,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACxB,IAAI,CAAC,EAAE,cAAc,CAAC;CACvB;AAED,wBAAgB,WAAW,CAAC,EAAE,IAAI,EAAE,IAAY,EAAE,QAAQ,EAAE,OAAc,EAAE,QAAe,EAAE,EAAE,gBAAgB,+BAI9G;AAED,MAAM,WAAW,sBAAuB,SAAQ,UAAU;IACxD,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC9B,IAAI,CAAC,EAAE,cAAc,CAAC;CACvB;AAED,wBAAgB,iBAAiB,CAAC,EAChC,UAAU,EACV,IAAY,EACZ,QAAQ,EACR,OAAc,EACd,QAAe,GAChB,EAAE,sBAAsB,+BAIxB"}
package/dist/guards.js ADDED
@@ -0,0 +1,29 @@
1
+ "use client";
2
+ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
3
+ import { useKontroliaAuthContext } from "./context.js";
4
+ /** Renders children only for an authenticated user. */
5
+ export function AuthGuard({ children, loading = null, fallback = null }) {
6
+ const { isLoading, isAuthenticated } = useKontroliaAuthContext();
7
+ if (isLoading)
8
+ return _jsx(_Fragment, { children: loading });
9
+ return _jsx(_Fragment, { children: isAuthenticated ? children : fallback });
10
+ }
11
+ /** Renders children only for a guest (unauthenticated) — typical for login/register pages. */
12
+ export function GuestGuard({ children, loading = null, fallback = null }) {
13
+ const { isLoading, isAuthenticated } = useKontroliaAuthContext();
14
+ if (isLoading)
15
+ return _jsx(_Fragment, { children: loading });
16
+ return _jsx(_Fragment, { children: !isAuthenticated ? children : fallback });
17
+ }
18
+ export function RequireRole({ role, mode = "any", children, loading = null, fallback = null }) {
19
+ const { isLoading, checker } = useKontroliaAuthContext();
20
+ if (isLoading)
21
+ return _jsx(_Fragment, { children: loading });
22
+ return _jsx(_Fragment, { children: checker.hasRole(role, mode) ? children : fallback });
23
+ }
24
+ export function RequirePermission({ permission, mode = "any", children, loading = null, fallback = null, }) {
25
+ const { isLoading, checker } = useKontroliaAuthContext();
26
+ if (isLoading)
27
+ return _jsx(_Fragment, { children: loading });
28
+ return _jsx(_Fragment, { children: checker.hasPermission(permission, mode) ? children : fallback });
29
+ }
@@ -0,0 +1,4 @@
1
+ export { AuthProvider, useKontroliaAuthContext, type KontroliaAuthState } from "./context.js";
2
+ export { useAuth } from "./use-auth.js";
3
+ export { AuthGuard, GuestGuard, RequireRole, RequirePermission, type GuardProps } from "./guards.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,uBAAuB,EAAE,KAAK,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAC9F,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,iBAAiB,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { AuthProvider, useKontroliaAuthContext } from "./context.js";
2
+ export { useAuth } from "./use-auth.js";
3
+ export { AuthGuard, GuestGuard, RequireRole, RequirePermission } from "./guards.js";
@@ -0,0 +1,34 @@
1
+ /**
2
+ * The single hook every consuming app uses to talk to KontrolIA Auth.
3
+ * Mirrors the @kontrolia/auth client 1:1 plus the current reactive state,
4
+ * so components never need to import @kontrolia/auth or supabase-js
5
+ * directly.
6
+ */
7
+ export declare function useAuth(): {
8
+ login: ({ email, password }: import("@kontrolia/auth").LoginCredentials) => Promise<void>;
9
+ loginWithOAuth: (provider: import("@kontrolia/auth").OAuthProvider, redirectTo?: string) => Promise<void>;
10
+ logout: () => Promise<void>;
11
+ register: ({ email, password, fullName }: import("@kontrolia/auth").RegisterInput) => Promise<void>;
12
+ refresh: () => Promise<void>;
13
+ requestPasswordReset: (email: string, redirectTo?: string) => Promise<void>;
14
+ updatePassword: (newPassword: string) => Promise<void>;
15
+ updateProfile: (input: import("@kontrolia/auth").UpdateProfileInput) => Promise<void>;
16
+ switchOrganization: (organizationId: string) => Promise<void>;
17
+ getToken: () => Promise<string | null>;
18
+ enrollMfa: (friendlyName?: string) => Promise<import("@kontrolia/auth").MfaEnrollment>;
19
+ verifyMfaEnrollment: (factorId: string, code: string) => Promise<void>;
20
+ challengeMfa: (factorId: string) => Promise<string>;
21
+ verifyMfaChallenge: (factorId: string, challengeId: string, code: string) => Promise<void>;
22
+ listMfaFactors: () => Promise<import("@kontrolia/auth").MfaFactor[]>;
23
+ unenrollMfa: (factorId: string) => Promise<void>;
24
+ getAuthenticatorAssuranceLevel: () => Promise<import("@kontrolia/auth").AuthenticatorAssuranceLevel>;
25
+ hasPermission: (required: string | string[], mode?: import("@kontrolia/permissions").EvaluationMode) => boolean;
26
+ hasRole: (required: string | string[], mode?: import("@kontrolia/permissions").EvaluationMode) => boolean;
27
+ isLoading: boolean;
28
+ isAuthenticated: boolean;
29
+ user: import("@kontrolia/shared").KontroliaUser | null;
30
+ organization: import("@kontrolia/shared").KontroliaOrganization | null;
31
+ roles: string[];
32
+ permissions: string[];
33
+ };
34
+ //# sourceMappingURL=use-auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-auth.d.ts","sourceRoot":"","sources":["../src/use-auth.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,wBAAgB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;EAyBtB"}
@@ -0,0 +1,33 @@
1
+ "use client";
2
+ import { useKontroliaAuthContext } from "./context.js";
3
+ /**
4
+ * The single hook every consuming app uses to talk to KontrolIA Auth.
5
+ * Mirrors the @kontrolia/auth client 1:1 plus the current reactive state,
6
+ * so components never need to import @kontrolia/auth or supabase-js
7
+ * directly.
8
+ */
9
+ export function useAuth() {
10
+ const { client, checker, ...state } = useKontroliaAuthContext();
11
+ return {
12
+ ...state,
13
+ login: client.login.bind(client),
14
+ loginWithOAuth: client.loginWithOAuth.bind(client),
15
+ logout: client.logout.bind(client),
16
+ register: client.register.bind(client),
17
+ refresh: client.refresh.bind(client),
18
+ requestPasswordReset: client.requestPasswordReset.bind(client),
19
+ updatePassword: client.updatePassword.bind(client),
20
+ updateProfile: client.updateProfile.bind(client),
21
+ switchOrganization: client.switchOrganization.bind(client),
22
+ getToken: client.getToken.bind(client),
23
+ enrollMfa: client.enrollMfa.bind(client),
24
+ verifyMfaEnrollment: client.verifyMfaEnrollment.bind(client),
25
+ challengeMfa: client.challengeMfa.bind(client),
26
+ verifyMfaChallenge: client.verifyMfaChallenge.bind(client),
27
+ listMfaFactors: client.listMfaFactors.bind(client),
28
+ unenrollMfa: client.unenrollMfa.bind(client),
29
+ getAuthenticatorAssuranceLevel: client.getAuthenticatorAssuranceLevel.bind(client),
30
+ hasPermission: checker.hasPermission,
31
+ hasRole: checker.hasRole,
32
+ };
33
+ }
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@kontrolia/react",
3
+ "version": "1.0.0",
4
+ "license": "MIT",
5
+ "description": "React bindings for KontrolIA Auth: <AuthProvider>, <AuthGuard>, <GuestGuard>, <RequireRole>, <RequirePermission> and useAuth().",
6
+ "keywords": [
7
+ "kontrolia",
8
+ "auth",
9
+ "react",
10
+ "authentication",
11
+ "authorization",
12
+ "rbac",
13
+ "sdk"
14
+ ],
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/rauldolores/auth.git",
18
+ "directory": "packages/react-sdk"
19
+ },
20
+ "homepage": "https://github.com/rauldolores/auth#readme",
21
+ "bugs": "https://github.com/rauldolores/auth/issues",
22
+ "author": "KontrolIA",
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "type": "module",
27
+ "main": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "peerDependencies": {
33
+ "react": ">=18.0.0",
34
+ "react-dom": ">=18.0.0"
35
+ },
36
+ "dependencies": {
37
+ "@kontrolia/permissions": "1.0.0",
38
+ "@kontrolia/shared": "1.0.0",
39
+ "@kontrolia/auth": "1.0.0"
40
+ },
41
+ "devDependencies": {
42
+ "@types/react": "^18.3.17",
43
+ "@types/react-dom": "^18.3.5",
44
+ "eslint": "^9.17.0",
45
+ "react": "^18.3.1",
46
+ "react-dom": "^18.3.1",
47
+ "typescript": "^5.7.2",
48
+ "@kontrolia/config": "0.1.0"
49
+ },
50
+ "scripts": {
51
+ "build": "tsc -p tsconfig.json",
52
+ "dev": "tsc -p tsconfig.json --watch",
53
+ "lint": "eslint .",
54
+ "typecheck": "tsc -p tsconfig.json --noEmit",
55
+ "clean": "rimraf dist"
56
+ }
57
+ }