@ram_28/kf-ai-sdk 1.0.1 → 1.0.3
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 +155 -1
- package/dist/auth/AuthProvider.d.ts +5 -0
- package/dist/auth/AuthProvider.d.ts.map +1 -0
- package/dist/auth/authClient.d.ts +42 -0
- package/dist/auth/authClient.d.ts.map +1 -0
- package/dist/auth/authConfig.d.ts +38 -0
- package/dist/auth/authConfig.d.ts.map +1 -0
- package/dist/auth/index.d.ts +6 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/types.d.ts +156 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/useAuth.d.ts +27 -0
- package/dist/auth/useAuth.d.ts.map +1 -0
- package/dist/index.cjs +12 -12
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +2005 -1731
- package/package.json +1 -1
- package/sdk/auth/AuthProvider.tsx +277 -0
- package/sdk/auth/authClient.ts +175 -0
- package/sdk/auth/authConfig.ts +105 -0
- package/sdk/auth/index.ts +40 -0
- package/sdk/auth/types.ts +210 -0
- package/sdk/auth/useAuth.ts +69 -0
- package/sdk/index.ts +4 -1
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ npm install react @tanstack/react-query
|
|
|
15
15
|
|
|
16
16
|
## Features
|
|
17
17
|
|
|
18
|
+
- **Authentication** - Cookie-based auth with AuthProvider and useAuth hook
|
|
18
19
|
- **useForm** - Dynamic schema-driven forms with backend validation
|
|
19
20
|
- **useTable** - Data tables with sorting, pagination, and React Query integration
|
|
20
21
|
- **useKanban** - Kanban board state management with drag-drop support
|
|
@@ -27,12 +28,22 @@ npm install react @tanstack/react-query
|
|
|
27
28
|
|
|
28
29
|
```tsx
|
|
29
30
|
import {
|
|
31
|
+
// Authentication
|
|
32
|
+
AuthProvider,
|
|
33
|
+
useAuth,
|
|
34
|
+
configureAuth,
|
|
35
|
+
|
|
36
|
+
// Hooks
|
|
30
37
|
useForm,
|
|
31
38
|
useTable,
|
|
32
39
|
useKanban,
|
|
33
40
|
useFilter,
|
|
41
|
+
|
|
42
|
+
// API
|
|
34
43
|
api,
|
|
35
44
|
setApiBaseUrl,
|
|
45
|
+
|
|
46
|
+
// Utilities
|
|
36
47
|
formatCurrency,
|
|
37
48
|
formatDate
|
|
38
49
|
} from '@ram_28/kf-ai-sdk';
|
|
@@ -41,6 +52,149 @@ import {
|
|
|
41
52
|
setApiBaseUrl('https://api.example.com');
|
|
42
53
|
```
|
|
43
54
|
|
|
55
|
+
## Authentication
|
|
56
|
+
|
|
57
|
+
The SDK provides a complete authentication solution with cookie-based session management.
|
|
58
|
+
|
|
59
|
+
### Setup
|
|
60
|
+
|
|
61
|
+
Wrap your app with `AuthProvider` inside a `QueryClientProvider`:
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
65
|
+
import { AuthProvider, setApiBaseUrl, configureAuth } from '@ram_28/kf-ai-sdk';
|
|
66
|
+
|
|
67
|
+
// Configure API
|
|
68
|
+
setApiBaseUrl('https://api.example.com');
|
|
69
|
+
|
|
70
|
+
// Optional: customize auth settings
|
|
71
|
+
configureAuth({
|
|
72
|
+
defaultProvider: 'google',
|
|
73
|
+
autoRedirect: true,
|
|
74
|
+
refetchOnWindowFocus: false, // Disable session check on tab switch
|
|
75
|
+
refetchOnReconnect: true, // Re-check session on network reconnect
|
|
76
|
+
providers: {
|
|
77
|
+
google: {
|
|
78
|
+
loginPath: '/api/auth/google/login',
|
|
79
|
+
logoutPath: '/api/auth/logout',
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const queryClient = new QueryClient();
|
|
85
|
+
|
|
86
|
+
function App() {
|
|
87
|
+
return (
|
|
88
|
+
<QueryClientProvider client={queryClient}>
|
|
89
|
+
<AuthProvider
|
|
90
|
+
loadingComponent={<div>Loading...</div>}
|
|
91
|
+
onAuthChange={(status, user) => console.log('Auth:', status, user)}
|
|
92
|
+
>
|
|
93
|
+
<MyApp />
|
|
94
|
+
</AuthProvider>
|
|
95
|
+
</QueryClientProvider>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### useAuth Hook
|
|
101
|
+
|
|
102
|
+
Access authentication state and operations in any component:
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
import { useAuth } from '@ram_28/kf-ai-sdk';
|
|
106
|
+
|
|
107
|
+
function UserMenu() {
|
|
108
|
+
const { user, isAuthenticated, isLoading, logout, hasRole } = useAuth();
|
|
109
|
+
|
|
110
|
+
if (isLoading) return <div>Loading...</div>;
|
|
111
|
+
if (!isAuthenticated) return null;
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<div>
|
|
115
|
+
<span>Welcome, {user._name}</span>
|
|
116
|
+
<span>Role: {user.Role}</span>
|
|
117
|
+
|
|
118
|
+
{hasRole('Admin') && <a href="/admin">Admin Dashboard</a>}
|
|
119
|
+
|
|
120
|
+
<button onClick={() => logout({ redirectUrl: '/' })}>
|
|
121
|
+
Logout
|
|
122
|
+
</button>
|
|
123
|
+
</div>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### useAuth Return Values
|
|
129
|
+
|
|
130
|
+
```tsx
|
|
131
|
+
const {
|
|
132
|
+
// User state
|
|
133
|
+
user, // UserDetails | null
|
|
134
|
+
staticBaseUrl, // string | null
|
|
135
|
+
buildId, // string | null
|
|
136
|
+
status, // 'loading' | 'authenticated' | 'unauthenticated'
|
|
137
|
+
isAuthenticated, // boolean
|
|
138
|
+
isLoading, // boolean
|
|
139
|
+
|
|
140
|
+
// Operations
|
|
141
|
+
login, // (provider?, options?) => void
|
|
142
|
+
logout, // (options?) => Promise<void>
|
|
143
|
+
refreshSession, // () => Promise<SessionResponse | null>
|
|
144
|
+
hasRole, // (role: string) => boolean
|
|
145
|
+
hasAnyRole, // (roles: string[]) => boolean
|
|
146
|
+
|
|
147
|
+
// Error handling
|
|
148
|
+
error, // Error | null
|
|
149
|
+
clearError, // () => void
|
|
150
|
+
} = useAuth();
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Multiple Auth Providers
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
import { useAuth } from '@ram_28/kf-ai-sdk';
|
|
157
|
+
|
|
158
|
+
function LoginPage() {
|
|
159
|
+
const { login } = useAuth();
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<div>
|
|
163
|
+
<button onClick={() => login('google')}>
|
|
164
|
+
Continue with Google
|
|
165
|
+
</button>
|
|
166
|
+
<button onClick={() => login('microsoft')}>
|
|
167
|
+
Continue with Microsoft
|
|
168
|
+
</button>
|
|
169
|
+
</div>
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Protected Routes
|
|
175
|
+
|
|
176
|
+
```tsx
|
|
177
|
+
import { useAuth } from '@ram_28/kf-ai-sdk';
|
|
178
|
+
import { Navigate } from 'react-router-dom';
|
|
179
|
+
|
|
180
|
+
function ProtectedRoute({ children, requiredRoles }) {
|
|
181
|
+
const { isAuthenticated, isLoading, hasAnyRole } = useAuth();
|
|
182
|
+
|
|
183
|
+
if (isLoading) return <div>Loading...</div>;
|
|
184
|
+
if (!isAuthenticated) return <Navigate to="/login" />;
|
|
185
|
+
if (requiredRoles && !hasAnyRole(requiredRoles)) {
|
|
186
|
+
return <Navigate to="/unauthorized" />;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return children;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Usage
|
|
193
|
+
<ProtectedRoute requiredRoles={['Admin', 'Manager']}>
|
|
194
|
+
<AdminDashboard />
|
|
195
|
+
</ProtectedRoute>
|
|
196
|
+
```
|
|
197
|
+
|
|
44
198
|
## Hooks
|
|
45
199
|
|
|
46
200
|
### useTable
|
|
@@ -310,7 +464,7 @@ cn('text-red-500', condition && 'text-blue-500');
|
|
|
310
464
|
|
|
311
465
|
## Documentation
|
|
312
466
|
|
|
313
|
-
Detailed documentation for each
|
|
467
|
+
Detailed documentation for each feature:
|
|
314
468
|
|
|
315
469
|
- [useForm Documentation](./docs/useForm.md)
|
|
316
470
|
- [useTable Documentation](./docs/useTable.md)
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { AuthContextValue, AuthProviderProps } from "./types";
|
|
3
|
+
export declare function AuthProvider({ children, config: configOverride, onAuthChange, onError, loadingComponent, unauthenticatedComponent, skipInitialCheck, }: AuthProviderProps): React.ReactElement;
|
|
4
|
+
export declare function useAuthContext(): AuthContextValue;
|
|
5
|
+
//# sourceMappingURL=AuthProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthProvider.d.ts","sourceRoot":"","sources":["../../sdk/auth/AuthProvider.tsx"],"names":[],"mappings":"AAKA,OAAO,KAQN,MAAM,OAAO,CAAC;AAGf,OAAO,KAAK,EACV,gBAAgB,EAChB,iBAAiB,EAOlB,MAAM,SAAS,CAAC;AAsBjB,wBAAgB,YAAY,CAAC,EAC3B,QAAQ,EACR,MAAM,EAAE,cAAc,EACtB,YAAY,EACZ,OAAO,EACP,gBAAgB,EAChB,wBAAwB,EACxB,gBAAwB,GACzB,EAAE,iBAAiB,GAAG,KAAK,CAAC,YAAY,CAiNxC;AAMD,wBAAgB,cAAc,IAAI,gBAAgB,CAMjD"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { SessionResponse, AuthProviderName, LoginOptions, LogoutOptions } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Custom error class for authentication errors
|
|
4
|
+
*/
|
|
5
|
+
export declare class AuthenticationError extends Error {
|
|
6
|
+
readonly statusCode: number;
|
|
7
|
+
constructor(message: string, statusCode: number);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Fetch current session from the server
|
|
11
|
+
* Calls the session endpoint (default: /api/id)
|
|
12
|
+
*
|
|
13
|
+
* @throws AuthenticationError if session check fails or user is not authenticated
|
|
14
|
+
*/
|
|
15
|
+
export declare function fetchSession(): Promise<SessionResponse>;
|
|
16
|
+
/**
|
|
17
|
+
* Initiates OAuth login flow by redirecting to the auth provider.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* This function redirects the browser and never resolves.
|
|
21
|
+
* Any code after calling this function will not execute.
|
|
22
|
+
*
|
|
23
|
+
* @param provider - OAuth provider to use (defaults to config.defaultProvider)
|
|
24
|
+
* @param options - Login options including callback URL and custom params
|
|
25
|
+
* @returns Promise that never resolves (browser redirects away)
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // Correct usage - no code after login()
|
|
30
|
+
* function handleLoginClick() {
|
|
31
|
+
* login('google');
|
|
32
|
+
* // Don't put code here - it won't run
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function initiateLogin(provider?: AuthProviderName, options?: LoginOptions): Promise<never>;
|
|
37
|
+
/**
|
|
38
|
+
* Logout the current user
|
|
39
|
+
* Optionally calls the logout endpoint before clearing client state
|
|
40
|
+
*/
|
|
41
|
+
export declare function performLogout(options?: LogoutOptions): Promise<void>;
|
|
42
|
+
//# sourceMappingURL=authClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authClient.d.ts","sourceRoot":"","sources":["../../sdk/auth/authClient.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACd,MAAM,SAAS,CAAC;AAQjB;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;IAC5C,SAAgB,UAAU,EAAE,MAAM,CAAC;gBAEvB,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;CAKhD;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,IAAI,OAAO,CAAC,eAAe,CAAC,CAuB7D;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAC3B,QAAQ,CAAC,EAAE,gBAAgB,EAC3B,OAAO,CAAC,EAAE,YAAY,GACrB,OAAO,CAAC,KAAK,CAAC,CA0DhB;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAyB1E"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { AuthConfig, AuthProviderName, AuthEndpointConfig } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Configure authentication settings globally
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* configureAuth({
|
|
7
|
+
* defaultProvider: "google",
|
|
8
|
+
* autoRedirect: true,
|
|
9
|
+
* providers: {
|
|
10
|
+
* google: { loginPath: "/api/auth/google/login" },
|
|
11
|
+
* microsoft: { loginPath: "/api/auth/microsoft/login" },
|
|
12
|
+
* },
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare function configureAuth(config: Partial<AuthConfig>): void;
|
|
17
|
+
/**
|
|
18
|
+
* Add or update an auth provider configuration
|
|
19
|
+
*/
|
|
20
|
+
export declare function setAuthProvider(provider: AuthProviderName, config: AuthEndpointConfig): void;
|
|
21
|
+
/**
|
|
22
|
+
* Get current auth configuration
|
|
23
|
+
*/
|
|
24
|
+
export declare function getAuthConfig(): Readonly<AuthConfig>;
|
|
25
|
+
/**
|
|
26
|
+
* Get the base URL for auth endpoints
|
|
27
|
+
* Falls back to API base URL if not explicitly set
|
|
28
|
+
*/
|
|
29
|
+
export declare function getAuthBaseUrl(): string;
|
|
30
|
+
/**
|
|
31
|
+
* Get endpoint configuration for a specific provider
|
|
32
|
+
*/
|
|
33
|
+
export declare function getProviderConfig(provider: AuthProviderName): AuthEndpointConfig | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Reset auth configuration to defaults
|
|
36
|
+
*/
|
|
37
|
+
export declare function resetAuthConfig(): void;
|
|
38
|
+
//# sourceMappingURL=authConfig.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authConfig.d.ts","sourceRoot":"","sources":["../../sdk/auth/authConfig.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AA+BhF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,IAAI,CAa/D;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,gBAAgB,EAC1B,MAAM,EAAE,kBAAkB,GACzB,IAAI,CAEN;AAED;;GAEG;AACH,wBAAgB,aAAa,IAAI,QAAQ,CAAC,UAAU,CAAC,CAEpD;AAED;;;GAGG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAEvC;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,gBAAgB,GACzB,kBAAkB,GAAG,SAAS,CAEhC;AAED;;GAEG;AACH,wBAAgB,eAAe,IAAI,IAAI,CAEtC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { AuthProvider } from "./AuthProvider";
|
|
2
|
+
export { useAuth } from "./useAuth";
|
|
3
|
+
export { configureAuth, setAuthProvider, getAuthConfig, getAuthBaseUrl, resetAuthConfig, } from "./authConfig";
|
|
4
|
+
export { fetchSession, initiateLogin, performLogout, AuthenticationError, } from "./authClient";
|
|
5
|
+
export type { UserDetails, SessionResponse, AuthStatus, AuthConfig, AuthProviderName, AuthEndpointConfig, AuthProviderProps, UseAuthReturn, LoginOptions, LogoutOptions, } from "./types";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../sdk/auth/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,OAAO,EACL,aAAa,EACb,eAAe,EACf,aAAa,EACb,cAAc,EACd,eAAe,GAChB,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,YAAY,EACZ,aAAa,EACb,aAAa,EACb,mBAAmB,GACpB,MAAM,cAAc,CAAC;AAGtB,YAAY,EACV,WAAW,EACX,eAAe,EACf,UAAU,EACV,UAAU,EACV,gBAAgB,EAChB,kBAAkB,EAClB,iBAAiB,EACjB,aAAa,EACb,YAAY,EACZ,aAAa,GACd,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User details returned from the session endpoint
|
|
3
|
+
*/
|
|
4
|
+
export interface UserDetails {
|
|
5
|
+
_id: string;
|
|
6
|
+
_name: string;
|
|
7
|
+
Role: string;
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Session response from /api/id endpoint
|
|
12
|
+
*/
|
|
13
|
+
export interface SessionResponse {
|
|
14
|
+
userDetails: UserDetails;
|
|
15
|
+
staticBaseUrl: string;
|
|
16
|
+
buildId: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Authentication status
|
|
20
|
+
*/
|
|
21
|
+
export type AuthStatus = "loading" | "authenticated" | "unauthenticated";
|
|
22
|
+
/**
|
|
23
|
+
* Authentication provider type (extensible for multiple OAuth providers)
|
|
24
|
+
*/
|
|
25
|
+
export type AuthProviderName = "google" | "microsoft" | "github" | "custom";
|
|
26
|
+
/**
|
|
27
|
+
* Auth endpoint configuration for a specific provider
|
|
28
|
+
*/
|
|
29
|
+
export interface AuthEndpointConfig {
|
|
30
|
+
/** Login endpoint path (e.g., "/api/auth/google/login") */
|
|
31
|
+
loginPath: string;
|
|
32
|
+
/** Optional logout endpoint path */
|
|
33
|
+
logoutPath?: string;
|
|
34
|
+
/** Optional callback endpoint path */
|
|
35
|
+
callbackPath?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Global authentication configuration
|
|
39
|
+
*/
|
|
40
|
+
export interface AuthConfig {
|
|
41
|
+
/** Base URL for auth endpoints (defaults to apiBaseUrl) */
|
|
42
|
+
baseUrl?: string;
|
|
43
|
+
/** Session check endpoint (default: "/api/id") */
|
|
44
|
+
sessionEndpoint: string;
|
|
45
|
+
/** Auth provider configurations */
|
|
46
|
+
providers: Partial<Record<AuthProviderName, AuthEndpointConfig>>;
|
|
47
|
+
/** Default provider to use for login */
|
|
48
|
+
defaultProvider: AuthProviderName;
|
|
49
|
+
/** Auto-redirect to login when unauthenticated */
|
|
50
|
+
autoRedirect: boolean;
|
|
51
|
+
/** Custom redirect URL (if not using provider's login path) */
|
|
52
|
+
loginRedirectUrl?: string;
|
|
53
|
+
/** URL to redirect after successful login */
|
|
54
|
+
callbackUrl?: string;
|
|
55
|
+
/** Session check interval in milliseconds (0 to disable) */
|
|
56
|
+
sessionCheckInterval: number;
|
|
57
|
+
/** Retry configuration for session check */
|
|
58
|
+
retry: {
|
|
59
|
+
count: number;
|
|
60
|
+
delay: number;
|
|
61
|
+
};
|
|
62
|
+
/** React Query stale time for session data */
|
|
63
|
+
staleTime: number;
|
|
64
|
+
/** Refetch session when window regains focus (default: true) */
|
|
65
|
+
refetchOnWindowFocus?: boolean;
|
|
66
|
+
/** Refetch session when network reconnects (default: true) */
|
|
67
|
+
refetchOnReconnect?: boolean;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* AuthProvider component props
|
|
71
|
+
*/
|
|
72
|
+
export interface AuthProviderProps {
|
|
73
|
+
children: React.ReactNode;
|
|
74
|
+
/** Override global config for this provider instance */
|
|
75
|
+
config?: Partial<AuthConfig>;
|
|
76
|
+
/** Callback when authentication status changes */
|
|
77
|
+
onAuthChange?: (status: AuthStatus, user: UserDetails | null) => void;
|
|
78
|
+
/** Callback on authentication error */
|
|
79
|
+
onError?: (error: Error) => void;
|
|
80
|
+
/** Custom loading component */
|
|
81
|
+
loadingComponent?: React.ReactNode;
|
|
82
|
+
/** Custom unauthenticated component (shown when autoRedirect is false) */
|
|
83
|
+
unauthenticatedComponent?: React.ReactNode;
|
|
84
|
+
/** Disable automatic session check on mount */
|
|
85
|
+
skipInitialCheck?: boolean;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Options for login operation
|
|
89
|
+
*/
|
|
90
|
+
export interface LoginOptions {
|
|
91
|
+
/** URL to redirect after successful login */
|
|
92
|
+
callbackUrl?: string;
|
|
93
|
+
/** Additional query parameters for login URL */
|
|
94
|
+
params?: Record<string, string>;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Options for logout operation
|
|
98
|
+
*/
|
|
99
|
+
export interface LogoutOptions {
|
|
100
|
+
/** URL to redirect after logout */
|
|
101
|
+
redirectUrl?: string;
|
|
102
|
+
/** Whether to call logout endpoint (default: true) */
|
|
103
|
+
callLogoutEndpoint?: boolean;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Return type for useAuth hook
|
|
107
|
+
*/
|
|
108
|
+
export interface UseAuthReturn {
|
|
109
|
+
/** Current authenticated user (null if not authenticated) */
|
|
110
|
+
user: UserDetails | null;
|
|
111
|
+
/** Static base URL from session */
|
|
112
|
+
staticBaseUrl: string | null;
|
|
113
|
+
/** Build ID from session */
|
|
114
|
+
buildId: string | null;
|
|
115
|
+
/** Current authentication status */
|
|
116
|
+
status: AuthStatus;
|
|
117
|
+
/** Convenience boolean for authenticated state */
|
|
118
|
+
isAuthenticated: boolean;
|
|
119
|
+
/** Convenience boolean for loading state */
|
|
120
|
+
isLoading: boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Initiate login flow
|
|
123
|
+
* @param provider - Auth provider to use (defaults to configured default)
|
|
124
|
+
* @param options - Additional options for login
|
|
125
|
+
*/
|
|
126
|
+
login: (provider?: AuthProviderName, options?: LoginOptions) => void;
|
|
127
|
+
/**
|
|
128
|
+
* Logout the current user
|
|
129
|
+
* @param options - Additional options for logout
|
|
130
|
+
*/
|
|
131
|
+
logout: (options?: LogoutOptions) => Promise<void>;
|
|
132
|
+
/**
|
|
133
|
+
* Manually refresh the session
|
|
134
|
+
*/
|
|
135
|
+
refreshSession: () => Promise<SessionResponse | null>;
|
|
136
|
+
/**
|
|
137
|
+
* Check if user has a specific role
|
|
138
|
+
*/
|
|
139
|
+
hasRole: (role: string) => boolean;
|
|
140
|
+
/**
|
|
141
|
+
* Check if user has any of the specified roles
|
|
142
|
+
*/
|
|
143
|
+
hasAnyRole: (roles: string[]) => boolean;
|
|
144
|
+
/** Last authentication error */
|
|
145
|
+
error: Error | null;
|
|
146
|
+
/** Clear the current error */
|
|
147
|
+
clearError: () => void;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Auth context value (internal)
|
|
151
|
+
*/
|
|
152
|
+
export interface AuthContextValue extends UseAuthReturn {
|
|
153
|
+
/** Internal: force re-check session */
|
|
154
|
+
_forceCheck: () => void;
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../sdk/auth/types.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,WAAW,CAAC;IACzB,aAAa,EAAE,MAAM,CAAC;IACtB,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IAAI,CAAC;IAErE;;;OAGG;IACH,MAAM,EAAE,CAAC,OAAO,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnD;;OAEG;IACH,cAAc,EAAE,MAAM,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC;IAEtD;;OAEG;IACH,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC;IAEnC;;OAEG;IACH,UAAU,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC;IAMzC,gCAAgC;IAChC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IAEpB,8BAA8B;IAC9B,UAAU,EAAE,MAAM,IAAI,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAiB,SAAQ,aAAa;IACrD,uCAAuC;IACvC,WAAW,EAAE,MAAM,IAAI,CAAC;CACzB"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { UseAuthReturn } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Hook to access authentication state and operations
|
|
4
|
+
*
|
|
5
|
+
* Must be used within an AuthProvider component.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* function UserProfile() {
|
|
10
|
+
* const { user, isAuthenticated, logout } = useAuth();
|
|
11
|
+
*
|
|
12
|
+
* if (!isAuthenticated) {
|
|
13
|
+
* return <div>Please log in</div>;
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* return (
|
|
17
|
+
* <div>
|
|
18
|
+
* <h1>Welcome, {user._name}</h1>
|
|
19
|
+
* <p>Role: {user.Role}</p>
|
|
20
|
+
* <button onClick={() => logout()}>Logout</button>
|
|
21
|
+
* </div>
|
|
22
|
+
* );
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function useAuth(): UseAuthReturn;
|
|
27
|
+
//# sourceMappingURL=useAuth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../sdk/auth/useAuth.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAG7C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,OAAO,IAAI,aAAa,CAmCvC"}
|