@ram_28/kf-ai-sdk 1.0.1 → 1.0.2

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.
@@ -0,0 +1,204 @@
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
+
84
+ /**
85
+ * AuthProvider component props
86
+ */
87
+ export interface AuthProviderProps {
88
+ children: React.ReactNode;
89
+
90
+ /** Override global config for this provider instance */
91
+ config?: Partial<AuthConfig>;
92
+
93
+ /** Callback when authentication status changes */
94
+ onAuthChange?: (status: AuthStatus, user: UserDetails | null) => void;
95
+
96
+ /** Callback on authentication error */
97
+ onError?: (error: Error) => void;
98
+
99
+ /** Custom loading component */
100
+ loadingComponent?: React.ReactNode;
101
+
102
+ /** Custom unauthenticated component (shown when autoRedirect is false) */
103
+ unauthenticatedComponent?: React.ReactNode;
104
+
105
+ /** Disable automatic session check on mount */
106
+ skipInitialCheck?: boolean;
107
+ }
108
+
109
+ /**
110
+ * Options for login operation
111
+ */
112
+ export interface LoginOptions {
113
+ /** URL to redirect after successful login */
114
+ callbackUrl?: string;
115
+ /** Additional query parameters for login URL */
116
+ params?: Record<string, string>;
117
+ }
118
+
119
+ /**
120
+ * Options for logout operation
121
+ */
122
+ export interface LogoutOptions {
123
+ /** URL to redirect after logout */
124
+ redirectUrl?: string;
125
+ /** Whether to call logout endpoint (default: true) */
126
+ callLogoutEndpoint?: boolean;
127
+ }
128
+
129
+ /**
130
+ * Return type for useAuth hook
131
+ */
132
+ export interface UseAuthReturn {
133
+ // ============================================================
134
+ // USER STATE
135
+ // ============================================================
136
+
137
+ /** Current authenticated user (null if not authenticated) */
138
+ user: UserDetails | null;
139
+
140
+ /** Static base URL from session */
141
+ staticBaseUrl: string | null;
142
+
143
+ /** Build ID from session */
144
+ buildId: string | null;
145
+
146
+ /** Current authentication status */
147
+ status: AuthStatus;
148
+
149
+ /** Convenience boolean for authenticated state */
150
+ isAuthenticated: boolean;
151
+
152
+ /** Convenience boolean for loading state */
153
+ isLoading: boolean;
154
+
155
+ // ============================================================
156
+ // AUTH OPERATIONS
157
+ // ============================================================
158
+
159
+ /**
160
+ * Initiate login flow
161
+ * @param provider - Auth provider to use (defaults to configured default)
162
+ * @param options - Additional options for login
163
+ */
164
+ login: (provider?: AuthProviderName, options?: LoginOptions) => void;
165
+
166
+ /**
167
+ * Logout the current user
168
+ * @param options - Additional options for logout
169
+ */
170
+ logout: (options?: LogoutOptions) => Promise<void>;
171
+
172
+ /**
173
+ * Manually refresh the session
174
+ */
175
+ refreshSession: () => Promise<SessionResponse | null>;
176
+
177
+ /**
178
+ * Check if user has a specific role
179
+ */
180
+ hasRole: (role: string) => boolean;
181
+
182
+ /**
183
+ * Check if user has any of the specified roles
184
+ */
185
+ hasAnyRole: (roles: string[]) => boolean;
186
+
187
+ // ============================================================
188
+ // ERROR STATE
189
+ // ============================================================
190
+
191
+ /** Last authentication error */
192
+ error: Error | null;
193
+
194
+ /** Clear the current error */
195
+ clearError: () => void;
196
+ }
197
+
198
+ /**
199
+ * Auth context value (internal)
200
+ */
201
+ export interface AuthContextValue extends UseAuthReturn {
202
+ /** Internal: force re-check session */
203
+ _forceCheck: () => void;
204
+ }
@@ -0,0 +1,67 @@
1
+ // ============================================================
2
+ // USE AUTH HOOK
3
+ // ============================================================
4
+ // Main hook for consuming authentication state
5
+
6
+ import type { UseAuthReturn } from "./types";
7
+ import { useAuthContext } from "./AuthProvider";
8
+
9
+ /**
10
+ * Hook to access authentication state and operations
11
+ *
12
+ * Must be used within an AuthProvider component.
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * function UserProfile() {
17
+ * const { user, isAuthenticated, logout } = useAuth();
18
+ *
19
+ * if (!isAuthenticated) {
20
+ * return <div>Please log in</div>;
21
+ * }
22
+ *
23
+ * return (
24
+ * <div>
25
+ * <h1>Welcome, {user._name}</h1>
26
+ * <p>Role: {user.Role}</p>
27
+ * <button onClick={() => logout()}>Logout</button>
28
+ * </div>
29
+ * );
30
+ * }
31
+ * ```
32
+ */
33
+ export function useAuth(): UseAuthReturn {
34
+ const context = useAuthContext();
35
+
36
+ const {
37
+ user,
38
+ staticBaseUrl,
39
+ buildId,
40
+ status,
41
+ isAuthenticated,
42
+ isLoading,
43
+ login,
44
+ logout,
45
+ refreshSession,
46
+ hasRole,
47
+ hasAnyRole,
48
+ error,
49
+ clearError,
50
+ } = context;
51
+
52
+ return {
53
+ user,
54
+ staticBaseUrl,
55
+ buildId,
56
+ status,
57
+ isAuthenticated,
58
+ isLoading,
59
+ login,
60
+ logout,
61
+ refreshSession,
62
+ hasRole,
63
+ hasAnyRole,
64
+ error,
65
+ clearError,
66
+ };
67
+ }
package/sdk/index.ts CHANGED
@@ -10,4 +10,7 @@ export * from './api';
10
10
  export * from './utils';
11
11
 
12
12
  // Components
13
- export * from './components';
13
+ export * from './components';
14
+
15
+ // Authentication
16
+ export * from './auth';