godgpt-web-auth 0.1.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.
- package/README.md +203 -0
- package/dist/index.cjs +1089 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +376 -0
- package/dist/index.d.ts +376 -0
- package/dist/index.js +1073 -0
- package/dist/index.js.map +1 -0
- package/package.json +78 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
4
|
+
interface AuthConfig {
|
|
5
|
+
/** Backend API URL for token exchange */
|
|
6
|
+
backendUrl: string;
|
|
7
|
+
/** Google OAuth configuration */
|
|
8
|
+
google?: {
|
|
9
|
+
clientId: string;
|
|
10
|
+
scopes?: string[];
|
|
11
|
+
};
|
|
12
|
+
/** Apple Sign-In configuration */
|
|
13
|
+
apple?: {
|
|
14
|
+
clientId: string;
|
|
15
|
+
redirectUri?: string;
|
|
16
|
+
};
|
|
17
|
+
/** Email auth configuration */
|
|
18
|
+
email?: {
|
|
19
|
+
enabled: boolean;
|
|
20
|
+
};
|
|
21
|
+
/** App identification for backend */
|
|
22
|
+
appId?: string;
|
|
23
|
+
}
|
|
24
|
+
type AuthProviderType = "google" | "apple" | "password";
|
|
25
|
+
interface JWTData {
|
|
26
|
+
access_token: string;
|
|
27
|
+
refresh_token?: string;
|
|
28
|
+
expires_in: number;
|
|
29
|
+
token_type: string;
|
|
30
|
+
}
|
|
31
|
+
interface AuthResult {
|
|
32
|
+
success: boolean;
|
|
33
|
+
state: "success" | "cancelled" | "error";
|
|
34
|
+
tokens: JWTData | null;
|
|
35
|
+
message?: string;
|
|
36
|
+
provider?: AuthProviderType;
|
|
37
|
+
}
|
|
38
|
+
interface AuthError {
|
|
39
|
+
provider: AuthProviderType;
|
|
40
|
+
code: string;
|
|
41
|
+
message: string;
|
|
42
|
+
}
|
|
43
|
+
interface AuthCallbacks {
|
|
44
|
+
/**
|
|
45
|
+
* Called when user logs out or tokens are invalid on mount.
|
|
46
|
+
* Use this to redirect to login, clear app state, etc.
|
|
47
|
+
*/
|
|
48
|
+
onLogout?: () => void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Public API exposed by useAuth() hook.
|
|
52
|
+
* Intentionally minimal - only what consumers need.
|
|
53
|
+
*/
|
|
54
|
+
interface AuthContextValue {
|
|
55
|
+
/** Current JWT tokens, null if not authenticated */
|
|
56
|
+
tokens: JWTData | null;
|
|
57
|
+
/** Logout and clear tokens. Triggers onLogout callback. */
|
|
58
|
+
logout: () => Promise<void>;
|
|
59
|
+
/** True while a login operation is in progress */
|
|
60
|
+
isLoading: boolean;
|
|
61
|
+
/** True once initial token load from storage is complete */
|
|
62
|
+
isAuthLoaded: boolean;
|
|
63
|
+
}
|
|
64
|
+
interface TokenRequestParams {
|
|
65
|
+
id_token?: string;
|
|
66
|
+
code?: string;
|
|
67
|
+
username?: string;
|
|
68
|
+
password?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface AuthProviderProps {
|
|
72
|
+
children: React.ReactNode;
|
|
73
|
+
/** Auth configuration with backend URL and provider settings */
|
|
74
|
+
config: AuthConfig;
|
|
75
|
+
/**
|
|
76
|
+
* Called when user logs out or tokens are invalid on mount.
|
|
77
|
+
* Use this to redirect, clear app state, etc.
|
|
78
|
+
*/
|
|
79
|
+
onLogout?: () => void;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* AuthProvider - Wraps your app to provide authentication state.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```tsx
|
|
86
|
+
* <AuthProvider
|
|
87
|
+
* config={{
|
|
88
|
+
* backendUrl: import.meta.env.VITE_AUTH_BACKEND_URL,
|
|
89
|
+
* google: { clientId: import.meta.env.VITE_GOOGLE_CLIENT_ID },
|
|
90
|
+
* apple: { clientId: import.meta.env.VITE_APPLE_CLIENT_ID },
|
|
91
|
+
* }}
|
|
92
|
+
* onLogout={() => router.push('/')}
|
|
93
|
+
* >
|
|
94
|
+
* <App />
|
|
95
|
+
* </AuthProvider>
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
declare function AuthProvider({ children, config, onLogout, }: AuthProviderProps): react_jsx_runtime.JSX.Element;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* LoginPage - Complete login UI component.
|
|
102
|
+
* Gets config from AuthProvider context.
|
|
103
|
+
* Handles Google, Apple, and Email sign-in.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```tsx
|
|
107
|
+
* function App() {
|
|
108
|
+
* const { tokens, isAuthLoaded } = useAuth();
|
|
109
|
+
*
|
|
110
|
+
* if (!isAuthLoaded) return <SplashScreen />;
|
|
111
|
+
* if (!tokens) return <LoginPage />;
|
|
112
|
+
* return <MainApp />;
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
declare function LoginPage(): react_jsx_runtime.JSX.Element;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* useAuth - Public hook for consumers.
|
|
120
|
+
* Returns only the public API (tokens, logout, isLoading, isAuthLoaded).
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```tsx
|
|
124
|
+
* function App() {
|
|
125
|
+
* const { tokens, logout, isLoading, isAuthLoaded } = useAuth();
|
|
126
|
+
*
|
|
127
|
+
* if (!isAuthLoaded) return <SplashScreen />;
|
|
128
|
+
* if (!tokens) return <LoginPage />;
|
|
129
|
+
* return <MainApp onLogout={logout} />;
|
|
130
|
+
* }
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
declare function useAuth(): AuthContextValue;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Sign in with email and password.
|
|
137
|
+
* Sends credentials to backend for authentication.
|
|
138
|
+
*
|
|
139
|
+
* @param email - User's email address
|
|
140
|
+
* @param password - User's password
|
|
141
|
+
* @param config - Auth configuration with backendUrl
|
|
142
|
+
* @returns AuthResult with tokens on success
|
|
143
|
+
*/
|
|
144
|
+
declare function signInWithEmail(email: string, password: string, config: AuthConfig): Promise<AuthResult>;
|
|
145
|
+
|
|
146
|
+
declare global {
|
|
147
|
+
interface Window {
|
|
148
|
+
google?: {
|
|
149
|
+
accounts: {
|
|
150
|
+
id: {
|
|
151
|
+
initialize: (config: GoogleInitConfig) => void;
|
|
152
|
+
prompt: (callback?: (notification: PromptNotification) => void) => void;
|
|
153
|
+
renderButton: (element: HTMLElement, config: ButtonConfig) => void;
|
|
154
|
+
revoke: (hint: string, callback: () => void) => void;
|
|
155
|
+
cancel: () => void;
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
interface GoogleInitConfig {
|
|
162
|
+
client_id: string;
|
|
163
|
+
callback: (response: GoogleCredentialResponse) => void;
|
|
164
|
+
auto_select?: boolean;
|
|
165
|
+
cancel_on_tap_outside?: boolean;
|
|
166
|
+
context?: "signin" | "signup" | "use";
|
|
167
|
+
ux_mode?: "popup" | "redirect";
|
|
168
|
+
login_uri?: string;
|
|
169
|
+
nonce?: string;
|
|
170
|
+
use_fedcm_for_prompt?: boolean;
|
|
171
|
+
}
|
|
172
|
+
interface GoogleCredentialResponse {
|
|
173
|
+
credential: string;
|
|
174
|
+
select_by?: string;
|
|
175
|
+
clientId?: string;
|
|
176
|
+
}
|
|
177
|
+
interface PromptNotification {
|
|
178
|
+
isNotDisplayed: () => boolean;
|
|
179
|
+
isSkippedMoment: () => boolean;
|
|
180
|
+
isDismissedMoment: () => boolean;
|
|
181
|
+
getNotDisplayedReason: () => string;
|
|
182
|
+
getSkippedReason: () => string;
|
|
183
|
+
getDismissedReason: () => string;
|
|
184
|
+
}
|
|
185
|
+
interface ButtonConfig {
|
|
186
|
+
type?: "standard" | "icon";
|
|
187
|
+
theme?: "outline" | "filled_blue" | "filled_black";
|
|
188
|
+
size?: "large" | "medium" | "small";
|
|
189
|
+
text?: "signin_with" | "signup_with" | "continue_with" | "signin";
|
|
190
|
+
shape?: "rectangular" | "pill" | "circle" | "square";
|
|
191
|
+
logo_alignment?: "left" | "center";
|
|
192
|
+
width?: number;
|
|
193
|
+
locale?: string;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Load Google Identity Services SDK
|
|
197
|
+
*/
|
|
198
|
+
declare function loadGoogleSDK(): Promise<void>;
|
|
199
|
+
/**
|
|
200
|
+
* Sign in with Google using Google Identity Services.
|
|
201
|
+
* Uses One Tap flow with popup fallback.
|
|
202
|
+
*
|
|
203
|
+
* @param config - Auth configuration with Google client ID
|
|
204
|
+
* @returns AuthResult with tokens on success
|
|
205
|
+
*/
|
|
206
|
+
declare function signInWithGoogle(config: AuthConfig): Promise<AuthResult>;
|
|
207
|
+
|
|
208
|
+
declare global {
|
|
209
|
+
interface Window {
|
|
210
|
+
AppleID?: {
|
|
211
|
+
auth: {
|
|
212
|
+
init: (config: AppleInitConfig) => void;
|
|
213
|
+
signIn: (config?: AppleSignInConfig) => Promise<AppleSignInResponse>;
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
interface AppleInitConfig {
|
|
219
|
+
clientId: string;
|
|
220
|
+
scope?: string;
|
|
221
|
+
redirectURI: string;
|
|
222
|
+
state?: string;
|
|
223
|
+
nonce?: string;
|
|
224
|
+
usePopup?: boolean;
|
|
225
|
+
}
|
|
226
|
+
interface AppleSignInConfig {
|
|
227
|
+
scope?: string;
|
|
228
|
+
state?: string;
|
|
229
|
+
nonce?: string;
|
|
230
|
+
}
|
|
231
|
+
interface AppleSignInResponse {
|
|
232
|
+
authorization: {
|
|
233
|
+
code: string;
|
|
234
|
+
id_token?: string;
|
|
235
|
+
state?: string;
|
|
236
|
+
};
|
|
237
|
+
user?: {
|
|
238
|
+
email?: string;
|
|
239
|
+
name?: {
|
|
240
|
+
firstName?: string;
|
|
241
|
+
lastName?: string;
|
|
242
|
+
};
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Load Apple Sign In JS SDK
|
|
247
|
+
*/
|
|
248
|
+
declare function loadAppleSDK(): Promise<void>;
|
|
249
|
+
/**
|
|
250
|
+
* Sign in with Apple using Apple Sign In JS.
|
|
251
|
+
*
|
|
252
|
+
* @param config - Auth configuration with Apple client ID
|
|
253
|
+
* @returns AuthResult with tokens on success
|
|
254
|
+
*/
|
|
255
|
+
declare function signInWithApple(config: AuthConfig): Promise<AuthResult>;
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Validate auth configuration.
|
|
259
|
+
* Called by AuthProvider on mount.
|
|
260
|
+
*/
|
|
261
|
+
declare function validateConfig(config: AuthConfig): void;
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Interface for custom storage adapters.
|
|
265
|
+
* Implement this to use your own storage (Redux, Zustand, AsyncStorage, etc.)
|
|
266
|
+
*/
|
|
267
|
+
interface StorageAdapter {
|
|
268
|
+
getItem: (key: string) => Promise<string | null> | string | null;
|
|
269
|
+
setItem: (key: string, value: string) => Promise<void> | void;
|
|
270
|
+
removeItem: (key: string) => Promise<void> | void;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Default localStorage adapter for web
|
|
274
|
+
*/
|
|
275
|
+
declare const localStorageAdapter: StorageAdapter;
|
|
276
|
+
/**
|
|
277
|
+
* Memory adapter for SSR or testing
|
|
278
|
+
*/
|
|
279
|
+
declare const memoryStorageAdapter: () => StorageAdapter;
|
|
280
|
+
/**
|
|
281
|
+
* Creates a storage service with the provided adapter.
|
|
282
|
+
* Reusable token logic with pluggable storage backend.
|
|
283
|
+
*/
|
|
284
|
+
declare function createStorageService(adapter: StorageAdapter): {
|
|
285
|
+
/**
|
|
286
|
+
* Save tokens to storage
|
|
287
|
+
*/
|
|
288
|
+
saveTokens(tokens: JWTData): Promise<void>;
|
|
289
|
+
/**
|
|
290
|
+
* Get tokens from storage
|
|
291
|
+
* Returns null if no tokens exist or if they're invalid
|
|
292
|
+
*/
|
|
293
|
+
getTokens(): Promise<JWTData | null>;
|
|
294
|
+
/**
|
|
295
|
+
* Get raw stored data (including metadata like stored_at)
|
|
296
|
+
*/
|
|
297
|
+
getRawData(): Promise<{
|
|
298
|
+
tokens: JWTData;
|
|
299
|
+
stored_at: number;
|
|
300
|
+
} | null>;
|
|
301
|
+
/**
|
|
302
|
+
* Clear tokens from storage
|
|
303
|
+
*/
|
|
304
|
+
clearTokens(): Promise<void>;
|
|
305
|
+
/**
|
|
306
|
+
* Check if tokens exist in storage
|
|
307
|
+
*/
|
|
308
|
+
hasTokens(): Promise<boolean>;
|
|
309
|
+
/**
|
|
310
|
+
* Check if stored tokens are expired
|
|
311
|
+
* Uses expires_in from the token data
|
|
312
|
+
*/
|
|
313
|
+
isTokenExpired(): Promise<boolean>;
|
|
314
|
+
/**
|
|
315
|
+
* Get the time until token expires (in milliseconds)
|
|
316
|
+
* Returns 0 if token is expired or doesn't exist
|
|
317
|
+
*/
|
|
318
|
+
getTimeUntilExpiry(): Promise<number>;
|
|
319
|
+
};
|
|
320
|
+
type StorageService = ReturnType<typeof createStorageService>;
|
|
321
|
+
/**
|
|
322
|
+
* Default storage service using localStorage.
|
|
323
|
+
* For custom storage, use createStorageService(yourAdapter) instead.
|
|
324
|
+
*/
|
|
325
|
+
declare const storageService: {
|
|
326
|
+
/**
|
|
327
|
+
* Save tokens to storage
|
|
328
|
+
*/
|
|
329
|
+
saveTokens(tokens: JWTData): Promise<void>;
|
|
330
|
+
/**
|
|
331
|
+
* Get tokens from storage
|
|
332
|
+
* Returns null if no tokens exist or if they're invalid
|
|
333
|
+
*/
|
|
334
|
+
getTokens(): Promise<JWTData | null>;
|
|
335
|
+
/**
|
|
336
|
+
* Get raw stored data (including metadata like stored_at)
|
|
337
|
+
*/
|
|
338
|
+
getRawData(): Promise<{
|
|
339
|
+
tokens: JWTData;
|
|
340
|
+
stored_at: number;
|
|
341
|
+
} | null>;
|
|
342
|
+
/**
|
|
343
|
+
* Clear tokens from storage
|
|
344
|
+
*/
|
|
345
|
+
clearTokens(): Promise<void>;
|
|
346
|
+
/**
|
|
347
|
+
* Check if tokens exist in storage
|
|
348
|
+
*/
|
|
349
|
+
hasTokens(): Promise<boolean>;
|
|
350
|
+
/**
|
|
351
|
+
* Check if stored tokens are expired
|
|
352
|
+
* Uses expires_in from the token data
|
|
353
|
+
*/
|
|
354
|
+
isTokenExpired(): Promise<boolean>;
|
|
355
|
+
/**
|
|
356
|
+
* Get the time until token expires (in milliseconds)
|
|
357
|
+
* Returns 0 if token is expired or doesn't exist
|
|
358
|
+
*/
|
|
359
|
+
getTimeUntilExpiry(): Promise<number>;
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Exchange provider credentials for JWT tokens from the backend.
|
|
364
|
+
* @param params - Auth parameters (id_token, code, username/password)
|
|
365
|
+
* @param provider - The auth provider type
|
|
366
|
+
* @param config - Auth configuration with backendUrl
|
|
367
|
+
*/
|
|
368
|
+
declare function exchangeToken(params: TokenRequestParams, provider: AuthProviderType, config: AuthConfig): Promise<AuthResult>;
|
|
369
|
+
/**
|
|
370
|
+
* Refresh an expired access token using the refresh token.
|
|
371
|
+
* @param refresh_token - The refresh token
|
|
372
|
+
* @param backendUrl - The backend API URL
|
|
373
|
+
*/
|
|
374
|
+
declare function refreshToken(refresh_token: string, backendUrl: string): Promise<JWTData | null>;
|
|
375
|
+
|
|
376
|
+
export { type AuthCallbacks, type AuthConfig, type AuthContextValue, type AuthError, AuthProvider, type AuthProviderType, type AuthResult, type JWTData, LoginPage, type StorageAdapter, type StorageService, createStorageService, exchangeToken, loadAppleSDK, loadGoogleSDK, localStorageAdapter, memoryStorageAdapter, refreshToken, signInWithApple, signInWithEmail, signInWithGoogle, storageService, useAuth, validateConfig };
|