@demokit-ai/auth 0.0.1

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,337 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+ import { SupabaseClient } from '@supabase/supabase-js';
4
+
5
+ /**
6
+ * Represents an authenticated user
7
+ */
8
+ interface AuthUser {
9
+ /**
10
+ * Unique identifier for the user
11
+ */
12
+ id: string;
13
+ /**
14
+ * User's email address
15
+ */
16
+ email: string | null;
17
+ /**
18
+ * User's display name
19
+ */
20
+ name?: string | null;
21
+ /**
22
+ * URL to user's avatar image
23
+ */
24
+ avatarUrl?: string | null;
25
+ /**
26
+ * Additional user metadata
27
+ */
28
+ metadata?: Record<string, unknown>;
29
+ }
30
+ /**
31
+ * Represents an authenticated session
32
+ */
33
+ interface AuthSession {
34
+ /**
35
+ * Access token for API requests
36
+ */
37
+ accessToken: string;
38
+ /**
39
+ * Refresh token for obtaining new access tokens
40
+ */
41
+ refreshToken?: string;
42
+ /**
43
+ * Unix timestamp when the access token expires
44
+ */
45
+ expiresAt?: number;
46
+ /**
47
+ * The authenticated user
48
+ */
49
+ user: AuthUser;
50
+ }
51
+ /**
52
+ * Supported OAuth providers
53
+ */
54
+ type OAuthProvider = 'github' | 'google' | 'gitlab';
55
+ /**
56
+ * Options for email/password sign in
57
+ */
58
+ interface SignInOptions {
59
+ email: string;
60
+ password: string;
61
+ }
62
+ /**
63
+ * Options for email/password sign up
64
+ */
65
+ interface SignUpOptions {
66
+ email: string;
67
+ password: string;
68
+ metadata?: Record<string, unknown>;
69
+ }
70
+ /**
71
+ * Result of an authentication operation
72
+ */
73
+ interface AuthResult {
74
+ /**
75
+ * The authenticated user, if successful
76
+ */
77
+ user: AuthUser | null;
78
+ /**
79
+ * The session, if successful
80
+ */
81
+ session: AuthSession | null;
82
+ /**
83
+ * Error details, if the operation failed
84
+ */
85
+ error: AuthError | null;
86
+ }
87
+ /**
88
+ * Authentication error details
89
+ */
90
+ interface AuthError {
91
+ /**
92
+ * Error code for programmatic handling
93
+ */
94
+ code: string;
95
+ /**
96
+ * Human-readable error message
97
+ */
98
+ message: string;
99
+ }
100
+ /**
101
+ * Events emitted by the auth state change listener
102
+ */
103
+ type AuthStateChangeEvent = 'SIGNED_IN' | 'SIGNED_OUT' | 'TOKEN_REFRESHED' | 'USER_UPDATED' | 'PASSWORD_RECOVERY' | 'INITIAL_SESSION';
104
+ /**
105
+ * Callback for auth state changes
106
+ */
107
+ type AuthStateChangeCallback = (event: AuthStateChangeEvent, session: AuthSession | null) => void;
108
+ /**
109
+ * Abstract authentication provider interface
110
+ *
111
+ * Implement this interface to add support for different auth providers.
112
+ * The default implementation uses Supabase Auth.
113
+ */
114
+ interface AuthProvider$1 {
115
+ /**
116
+ * Get the current session, if any
117
+ */
118
+ getSession(): Promise<AuthSession | null>;
119
+ /**
120
+ * Get the current user, if authenticated
121
+ */
122
+ getUser(): Promise<AuthUser | null>;
123
+ /**
124
+ * Sign in with email and password
125
+ */
126
+ signIn(options: SignInOptions): Promise<AuthResult>;
127
+ /**
128
+ * Sign up with email and password
129
+ */
130
+ signUp(options: SignUpOptions): Promise<AuthResult>;
131
+ /**
132
+ * Sign out the current user
133
+ */
134
+ signOut(): Promise<void>;
135
+ /**
136
+ * Sign in with an OAuth provider
137
+ * This will redirect to the provider's login page
138
+ */
139
+ signInWithOAuth(provider: OAuthProvider, redirectTo?: string): Promise<void>;
140
+ /**
141
+ * Send a password reset email
142
+ */
143
+ resetPasswordForEmail(email: string, redirectTo?: string): Promise<void>;
144
+ /**
145
+ * Update the current user's password
146
+ */
147
+ updatePassword(newPassword: string): Promise<void>;
148
+ /**
149
+ * Subscribe to auth state changes
150
+ * @returns Unsubscribe function
151
+ */
152
+ onAuthStateChange(callback: AuthStateChangeCallback): () => void;
153
+ }
154
+
155
+ /**
156
+ * Context value provided by AuthProvider
157
+ */
158
+ interface AuthContextValue {
159
+ /**
160
+ * The current authenticated user, or null if not authenticated
161
+ */
162
+ user: AuthUser | null;
163
+ /**
164
+ * The current session, or null if not authenticated
165
+ */
166
+ session: AuthSession | null;
167
+ /**
168
+ * Whether the auth state is still loading
169
+ */
170
+ isLoading: boolean;
171
+ /**
172
+ * Whether the user is authenticated
173
+ */
174
+ isAuthenticated: boolean;
175
+ /**
176
+ * Sign in with email and password
177
+ */
178
+ signIn: (email: string, password: string) => Promise<AuthResult>;
179
+ /**
180
+ * Sign up with email and password
181
+ */
182
+ signUp: (email: string, password: string, metadata?: Record<string, unknown>) => Promise<AuthResult>;
183
+ /**
184
+ * Sign out the current user
185
+ */
186
+ signOut: () => Promise<void>;
187
+ /**
188
+ * Sign in with an OAuth provider
189
+ */
190
+ signInWithOAuth: (provider: OAuthProvider, redirectTo?: string) => Promise<void>;
191
+ /**
192
+ * Send a password reset email
193
+ */
194
+ resetPasswordForEmail: (email: string, redirectTo?: string) => Promise<void>;
195
+ }
196
+ /**
197
+ * Props for the AuthProvider component
198
+ */
199
+ interface AuthProviderProps {
200
+ /**
201
+ * Child components to render
202
+ */
203
+ children: ReactNode;
204
+ /**
205
+ * The auth provider implementation to use
206
+ */
207
+ provider: AuthProvider$1;
208
+ }
209
+ /**
210
+ * Provider component that manages authentication state
211
+ *
212
+ * @example
213
+ * import { AuthProvider, createSupabaseAuthProvider } from '@demokit-ai/auth'
214
+ *
215
+ * const authProvider = createSupabaseAuthProvider({
216
+ * supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL!,
217
+ * supabaseAnonKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
218
+ * })
219
+ *
220
+ * function App() {
221
+ * return (
222
+ * <AuthProvider provider={authProvider}>
223
+ * <YourApp />
224
+ * </AuthProvider>
225
+ * )
226
+ * }
227
+ */
228
+ declare function AuthProvider({ children, provider }: AuthProviderProps): react_jsx_runtime.JSX.Element;
229
+ /**
230
+ * Hook to access authentication state and methods
231
+ *
232
+ * @throws Error if used outside of AuthProvider
233
+ *
234
+ * @example
235
+ * function LoginButton() {
236
+ * const { user, signIn, signOut, isLoading } = useAuth()
237
+ *
238
+ * if (isLoading) return <Loading />
239
+ *
240
+ * if (user) {
241
+ * return <button onClick={signOut}>Sign Out</button>
242
+ * }
243
+ *
244
+ * return <button onClick={() => signIn('user@example.com', 'password')}>Sign In</button>
245
+ * }
246
+ */
247
+ declare function useAuth(): AuthContextValue;
248
+ /**
249
+ * Hook to get the current user
250
+ * Shorthand for useAuth().user
251
+ */
252
+ declare function useUser(): AuthUser | null;
253
+ /**
254
+ * Hook to get the current session
255
+ * Shorthand for useAuth().session
256
+ */
257
+ declare function useSession(): AuthSession | null;
258
+ /**
259
+ * Hook to check if user is authenticated
260
+ * Shorthand for useAuth().isAuthenticated
261
+ */
262
+ declare function useIsAuthenticated(): boolean;
263
+
264
+ /**
265
+ * Configuration for the Supabase auth provider
266
+ */
267
+ interface SupabaseAuthConfig {
268
+ /**
269
+ * Supabase project URL
270
+ */
271
+ supabaseUrl: string;
272
+ /**
273
+ * Supabase anonymous/public key
274
+ */
275
+ supabaseAnonKey: string;
276
+ /**
277
+ * Optional: existing Supabase client instance
278
+ * If provided, supabaseUrl and supabaseAnonKey are ignored
279
+ */
280
+ client?: SupabaseClient;
281
+ }
282
+ /**
283
+ * Create an auth provider using Supabase Auth
284
+ *
285
+ * @example
286
+ * const authProvider = createSupabaseAuthProvider({
287
+ * supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL!,
288
+ * supabaseAnonKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
289
+ * })
290
+ *
291
+ * // Use in AuthProvider
292
+ * <AuthProvider provider={authProvider}>
293
+ * <App />
294
+ * </AuthProvider>
295
+ */
296
+ declare function createSupabaseAuthProvider(config: SupabaseAuthConfig): AuthProvider$1;
297
+
298
+ /**
299
+ * Configuration for the memory auth provider
300
+ */
301
+ interface MemoryAuthConfig {
302
+ /**
303
+ * Pre-populated users for testing
304
+ */
305
+ users?: Map<string, {
306
+ password: string;
307
+ user: AuthUser;
308
+ }>;
309
+ /**
310
+ * Initial session (for testing authenticated state)
311
+ */
312
+ initialSession?: AuthSession | null;
313
+ /**
314
+ * Delay in ms to simulate network latency
315
+ * @default 0
316
+ */
317
+ delay?: number;
318
+ }
319
+ /**
320
+ * Create an in-memory auth provider for testing
321
+ *
322
+ * This provider stores all data in memory and resets on page refresh.
323
+ * Useful for unit tests and local development without a real auth backend.
324
+ *
325
+ * @example
326
+ * const authProvider = createMemoryAuthProvider({
327
+ * users: new Map([
328
+ * ['test@example.com', {
329
+ * password: 'password123',
330
+ * user: { id: '1', email: 'test@example.com', name: 'Test User' }
331
+ * }]
332
+ * ])
333
+ * })
334
+ */
335
+ declare function createMemoryAuthProvider(config?: MemoryAuthConfig): AuthProvider$1;
336
+
337
+ export { type AuthContextValue, type AuthError, AuthProvider, type AuthProviderProps, type AuthResult, type AuthSession, type AuthStateChangeCallback, type AuthStateChangeEvent, type AuthUser, type AuthProvider$1 as IAuthProvider, type MemoryAuthConfig, type OAuthProvider, type SignInOptions, type SignUpOptions, type SupabaseAuthConfig, createMemoryAuthProvider, createSupabaseAuthProvider, useAuth, useIsAuthenticated, useSession, useUser };
@@ -0,0 +1,337 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+ import { SupabaseClient } from '@supabase/supabase-js';
4
+
5
+ /**
6
+ * Represents an authenticated user
7
+ */
8
+ interface AuthUser {
9
+ /**
10
+ * Unique identifier for the user
11
+ */
12
+ id: string;
13
+ /**
14
+ * User's email address
15
+ */
16
+ email: string | null;
17
+ /**
18
+ * User's display name
19
+ */
20
+ name?: string | null;
21
+ /**
22
+ * URL to user's avatar image
23
+ */
24
+ avatarUrl?: string | null;
25
+ /**
26
+ * Additional user metadata
27
+ */
28
+ metadata?: Record<string, unknown>;
29
+ }
30
+ /**
31
+ * Represents an authenticated session
32
+ */
33
+ interface AuthSession {
34
+ /**
35
+ * Access token for API requests
36
+ */
37
+ accessToken: string;
38
+ /**
39
+ * Refresh token for obtaining new access tokens
40
+ */
41
+ refreshToken?: string;
42
+ /**
43
+ * Unix timestamp when the access token expires
44
+ */
45
+ expiresAt?: number;
46
+ /**
47
+ * The authenticated user
48
+ */
49
+ user: AuthUser;
50
+ }
51
+ /**
52
+ * Supported OAuth providers
53
+ */
54
+ type OAuthProvider = 'github' | 'google' | 'gitlab';
55
+ /**
56
+ * Options for email/password sign in
57
+ */
58
+ interface SignInOptions {
59
+ email: string;
60
+ password: string;
61
+ }
62
+ /**
63
+ * Options for email/password sign up
64
+ */
65
+ interface SignUpOptions {
66
+ email: string;
67
+ password: string;
68
+ metadata?: Record<string, unknown>;
69
+ }
70
+ /**
71
+ * Result of an authentication operation
72
+ */
73
+ interface AuthResult {
74
+ /**
75
+ * The authenticated user, if successful
76
+ */
77
+ user: AuthUser | null;
78
+ /**
79
+ * The session, if successful
80
+ */
81
+ session: AuthSession | null;
82
+ /**
83
+ * Error details, if the operation failed
84
+ */
85
+ error: AuthError | null;
86
+ }
87
+ /**
88
+ * Authentication error details
89
+ */
90
+ interface AuthError {
91
+ /**
92
+ * Error code for programmatic handling
93
+ */
94
+ code: string;
95
+ /**
96
+ * Human-readable error message
97
+ */
98
+ message: string;
99
+ }
100
+ /**
101
+ * Events emitted by the auth state change listener
102
+ */
103
+ type AuthStateChangeEvent = 'SIGNED_IN' | 'SIGNED_OUT' | 'TOKEN_REFRESHED' | 'USER_UPDATED' | 'PASSWORD_RECOVERY' | 'INITIAL_SESSION';
104
+ /**
105
+ * Callback for auth state changes
106
+ */
107
+ type AuthStateChangeCallback = (event: AuthStateChangeEvent, session: AuthSession | null) => void;
108
+ /**
109
+ * Abstract authentication provider interface
110
+ *
111
+ * Implement this interface to add support for different auth providers.
112
+ * The default implementation uses Supabase Auth.
113
+ */
114
+ interface AuthProvider$1 {
115
+ /**
116
+ * Get the current session, if any
117
+ */
118
+ getSession(): Promise<AuthSession | null>;
119
+ /**
120
+ * Get the current user, if authenticated
121
+ */
122
+ getUser(): Promise<AuthUser | null>;
123
+ /**
124
+ * Sign in with email and password
125
+ */
126
+ signIn(options: SignInOptions): Promise<AuthResult>;
127
+ /**
128
+ * Sign up with email and password
129
+ */
130
+ signUp(options: SignUpOptions): Promise<AuthResult>;
131
+ /**
132
+ * Sign out the current user
133
+ */
134
+ signOut(): Promise<void>;
135
+ /**
136
+ * Sign in with an OAuth provider
137
+ * This will redirect to the provider's login page
138
+ */
139
+ signInWithOAuth(provider: OAuthProvider, redirectTo?: string): Promise<void>;
140
+ /**
141
+ * Send a password reset email
142
+ */
143
+ resetPasswordForEmail(email: string, redirectTo?: string): Promise<void>;
144
+ /**
145
+ * Update the current user's password
146
+ */
147
+ updatePassword(newPassword: string): Promise<void>;
148
+ /**
149
+ * Subscribe to auth state changes
150
+ * @returns Unsubscribe function
151
+ */
152
+ onAuthStateChange(callback: AuthStateChangeCallback): () => void;
153
+ }
154
+
155
+ /**
156
+ * Context value provided by AuthProvider
157
+ */
158
+ interface AuthContextValue {
159
+ /**
160
+ * The current authenticated user, or null if not authenticated
161
+ */
162
+ user: AuthUser | null;
163
+ /**
164
+ * The current session, or null if not authenticated
165
+ */
166
+ session: AuthSession | null;
167
+ /**
168
+ * Whether the auth state is still loading
169
+ */
170
+ isLoading: boolean;
171
+ /**
172
+ * Whether the user is authenticated
173
+ */
174
+ isAuthenticated: boolean;
175
+ /**
176
+ * Sign in with email and password
177
+ */
178
+ signIn: (email: string, password: string) => Promise<AuthResult>;
179
+ /**
180
+ * Sign up with email and password
181
+ */
182
+ signUp: (email: string, password: string, metadata?: Record<string, unknown>) => Promise<AuthResult>;
183
+ /**
184
+ * Sign out the current user
185
+ */
186
+ signOut: () => Promise<void>;
187
+ /**
188
+ * Sign in with an OAuth provider
189
+ */
190
+ signInWithOAuth: (provider: OAuthProvider, redirectTo?: string) => Promise<void>;
191
+ /**
192
+ * Send a password reset email
193
+ */
194
+ resetPasswordForEmail: (email: string, redirectTo?: string) => Promise<void>;
195
+ }
196
+ /**
197
+ * Props for the AuthProvider component
198
+ */
199
+ interface AuthProviderProps {
200
+ /**
201
+ * Child components to render
202
+ */
203
+ children: ReactNode;
204
+ /**
205
+ * The auth provider implementation to use
206
+ */
207
+ provider: AuthProvider$1;
208
+ }
209
+ /**
210
+ * Provider component that manages authentication state
211
+ *
212
+ * @example
213
+ * import { AuthProvider, createSupabaseAuthProvider } from '@demokit-ai/auth'
214
+ *
215
+ * const authProvider = createSupabaseAuthProvider({
216
+ * supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL!,
217
+ * supabaseAnonKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
218
+ * })
219
+ *
220
+ * function App() {
221
+ * return (
222
+ * <AuthProvider provider={authProvider}>
223
+ * <YourApp />
224
+ * </AuthProvider>
225
+ * )
226
+ * }
227
+ */
228
+ declare function AuthProvider({ children, provider }: AuthProviderProps): react_jsx_runtime.JSX.Element;
229
+ /**
230
+ * Hook to access authentication state and methods
231
+ *
232
+ * @throws Error if used outside of AuthProvider
233
+ *
234
+ * @example
235
+ * function LoginButton() {
236
+ * const { user, signIn, signOut, isLoading } = useAuth()
237
+ *
238
+ * if (isLoading) return <Loading />
239
+ *
240
+ * if (user) {
241
+ * return <button onClick={signOut}>Sign Out</button>
242
+ * }
243
+ *
244
+ * return <button onClick={() => signIn('user@example.com', 'password')}>Sign In</button>
245
+ * }
246
+ */
247
+ declare function useAuth(): AuthContextValue;
248
+ /**
249
+ * Hook to get the current user
250
+ * Shorthand for useAuth().user
251
+ */
252
+ declare function useUser(): AuthUser | null;
253
+ /**
254
+ * Hook to get the current session
255
+ * Shorthand for useAuth().session
256
+ */
257
+ declare function useSession(): AuthSession | null;
258
+ /**
259
+ * Hook to check if user is authenticated
260
+ * Shorthand for useAuth().isAuthenticated
261
+ */
262
+ declare function useIsAuthenticated(): boolean;
263
+
264
+ /**
265
+ * Configuration for the Supabase auth provider
266
+ */
267
+ interface SupabaseAuthConfig {
268
+ /**
269
+ * Supabase project URL
270
+ */
271
+ supabaseUrl: string;
272
+ /**
273
+ * Supabase anonymous/public key
274
+ */
275
+ supabaseAnonKey: string;
276
+ /**
277
+ * Optional: existing Supabase client instance
278
+ * If provided, supabaseUrl and supabaseAnonKey are ignored
279
+ */
280
+ client?: SupabaseClient;
281
+ }
282
+ /**
283
+ * Create an auth provider using Supabase Auth
284
+ *
285
+ * @example
286
+ * const authProvider = createSupabaseAuthProvider({
287
+ * supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL!,
288
+ * supabaseAnonKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
289
+ * })
290
+ *
291
+ * // Use in AuthProvider
292
+ * <AuthProvider provider={authProvider}>
293
+ * <App />
294
+ * </AuthProvider>
295
+ */
296
+ declare function createSupabaseAuthProvider(config: SupabaseAuthConfig): AuthProvider$1;
297
+
298
+ /**
299
+ * Configuration for the memory auth provider
300
+ */
301
+ interface MemoryAuthConfig {
302
+ /**
303
+ * Pre-populated users for testing
304
+ */
305
+ users?: Map<string, {
306
+ password: string;
307
+ user: AuthUser;
308
+ }>;
309
+ /**
310
+ * Initial session (for testing authenticated state)
311
+ */
312
+ initialSession?: AuthSession | null;
313
+ /**
314
+ * Delay in ms to simulate network latency
315
+ * @default 0
316
+ */
317
+ delay?: number;
318
+ }
319
+ /**
320
+ * Create an in-memory auth provider for testing
321
+ *
322
+ * This provider stores all data in memory and resets on page refresh.
323
+ * Useful for unit tests and local development without a real auth backend.
324
+ *
325
+ * @example
326
+ * const authProvider = createMemoryAuthProvider({
327
+ * users: new Map([
328
+ * ['test@example.com', {
329
+ * password: 'password123',
330
+ * user: { id: '1', email: 'test@example.com', name: 'Test User' }
331
+ * }]
332
+ * ])
333
+ * })
334
+ */
335
+ declare function createMemoryAuthProvider(config?: MemoryAuthConfig): AuthProvider$1;
336
+
337
+ export { type AuthContextValue, type AuthError, AuthProvider, type AuthProviderProps, type AuthResult, type AuthSession, type AuthStateChangeCallback, type AuthStateChangeEvent, type AuthUser, type AuthProvider$1 as IAuthProvider, type MemoryAuthConfig, type OAuthProvider, type SignInOptions, type SignUpOptions, type SupabaseAuthConfig, createMemoryAuthProvider, createSupabaseAuthProvider, useAuth, useIsAuthenticated, useSession, useUser };