@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
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// AUTHENTICATION TYPE DEFINITIONS
|
|
3
|
+
// ============================================================
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* User details returned from the session endpoint
|
|
7
|
+
*/
|
|
8
|
+
export interface UserDetails {
|
|
9
|
+
_id: string;
|
|
10
|
+
_name: string;
|
|
11
|
+
Role: string;
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Session response from /api/id endpoint
|
|
17
|
+
*/
|
|
18
|
+
export interface SessionResponse {
|
|
19
|
+
userDetails: UserDetails;
|
|
20
|
+
staticBaseUrl: string;
|
|
21
|
+
buildId: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Authentication status
|
|
26
|
+
*/
|
|
27
|
+
export type AuthStatus = "loading" | "authenticated" | "unauthenticated";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Authentication provider type (extensible for multiple OAuth providers)
|
|
31
|
+
*/
|
|
32
|
+
export type AuthProviderName = "google" | "microsoft" | "github" | "custom";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Auth endpoint configuration for a specific provider
|
|
36
|
+
*/
|
|
37
|
+
export interface AuthEndpointConfig {
|
|
38
|
+
/** Login endpoint path (e.g., "/api/auth/google/login") */
|
|
39
|
+
loginPath: string;
|
|
40
|
+
/** Optional logout endpoint path */
|
|
41
|
+
logoutPath?: string;
|
|
42
|
+
/** Optional callback endpoint path */
|
|
43
|
+
callbackPath?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Global authentication configuration
|
|
48
|
+
*/
|
|
49
|
+
export interface AuthConfig {
|
|
50
|
+
/** Base URL for auth endpoints (defaults to apiBaseUrl) */
|
|
51
|
+
baseUrl?: string;
|
|
52
|
+
|
|
53
|
+
/** Session check endpoint (default: "/api/id") */
|
|
54
|
+
sessionEndpoint: string;
|
|
55
|
+
|
|
56
|
+
/** Auth provider configurations */
|
|
57
|
+
providers: Partial<Record<AuthProviderName, AuthEndpointConfig>>;
|
|
58
|
+
|
|
59
|
+
/** Default provider to use for login */
|
|
60
|
+
defaultProvider: AuthProviderName;
|
|
61
|
+
|
|
62
|
+
/** Auto-redirect to login when unauthenticated */
|
|
63
|
+
autoRedirect: boolean;
|
|
64
|
+
|
|
65
|
+
/** Custom redirect URL (if not using provider's login path) */
|
|
66
|
+
loginRedirectUrl?: string;
|
|
67
|
+
|
|
68
|
+
/** URL to redirect after successful login */
|
|
69
|
+
callbackUrl?: string;
|
|
70
|
+
|
|
71
|
+
/** Session check interval in milliseconds (0 to disable) */
|
|
72
|
+
sessionCheckInterval: number;
|
|
73
|
+
|
|
74
|
+
/** Retry configuration for session check */
|
|
75
|
+
retry: {
|
|
76
|
+
count: number;
|
|
77
|
+
delay: number;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/** React Query stale time for session data */
|
|
81
|
+
staleTime: number;
|
|
82
|
+
|
|
83
|
+
/** Refetch session when window regains focus (default: true) */
|
|
84
|
+
refetchOnWindowFocus?: boolean;
|
|
85
|
+
|
|
86
|
+
/** Refetch session when network reconnects (default: true) */
|
|
87
|
+
refetchOnReconnect?: boolean;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* AuthProvider component props
|
|
92
|
+
*/
|
|
93
|
+
export interface AuthProviderProps {
|
|
94
|
+
children: React.ReactNode;
|
|
95
|
+
|
|
96
|
+
/** Override global config for this provider instance */
|
|
97
|
+
config?: Partial<AuthConfig>;
|
|
98
|
+
|
|
99
|
+
/** Callback when authentication status changes */
|
|
100
|
+
onAuthChange?: (status: AuthStatus, user: UserDetails | null) => void;
|
|
101
|
+
|
|
102
|
+
/** Callback on authentication error */
|
|
103
|
+
onError?: (error: Error) => void;
|
|
104
|
+
|
|
105
|
+
/** Custom loading component */
|
|
106
|
+
loadingComponent?: React.ReactNode;
|
|
107
|
+
|
|
108
|
+
/** Custom unauthenticated component (shown when autoRedirect is false) */
|
|
109
|
+
unauthenticatedComponent?: React.ReactNode;
|
|
110
|
+
|
|
111
|
+
/** Disable automatic session check on mount */
|
|
112
|
+
skipInitialCheck?: boolean;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Options for login operation
|
|
117
|
+
*/
|
|
118
|
+
export interface LoginOptions {
|
|
119
|
+
/** URL to redirect after successful login */
|
|
120
|
+
callbackUrl?: string;
|
|
121
|
+
/** Additional query parameters for login URL */
|
|
122
|
+
params?: Record<string, string>;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Options for logout operation
|
|
127
|
+
*/
|
|
128
|
+
export interface LogoutOptions {
|
|
129
|
+
/** URL to redirect after logout */
|
|
130
|
+
redirectUrl?: string;
|
|
131
|
+
/** Whether to call logout endpoint (default: true) */
|
|
132
|
+
callLogoutEndpoint?: boolean;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Return type for useAuth hook
|
|
137
|
+
*/
|
|
138
|
+
export interface UseAuthReturn {
|
|
139
|
+
// ============================================================
|
|
140
|
+
// USER STATE
|
|
141
|
+
// ============================================================
|
|
142
|
+
|
|
143
|
+
/** Current authenticated user (null if not authenticated) */
|
|
144
|
+
user: UserDetails | null;
|
|
145
|
+
|
|
146
|
+
/** Static base URL from session */
|
|
147
|
+
staticBaseUrl: string | null;
|
|
148
|
+
|
|
149
|
+
/** Build ID from session */
|
|
150
|
+
buildId: string | null;
|
|
151
|
+
|
|
152
|
+
/** Current authentication status */
|
|
153
|
+
status: AuthStatus;
|
|
154
|
+
|
|
155
|
+
/** Convenience boolean for authenticated state */
|
|
156
|
+
isAuthenticated: boolean;
|
|
157
|
+
|
|
158
|
+
/** Convenience boolean for loading state */
|
|
159
|
+
isLoading: boolean;
|
|
160
|
+
|
|
161
|
+
// ============================================================
|
|
162
|
+
// AUTH OPERATIONS
|
|
163
|
+
// ============================================================
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Initiate login flow
|
|
167
|
+
* @param provider - Auth provider to use (defaults to configured default)
|
|
168
|
+
* @param options - Additional options for login
|
|
169
|
+
*/
|
|
170
|
+
login: (provider?: AuthProviderName, options?: LoginOptions) => void;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Logout the current user
|
|
174
|
+
* @param options - Additional options for logout
|
|
175
|
+
*/
|
|
176
|
+
logout: (options?: LogoutOptions) => Promise<void>;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Manually refresh the session
|
|
180
|
+
*/
|
|
181
|
+
refreshSession: () => Promise<SessionResponse | null>;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Check if user has a specific role
|
|
185
|
+
*/
|
|
186
|
+
hasRole: (role: string) => boolean;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Check if user has any of the specified roles
|
|
190
|
+
*/
|
|
191
|
+
hasAnyRole: (roles: string[]) => boolean;
|
|
192
|
+
|
|
193
|
+
// ============================================================
|
|
194
|
+
// ERROR STATE
|
|
195
|
+
// ============================================================
|
|
196
|
+
|
|
197
|
+
/** Last authentication error */
|
|
198
|
+
error: Error | null;
|
|
199
|
+
|
|
200
|
+
/** Clear the current error */
|
|
201
|
+
clearError: () => void;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Auth context value (internal)
|
|
206
|
+
*/
|
|
207
|
+
export interface AuthContextValue extends UseAuthReturn {
|
|
208
|
+
/** Internal: force re-check session */
|
|
209
|
+
_forceCheck: () => void;
|
|
210
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// USE AUTH HOOK
|
|
3
|
+
// ============================================================
|
|
4
|
+
// Main hook for consuming authentication state
|
|
5
|
+
|
|
6
|
+
import { useMemo } from "react";
|
|
7
|
+
import type { UseAuthReturn } from "./types";
|
|
8
|
+
import { useAuthContext } from "./AuthProvider";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Hook to access authentication state and operations
|
|
12
|
+
*
|
|
13
|
+
* Must be used within an AuthProvider component.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* function UserProfile() {
|
|
18
|
+
* const { user, isAuthenticated, logout } = useAuth();
|
|
19
|
+
*
|
|
20
|
+
* if (!isAuthenticated) {
|
|
21
|
+
* return <div>Please log in</div>;
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* return (
|
|
25
|
+
* <div>
|
|
26
|
+
* <h1>Welcome, {user._name}</h1>
|
|
27
|
+
* <p>Role: {user.Role}</p>
|
|
28
|
+
* <button onClick={() => logout()}>Logout</button>
|
|
29
|
+
* </div>
|
|
30
|
+
* );
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function useAuth(): UseAuthReturn {
|
|
35
|
+
const context = useAuthContext();
|
|
36
|
+
|
|
37
|
+
return useMemo(
|
|
38
|
+
() => ({
|
|
39
|
+
user: context.user,
|
|
40
|
+
staticBaseUrl: context.staticBaseUrl,
|
|
41
|
+
buildId: context.buildId,
|
|
42
|
+
status: context.status,
|
|
43
|
+
isAuthenticated: context.isAuthenticated,
|
|
44
|
+
isLoading: context.isLoading,
|
|
45
|
+
login: context.login,
|
|
46
|
+
logout: context.logout,
|
|
47
|
+
refreshSession: context.refreshSession,
|
|
48
|
+
hasRole: context.hasRole,
|
|
49
|
+
hasAnyRole: context.hasAnyRole,
|
|
50
|
+
error: context.error,
|
|
51
|
+
clearError: context.clearError,
|
|
52
|
+
}),
|
|
53
|
+
[
|
|
54
|
+
context.user,
|
|
55
|
+
context.staticBaseUrl,
|
|
56
|
+
context.buildId,
|
|
57
|
+
context.status,
|
|
58
|
+
context.isAuthenticated,
|
|
59
|
+
context.isLoading,
|
|
60
|
+
context.login,
|
|
61
|
+
context.logout,
|
|
62
|
+
context.refreshSession,
|
|
63
|
+
context.hasRole,
|
|
64
|
+
context.hasAnyRole,
|
|
65
|
+
context.error,
|
|
66
|
+
context.clearError,
|
|
67
|
+
]
|
|
68
|
+
);
|
|
69
|
+
}
|