@ccatto/react-auth 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.
@@ -0,0 +1,296 @@
1
+ export { C as CattoAuthClientConfig, a as CattoAuthDatabaseClient, b as CattoAuthServerConfig, c as CattoAuthSocialProvider, d as CattoSessionProviderConfig } from './config-CvzbPvtw.cjs';
2
+
3
+ /**
4
+ * @ccatto/react-auth - Session Types
5
+ *
6
+ * Types for enriched sessions, users, and compatibility with NextAuth patterns.
7
+ */
8
+ interface EnrichedUser {
9
+ id: string;
10
+ email: string;
11
+ name: string | null;
12
+ image: string | null;
13
+ /** Custom fields added by enrichSession hook */
14
+ playerID?: number;
15
+ role: string;
16
+ organizationId: string | null;
17
+ organizations: Array<{
18
+ id: string;
19
+ name: string;
20
+ slug: string;
21
+ role: string;
22
+ permissions: string[];
23
+ }>;
24
+ /** Allow additional custom fields from enrichSession */
25
+ [key: string]: unknown;
26
+ }
27
+ interface EnrichedSession {
28
+ user: EnrichedUser;
29
+ session: {
30
+ id: string;
31
+ expiresAt: Date;
32
+ token: string;
33
+ createdAt: Date;
34
+ updatedAt: Date;
35
+ userId: string;
36
+ };
37
+ }
38
+ interface CompatSessionUser {
39
+ id: string;
40
+ email: string;
41
+ name: string | null;
42
+ image: string | null;
43
+ playerID?: number;
44
+ role: string;
45
+ organizationId: string | null;
46
+ organizations: Array<{
47
+ id: string;
48
+ name: string;
49
+ slug: string;
50
+ role: string;
51
+ permissions: string[];
52
+ }>;
53
+ }
54
+ interface CompatSession {
55
+ user: CompatSessionUser;
56
+ expires: string;
57
+ }
58
+ interface AuthStoreUser {
59
+ userId: string;
60
+ email: string;
61
+ name?: string;
62
+ image?: string;
63
+ role?: string;
64
+ playerID?: number;
65
+ organizationId?: string;
66
+ organizations?: Array<{
67
+ id: string;
68
+ name: string;
69
+ slug: string;
70
+ role: string;
71
+ permissions: string[];
72
+ }>;
73
+ }
74
+
75
+ /**
76
+ * @ccatto/react-auth - Session Store
77
+ *
78
+ * Synchronous session state store for sharing auth session with Apollo Client.
79
+ * Avoids network calls in Apollo's authLink.
80
+ *
81
+ * @example
82
+ * // Write (from SessionSync component in React tree)
83
+ * sessionStore.setSession(session);
84
+ *
85
+ * // Read (from Apollo authLink — synchronous, no network call)
86
+ * const session = sessionStore.getSession();
87
+ */
88
+
89
+ declare const sessionStore: {
90
+ /** Get the current session (synchronous, no network call) */
91
+ getSession(): CompatSession | null;
92
+ /** Update the session (called by SessionSync component) */
93
+ setSession(session: CompatSession | null): void;
94
+ /** Subscribe to session changes */
95
+ subscribe(listener: (session: CompatSession | null) => void): () => void;
96
+ /** Get the user ID from the current session (convenience method) */
97
+ getUserId(): string | null;
98
+ };
99
+
100
+ /**
101
+ * @ccatto/react-auth - Auth Storage Interface
102
+ *
103
+ * Platform-agnostic interface for auth token storage.
104
+ * Implementations handle platform-specific storage (web vs mobile).
105
+ */
106
+ interface IAuthStorage {
107
+ /** Store access token */
108
+ setAccessToken(token: string): Promise<void>;
109
+ /** Retrieve access token */
110
+ getAccessToken(): Promise<string | null>;
111
+ /** Store refresh token */
112
+ setRefreshToken(token: string): Promise<void>;
113
+ /** Retrieve refresh token */
114
+ getRefreshToken(): Promise<string | null>;
115
+ /** Remove all tokens (logout) */
116
+ clearTokens(): Promise<void>;
117
+ /** Check if user has valid tokens */
118
+ hasTokens(): Promise<boolean>;
119
+ }
120
+
121
+ /**
122
+ * @ccatto/react-auth - Auth API Interface
123
+ *
124
+ * Abstract interface for auth API calls. Apps provide their own
125
+ * implementation (e.g., GraphQL, REST, etc.).
126
+ */
127
+ interface AuthUser {
128
+ id: string;
129
+ email: string;
130
+ name: string | null;
131
+ role: string;
132
+ playerID?: number;
133
+ organizationId?: string | null;
134
+ }
135
+ interface LoginCredentials {
136
+ email: string;
137
+ password: string;
138
+ }
139
+ interface RegisterData {
140
+ email: string;
141
+ password: string;
142
+ name: string;
143
+ }
144
+ interface AuthTokens {
145
+ accessToken: string;
146
+ refreshToken: string;
147
+ }
148
+ interface LoginResponse extends AuthTokens {
149
+ user: AuthUser;
150
+ }
151
+ interface PasskeyAuthenticationOptions {
152
+ options: string;
153
+ sessionId: string;
154
+ }
155
+ interface SendOtpResponse {
156
+ success: boolean;
157
+ message: string;
158
+ expiresIn: number;
159
+ }
160
+ interface VerifyOtpSuccess {
161
+ success: true;
162
+ message: string;
163
+ accessToken: string;
164
+ refreshToken: string;
165
+ isNewUser?: boolean;
166
+ userId?: string;
167
+ }
168
+ interface VerifyOtpFailure {
169
+ success: false;
170
+ message: string;
171
+ }
172
+ type VerifyOtpResponse = VerifyOtpSuccess | VerifyOtpFailure;
173
+ /**
174
+ * Auth API service interface.
175
+ * Implement this with your API layer (GraphQL, REST, etc.).
176
+ */
177
+ interface IAuthApiService {
178
+ login(credentials: LoginCredentials): Promise<LoginResponse>;
179
+ register(data: RegisterData): Promise<LoginResponse>;
180
+ logout(refreshToken: string): Promise<void>;
181
+ refreshToken(refreshToken: string): Promise<{
182
+ accessToken: string;
183
+ }>;
184
+ forgotPassword?(email: string): Promise<{
185
+ message: string;
186
+ resetToken?: string;
187
+ }>;
188
+ resetPassword?(resetToken: string, newPassword: string): Promise<{
189
+ message: string;
190
+ }>;
191
+ sendPhoneOtp?(phoneNumber: string): Promise<SendOtpResponse>;
192
+ verifyPhoneOtp?(phoneNumber: string, code: string): Promise<VerifyOtpResponse>;
193
+ generatePasskeyAuthenticationOptions?(email?: string): Promise<PasskeyAuthenticationOptions>;
194
+ verifyPasskeyAuthentication?(sessionId: string, response: string): Promise<LoginResponse>;
195
+ }
196
+ /**
197
+ * Optional logger interface for auth services.
198
+ * Implement with your logging framework (Pino, Winston, console, etc.).
199
+ */
200
+ interface IAuthLogger {
201
+ info(message: string, data?: Record<string, unknown>): void;
202
+ warn(message: string, data?: Record<string, unknown>): void;
203
+ error(message: string, data?: Record<string, unknown>): void;
204
+ }
205
+
206
+ /**
207
+ * @ccatto/react-auth - JWT Auth Service
208
+ *
209
+ * Platform-agnostic JWT authentication service.
210
+ * Handles token storage, login, register, refresh, and passkey auth.
211
+ *
212
+ * Uses IAuthStorage for token persistence and IAuthApiService for API calls.
213
+ *
214
+ * @example
215
+ * ```typescript
216
+ * import { JwtAuthService, CapacitorAuthStorage } from '@ccatto/react-auth';
217
+ *
218
+ * const storage = new CapacitorAuthStorage({ keyPrefix: 'myapp' });
219
+ * const authService = new JwtAuthService(storage, myApiService, undefined, {
220
+ * onSessionExpired: () => router.push('/login'),
221
+ * });
222
+ * await authService.login({ email, password });
223
+ * ```
224
+ */
225
+
226
+ interface JwtAuthServiceOptions {
227
+ /** Called when session expires (refresh token fails). Use to redirect to login. */
228
+ onSessionExpired?: () => void;
229
+ }
230
+ declare class JwtAuthService {
231
+ private storage;
232
+ private api;
233
+ private log;
234
+ private options;
235
+ private cachedTokenExp;
236
+ private refreshPromise;
237
+ private lastRefreshFailure;
238
+ private static REFRESH_COOLDOWN_MS;
239
+ constructor(storage: IAuthStorage, api: IAuthApiService, logger?: IAuthLogger, options?: JwtAuthServiceOptions);
240
+ /** Login with email and password */
241
+ login(credentials: LoginCredentials): Promise<LoginResponse>;
242
+ /** Register new user */
243
+ register(data: RegisterData): Promise<LoginResponse>;
244
+ /** Logout (clear tokens) */
245
+ logout(): Promise<void>;
246
+ /** Refresh access token */
247
+ refreshAccessToken(): Promise<string>;
248
+ /** Get current access token */
249
+ getAccessToken(): Promise<string | null>;
250
+ /** Check if a JWT token is expired or about to expire */
251
+ private isTokenExpiredOrExpiring;
252
+ /**
253
+ * Get auth headers for API requests.
254
+ * Proactively refreshes the access token if it's expired or about to expire.
255
+ * Includes cooldown to prevent repeated refresh attempts after failure.
256
+ */
257
+ getAuthHeaders(): Promise<Record<string, string>>;
258
+ /** Check if user is authenticated */
259
+ isAuthenticated(): Promise<boolean>;
260
+ /** Check if tokens exist in storage */
261
+ hasTokens(): Promise<boolean>;
262
+ /** Decode JWT token (client-side only — for user info, NOT for security) */
263
+ decodeToken(token: string): AuthUser | null;
264
+ /** Get current user from token (client-side decode) */
265
+ getCurrentUser(): Promise<AuthUser | null>;
266
+ /** Login with passkey (WebAuthn/FIDO2) */
267
+ loginWithPasskey(): Promise<LoginResponse>;
268
+ /** Send OTP to phone number for phone-based login */
269
+ sendPhoneOtp(phoneNumber: string): Promise<{
270
+ success: boolean;
271
+ message: string;
272
+ expiresIn: number;
273
+ }>;
274
+ /** Verify OTP and login/register user */
275
+ verifyPhoneOtp(phoneNumber: string, code: string): Promise<LoginResponse & {
276
+ isNewUser: boolean;
277
+ }>;
278
+ }
279
+
280
+ interface CapacitorAuthStorageOptions {
281
+ /** Key prefix for stored tokens (default: 'catto_auth') */
282
+ keyPrefix?: string;
283
+ }
284
+ declare class CapacitorAuthStorage implements IAuthStorage {
285
+ private readonly ACCESS_TOKEN_KEY;
286
+ private readonly REFRESH_TOKEN_KEY;
287
+ constructor(options?: CapacitorAuthStorageOptions);
288
+ setAccessToken(token: string): Promise<void>;
289
+ getAccessToken(): Promise<string | null>;
290
+ setRefreshToken(token: string): Promise<void>;
291
+ getRefreshToken(): Promise<string | null>;
292
+ clearTokens(): Promise<void>;
293
+ hasTokens(): Promise<boolean>;
294
+ }
295
+
296
+ export { type AuthStoreUser, type AuthTokens, type AuthUser, CapacitorAuthStorage, type CapacitorAuthStorageOptions, type CompatSession, type CompatSessionUser, type EnrichedSession, type EnrichedUser, type IAuthApiService, type IAuthLogger, type IAuthStorage, JwtAuthService, type LoginCredentials, type LoginResponse, type PasskeyAuthenticationOptions, type RegisterData, type SendOtpResponse, type VerifyOtpFailure, type VerifyOtpResponse, type VerifyOtpSuccess, sessionStore };
@@ -0,0 +1,296 @@
1
+ export { C as CattoAuthClientConfig, a as CattoAuthDatabaseClient, b as CattoAuthServerConfig, c as CattoAuthSocialProvider, d as CattoSessionProviderConfig } from './config-CvzbPvtw.js';
2
+
3
+ /**
4
+ * @ccatto/react-auth - Session Types
5
+ *
6
+ * Types for enriched sessions, users, and compatibility with NextAuth patterns.
7
+ */
8
+ interface EnrichedUser {
9
+ id: string;
10
+ email: string;
11
+ name: string | null;
12
+ image: string | null;
13
+ /** Custom fields added by enrichSession hook */
14
+ playerID?: number;
15
+ role: string;
16
+ organizationId: string | null;
17
+ organizations: Array<{
18
+ id: string;
19
+ name: string;
20
+ slug: string;
21
+ role: string;
22
+ permissions: string[];
23
+ }>;
24
+ /** Allow additional custom fields from enrichSession */
25
+ [key: string]: unknown;
26
+ }
27
+ interface EnrichedSession {
28
+ user: EnrichedUser;
29
+ session: {
30
+ id: string;
31
+ expiresAt: Date;
32
+ token: string;
33
+ createdAt: Date;
34
+ updatedAt: Date;
35
+ userId: string;
36
+ };
37
+ }
38
+ interface CompatSessionUser {
39
+ id: string;
40
+ email: string;
41
+ name: string | null;
42
+ image: string | null;
43
+ playerID?: number;
44
+ role: string;
45
+ organizationId: string | null;
46
+ organizations: Array<{
47
+ id: string;
48
+ name: string;
49
+ slug: string;
50
+ role: string;
51
+ permissions: string[];
52
+ }>;
53
+ }
54
+ interface CompatSession {
55
+ user: CompatSessionUser;
56
+ expires: string;
57
+ }
58
+ interface AuthStoreUser {
59
+ userId: string;
60
+ email: string;
61
+ name?: string;
62
+ image?: string;
63
+ role?: string;
64
+ playerID?: number;
65
+ organizationId?: string;
66
+ organizations?: Array<{
67
+ id: string;
68
+ name: string;
69
+ slug: string;
70
+ role: string;
71
+ permissions: string[];
72
+ }>;
73
+ }
74
+
75
+ /**
76
+ * @ccatto/react-auth - Session Store
77
+ *
78
+ * Synchronous session state store for sharing auth session with Apollo Client.
79
+ * Avoids network calls in Apollo's authLink.
80
+ *
81
+ * @example
82
+ * // Write (from SessionSync component in React tree)
83
+ * sessionStore.setSession(session);
84
+ *
85
+ * // Read (from Apollo authLink — synchronous, no network call)
86
+ * const session = sessionStore.getSession();
87
+ */
88
+
89
+ declare const sessionStore: {
90
+ /** Get the current session (synchronous, no network call) */
91
+ getSession(): CompatSession | null;
92
+ /** Update the session (called by SessionSync component) */
93
+ setSession(session: CompatSession | null): void;
94
+ /** Subscribe to session changes */
95
+ subscribe(listener: (session: CompatSession | null) => void): () => void;
96
+ /** Get the user ID from the current session (convenience method) */
97
+ getUserId(): string | null;
98
+ };
99
+
100
+ /**
101
+ * @ccatto/react-auth - Auth Storage Interface
102
+ *
103
+ * Platform-agnostic interface for auth token storage.
104
+ * Implementations handle platform-specific storage (web vs mobile).
105
+ */
106
+ interface IAuthStorage {
107
+ /** Store access token */
108
+ setAccessToken(token: string): Promise<void>;
109
+ /** Retrieve access token */
110
+ getAccessToken(): Promise<string | null>;
111
+ /** Store refresh token */
112
+ setRefreshToken(token: string): Promise<void>;
113
+ /** Retrieve refresh token */
114
+ getRefreshToken(): Promise<string | null>;
115
+ /** Remove all tokens (logout) */
116
+ clearTokens(): Promise<void>;
117
+ /** Check if user has valid tokens */
118
+ hasTokens(): Promise<boolean>;
119
+ }
120
+
121
+ /**
122
+ * @ccatto/react-auth - Auth API Interface
123
+ *
124
+ * Abstract interface for auth API calls. Apps provide their own
125
+ * implementation (e.g., GraphQL, REST, etc.).
126
+ */
127
+ interface AuthUser {
128
+ id: string;
129
+ email: string;
130
+ name: string | null;
131
+ role: string;
132
+ playerID?: number;
133
+ organizationId?: string | null;
134
+ }
135
+ interface LoginCredentials {
136
+ email: string;
137
+ password: string;
138
+ }
139
+ interface RegisterData {
140
+ email: string;
141
+ password: string;
142
+ name: string;
143
+ }
144
+ interface AuthTokens {
145
+ accessToken: string;
146
+ refreshToken: string;
147
+ }
148
+ interface LoginResponse extends AuthTokens {
149
+ user: AuthUser;
150
+ }
151
+ interface PasskeyAuthenticationOptions {
152
+ options: string;
153
+ sessionId: string;
154
+ }
155
+ interface SendOtpResponse {
156
+ success: boolean;
157
+ message: string;
158
+ expiresIn: number;
159
+ }
160
+ interface VerifyOtpSuccess {
161
+ success: true;
162
+ message: string;
163
+ accessToken: string;
164
+ refreshToken: string;
165
+ isNewUser?: boolean;
166
+ userId?: string;
167
+ }
168
+ interface VerifyOtpFailure {
169
+ success: false;
170
+ message: string;
171
+ }
172
+ type VerifyOtpResponse = VerifyOtpSuccess | VerifyOtpFailure;
173
+ /**
174
+ * Auth API service interface.
175
+ * Implement this with your API layer (GraphQL, REST, etc.).
176
+ */
177
+ interface IAuthApiService {
178
+ login(credentials: LoginCredentials): Promise<LoginResponse>;
179
+ register(data: RegisterData): Promise<LoginResponse>;
180
+ logout(refreshToken: string): Promise<void>;
181
+ refreshToken(refreshToken: string): Promise<{
182
+ accessToken: string;
183
+ }>;
184
+ forgotPassword?(email: string): Promise<{
185
+ message: string;
186
+ resetToken?: string;
187
+ }>;
188
+ resetPassword?(resetToken: string, newPassword: string): Promise<{
189
+ message: string;
190
+ }>;
191
+ sendPhoneOtp?(phoneNumber: string): Promise<SendOtpResponse>;
192
+ verifyPhoneOtp?(phoneNumber: string, code: string): Promise<VerifyOtpResponse>;
193
+ generatePasskeyAuthenticationOptions?(email?: string): Promise<PasskeyAuthenticationOptions>;
194
+ verifyPasskeyAuthentication?(sessionId: string, response: string): Promise<LoginResponse>;
195
+ }
196
+ /**
197
+ * Optional logger interface for auth services.
198
+ * Implement with your logging framework (Pino, Winston, console, etc.).
199
+ */
200
+ interface IAuthLogger {
201
+ info(message: string, data?: Record<string, unknown>): void;
202
+ warn(message: string, data?: Record<string, unknown>): void;
203
+ error(message: string, data?: Record<string, unknown>): void;
204
+ }
205
+
206
+ /**
207
+ * @ccatto/react-auth - JWT Auth Service
208
+ *
209
+ * Platform-agnostic JWT authentication service.
210
+ * Handles token storage, login, register, refresh, and passkey auth.
211
+ *
212
+ * Uses IAuthStorage for token persistence and IAuthApiService for API calls.
213
+ *
214
+ * @example
215
+ * ```typescript
216
+ * import { JwtAuthService, CapacitorAuthStorage } from '@ccatto/react-auth';
217
+ *
218
+ * const storage = new CapacitorAuthStorage({ keyPrefix: 'myapp' });
219
+ * const authService = new JwtAuthService(storage, myApiService, undefined, {
220
+ * onSessionExpired: () => router.push('/login'),
221
+ * });
222
+ * await authService.login({ email, password });
223
+ * ```
224
+ */
225
+
226
+ interface JwtAuthServiceOptions {
227
+ /** Called when session expires (refresh token fails). Use to redirect to login. */
228
+ onSessionExpired?: () => void;
229
+ }
230
+ declare class JwtAuthService {
231
+ private storage;
232
+ private api;
233
+ private log;
234
+ private options;
235
+ private cachedTokenExp;
236
+ private refreshPromise;
237
+ private lastRefreshFailure;
238
+ private static REFRESH_COOLDOWN_MS;
239
+ constructor(storage: IAuthStorage, api: IAuthApiService, logger?: IAuthLogger, options?: JwtAuthServiceOptions);
240
+ /** Login with email and password */
241
+ login(credentials: LoginCredentials): Promise<LoginResponse>;
242
+ /** Register new user */
243
+ register(data: RegisterData): Promise<LoginResponse>;
244
+ /** Logout (clear tokens) */
245
+ logout(): Promise<void>;
246
+ /** Refresh access token */
247
+ refreshAccessToken(): Promise<string>;
248
+ /** Get current access token */
249
+ getAccessToken(): Promise<string | null>;
250
+ /** Check if a JWT token is expired or about to expire */
251
+ private isTokenExpiredOrExpiring;
252
+ /**
253
+ * Get auth headers for API requests.
254
+ * Proactively refreshes the access token if it's expired or about to expire.
255
+ * Includes cooldown to prevent repeated refresh attempts after failure.
256
+ */
257
+ getAuthHeaders(): Promise<Record<string, string>>;
258
+ /** Check if user is authenticated */
259
+ isAuthenticated(): Promise<boolean>;
260
+ /** Check if tokens exist in storage */
261
+ hasTokens(): Promise<boolean>;
262
+ /** Decode JWT token (client-side only — for user info, NOT for security) */
263
+ decodeToken(token: string): AuthUser | null;
264
+ /** Get current user from token (client-side decode) */
265
+ getCurrentUser(): Promise<AuthUser | null>;
266
+ /** Login with passkey (WebAuthn/FIDO2) */
267
+ loginWithPasskey(): Promise<LoginResponse>;
268
+ /** Send OTP to phone number for phone-based login */
269
+ sendPhoneOtp(phoneNumber: string): Promise<{
270
+ success: boolean;
271
+ message: string;
272
+ expiresIn: number;
273
+ }>;
274
+ /** Verify OTP and login/register user */
275
+ verifyPhoneOtp(phoneNumber: string, code: string): Promise<LoginResponse & {
276
+ isNewUser: boolean;
277
+ }>;
278
+ }
279
+
280
+ interface CapacitorAuthStorageOptions {
281
+ /** Key prefix for stored tokens (default: 'catto_auth') */
282
+ keyPrefix?: string;
283
+ }
284
+ declare class CapacitorAuthStorage implements IAuthStorage {
285
+ private readonly ACCESS_TOKEN_KEY;
286
+ private readonly REFRESH_TOKEN_KEY;
287
+ constructor(options?: CapacitorAuthStorageOptions);
288
+ setAccessToken(token: string): Promise<void>;
289
+ getAccessToken(): Promise<string | null>;
290
+ setRefreshToken(token: string): Promise<void>;
291
+ getRefreshToken(): Promise<string | null>;
292
+ clearTokens(): Promise<void>;
293
+ hasTokens(): Promise<boolean>;
294
+ }
295
+
296
+ export { type AuthStoreUser, type AuthTokens, type AuthUser, CapacitorAuthStorage, type CapacitorAuthStorageOptions, type CompatSession, type CompatSessionUser, type EnrichedSession, type EnrichedUser, type IAuthApiService, type IAuthLogger, type IAuthStorage, JwtAuthService, type LoginCredentials, type LoginResponse, type PasskeyAuthenticationOptions, type RegisterData, type SendOtpResponse, type VerifyOtpFailure, type VerifyOtpResponse, type VerifyOtpSuccess, sessionStore };