@thetechfossil/auth2 1.2.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,1757 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var axios = require('axios');
5
+ var jsxRuntime = require('react/jsx-runtime');
6
+
7
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
+
9
+ var axios__default = /*#__PURE__*/_interopDefault(axios);
10
+
11
+ // src/react/components/LoginForm.tsx
12
+ var HttpClient = class {
13
+ constructor(baseUrl, defaultHeaders = {}) {
14
+ this.csrfToken = null;
15
+ this.axiosInstance = axios__default.default.create({
16
+ baseURL: baseUrl.replace(/\/$/, ""),
17
+ headers: {
18
+ "Content-Type": "application/json",
19
+ ...defaultHeaders
20
+ },
21
+ withCredentials: true,
22
+ // Include cookies for CSRF
23
+ timeout: 3e4
24
+ // 30 second timeout
25
+ });
26
+ this.axiosInstance.interceptors.request.use(
27
+ (config) => {
28
+ if (this.csrfToken && ["post", "put", "delete", "patch"].includes(config.method?.toLowerCase() || "")) {
29
+ config.headers["x-csrf-token"] = this.csrfToken;
30
+ }
31
+ return config;
32
+ },
33
+ (error) => Promise.reject(error)
34
+ );
35
+ this.axiosInstance.interceptors.response.use(
36
+ (response) => response,
37
+ async (error) => {
38
+ const originalRequest = error.config;
39
+ if (error.response?.status === 403 && !originalRequest._retry) {
40
+ originalRequest._retry = true;
41
+ try {
42
+ await this.refreshCsrfToken();
43
+ if (originalRequest.headers) {
44
+ originalRequest.headers["x-csrf-token"] = this.csrfToken;
45
+ }
46
+ return this.axiosInstance(originalRequest);
47
+ } catch (refreshError) {
48
+ return Promise.reject(refreshError);
49
+ }
50
+ }
51
+ return Promise.reject(error);
52
+ }
53
+ );
54
+ }
55
+ async get(endpoint, headers) {
56
+ const response = await this.axiosInstance.get(endpoint, { headers });
57
+ return response.data;
58
+ }
59
+ async post(endpoint, data, headers) {
60
+ const response = await this.axiosInstance.post(endpoint, data, { headers });
61
+ return response.data;
62
+ }
63
+ async put(endpoint, data, headers) {
64
+ const response = await this.axiosInstance.put(endpoint, data, { headers });
65
+ return response.data;
66
+ }
67
+ async delete(endpoint, headers) {
68
+ const response = await this.axiosInstance.delete(endpoint, { headers });
69
+ return response.data;
70
+ }
71
+ setAuthToken(token) {
72
+ this.axiosInstance.defaults.headers.common["Authorization"] = `Bearer ${token}`;
73
+ }
74
+ removeAuthToken() {
75
+ delete this.axiosInstance.defaults.headers.common["Authorization"];
76
+ }
77
+ setCsrfToken(token) {
78
+ this.csrfToken = token;
79
+ }
80
+ getCsrfToken() {
81
+ return this.csrfToken;
82
+ }
83
+ removeCsrfToken() {
84
+ this.csrfToken = null;
85
+ }
86
+ async refreshCsrfToken() {
87
+ try {
88
+ const response = await this.axiosInstance.get("/api/v1/auth/csrf-token");
89
+ this.csrfToken = response.data.csrfToken;
90
+ } catch (error) {
91
+ console.error("Failed to refresh CSRF token:", error);
92
+ throw error;
93
+ }
94
+ }
95
+ };
96
+
97
+ // src/core/auth-service.ts
98
+ var AuthService = class {
99
+ constructor(config) {
100
+ this.token = null;
101
+ this.config = {
102
+ localStorageKey: "auth_token",
103
+ csrfEnabled: true,
104
+ ...config
105
+ };
106
+ this.httpClient = new HttpClient(this.config.baseUrl);
107
+ this.loadTokenFromStorage();
108
+ if (this.config.csrfEnabled && typeof window !== "undefined") {
109
+ this.refreshCsrfToken();
110
+ }
111
+ }
112
+ loadTokenFromStorage() {
113
+ if (typeof window !== "undefined" && this.config.localStorageKey) {
114
+ try {
115
+ const token = localStorage.getItem(this.config.localStorageKey);
116
+ if (token) {
117
+ this.token = token;
118
+ this.httpClient.setAuthToken(token);
119
+ }
120
+ } catch (error) {
121
+ console.warn("Failed to load token from storage:", error);
122
+ }
123
+ }
124
+ }
125
+ saveTokenToStorage(token) {
126
+ if (typeof window !== "undefined" && this.config.localStorageKey) {
127
+ try {
128
+ localStorage.setItem(this.config.localStorageKey, token);
129
+ } catch (error) {
130
+ console.warn("Failed to save token to storage:", error);
131
+ }
132
+ }
133
+ }
134
+ removeTokenFromStorage() {
135
+ if (typeof window !== "undefined" && this.config.localStorageKey) {
136
+ try {
137
+ localStorage.removeItem(this.config.localStorageKey);
138
+ } catch (error) {
139
+ console.warn("Failed to remove token from storage:", error);
140
+ }
141
+ }
142
+ }
143
+ isAuthenticated() {
144
+ return !!this.token;
145
+ }
146
+ getToken() {
147
+ return this.token;
148
+ }
149
+ getCurrentUser() {
150
+ if (!this.token)
151
+ return null;
152
+ try {
153
+ const payload = JSON.parse(atob(this.token.split(".")[1]));
154
+ return payload.user || null;
155
+ } catch (error) {
156
+ console.error("Failed to parse user from token:", error);
157
+ return null;
158
+ }
159
+ }
160
+ // CSRF Token Management
161
+ async refreshCsrfToken() {
162
+ if (!this.config.csrfEnabled)
163
+ return;
164
+ try {
165
+ const response = await this.httpClient.get("/api/v1/auth/csrf-token");
166
+ if (response.csrfToken) {
167
+ this.httpClient.setCsrfToken(response.csrfToken);
168
+ }
169
+ } catch (error) {
170
+ console.error("Failed to get CSRF token:", error);
171
+ }
172
+ }
173
+ getCsrfToken() {
174
+ return this.httpClient.getCsrfToken();
175
+ }
176
+ // OAuth Methods
177
+ loginWithOAuth(provider) {
178
+ if (typeof window === "undefined") {
179
+ throw new Error("OAuth login is only available in browser environments");
180
+ }
181
+ const oauthUrl = `${this.config.baseUrl}/api/v1/auth/oauth/${provider}`;
182
+ window.location.href = oauthUrl;
183
+ }
184
+ linkOAuthProvider(provider) {
185
+ if (typeof window === "undefined") {
186
+ throw new Error("OAuth linking is only available in browser environments");
187
+ }
188
+ if (!this.token) {
189
+ throw new Error("Must be authenticated to link OAuth provider");
190
+ }
191
+ const linkUrl = `${this.config.baseUrl}/api/v1/auth/oauth/${provider}/link`;
192
+ window.location.href = linkUrl;
193
+ }
194
+ async unlinkOAuthProvider(provider) {
195
+ if (!this.token) {
196
+ throw new Error("Not authenticated");
197
+ }
198
+ const response = await this.httpClient.delete(
199
+ `/api/v1/auth/oauth/${provider}/unlink`
200
+ );
201
+ return response;
202
+ }
203
+ // Standard Auth Methods
204
+ async login(data) {
205
+ const response = await this.httpClient.post("/api/v1/auth/login", data);
206
+ if (response.success && response.token) {
207
+ this.token = response.token;
208
+ this.httpClient.setAuthToken(response.token);
209
+ this.saveTokenToStorage(response.token);
210
+ return response;
211
+ }
212
+ if (response.success && response.message === "OTP sent to your email.") {
213
+ return response;
214
+ }
215
+ if (response.success && response.message === "OTP verified successfully." && response.token) {
216
+ this.token = response.token;
217
+ this.httpClient.setAuthToken(response.token);
218
+ this.saveTokenToStorage(response.token);
219
+ return response;
220
+ }
221
+ throw new Error(response.message || "Login failed");
222
+ }
223
+ async register(data) {
224
+ const registerData = { ...data };
225
+ if (!registerData.frontendBaseUrl && typeof window !== "undefined") {
226
+ registerData.frontendBaseUrl = process.env.FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_APP_URL || window.location.origin;
227
+ }
228
+ const response = await this.httpClient.post("/api/v1/auth/register", registerData);
229
+ if (response.success && response.message === "Registration data saved. Verification email sent. Please check your inbox.") {
230
+ return response;
231
+ }
232
+ throw new Error(response.message || "Registration failed");
233
+ }
234
+ async verify(data) {
235
+ const response = await this.httpClient.post("/api/v1/auth/verify", data);
236
+ if (response.success && response.token) {
237
+ this.token = response.token;
238
+ this.httpClient.setAuthToken(response.token);
239
+ this.saveTokenToStorage(response.token);
240
+ }
241
+ return response;
242
+ }
243
+ async verifyEmailToken(token) {
244
+ const response = await this.httpClient.get(`/api/v1/auth/verify-email?token=${token}`);
245
+ if (response.success && response.token) {
246
+ this.token = response.token;
247
+ this.httpClient.setAuthToken(response.token);
248
+ this.saveTokenToStorage(response.token);
249
+ }
250
+ return response;
251
+ }
252
+ async logout() {
253
+ this.token = null;
254
+ this.httpClient.removeAuthToken();
255
+ this.httpClient.removeCsrfToken();
256
+ this.removeTokenFromStorage();
257
+ }
258
+ async getProfile() {
259
+ if (!this.token) {
260
+ throw new Error("Not authenticated");
261
+ }
262
+ const response = await this.httpClient.get("/api/v1/user/me");
263
+ return response.user;
264
+ }
265
+ async updateProfile(data) {
266
+ if (!this.token) {
267
+ throw new Error("Not authenticated");
268
+ }
269
+ const response = await this.httpClient.post("/api/v1/user/update/user", data);
270
+ if (response.success && response.token) {
271
+ this.token = response.token;
272
+ this.httpClient.setAuthToken(response.token);
273
+ this.saveTokenToStorage(response.token);
274
+ }
275
+ return response;
276
+ }
277
+ async getAllUsers() {
278
+ if (!this.token) {
279
+ throw new Error("Not authenticated");
280
+ }
281
+ const response = await this.httpClient.get("/api/v1/user/all");
282
+ return response.users;
283
+ }
284
+ async getUserById(id) {
285
+ const response = await this.httpClient.get(`/api/v1/user/${id}`);
286
+ return response.user;
287
+ }
288
+ async forgotPassword(email) {
289
+ if (typeof window !== "undefined") {
290
+ process.env.FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_APP_URL || window.location.origin;
291
+ }
292
+ const response = await this.httpClient.post("/api/v1/auth/forgot-password", { email });
293
+ return response;
294
+ }
295
+ async resetPassword(token, password) {
296
+ const response = await this.httpClient.post("/api/v1/auth/reset-password", { token, password });
297
+ return response;
298
+ }
299
+ };
300
+
301
+ // src/react/use-auth.ts
302
+ var useAuth = (config) => {
303
+ const [authService] = react.useState(() => new AuthService(config));
304
+ const [user, setUser] = react.useState(null);
305
+ const [isAuthenticated, setIsAuthenticated] = react.useState(false);
306
+ const [loading, setLoading] = react.useState(true);
307
+ const checkAuthStatus = react.useCallback(() => {
308
+ const authenticated = authService.isAuthenticated();
309
+ setIsAuthenticated(authenticated);
310
+ if (authenticated) {
311
+ const currentUser = authService.getCurrentUser();
312
+ setUser(currentUser);
313
+ } else {
314
+ setUser(null);
315
+ }
316
+ setLoading(false);
317
+ }, [authService]);
318
+ react.useEffect(() => {
319
+ checkAuthStatus();
320
+ }, [checkAuthStatus]);
321
+ const register = react.useCallback(async (data) => {
322
+ setLoading(true);
323
+ try {
324
+ const registerData = { ...data };
325
+ if (!registerData.frontendBaseUrl && typeof window !== "undefined") {
326
+ registerData.frontendBaseUrl = process.env.REACT_APP_FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_APP_URL || window.location.origin;
327
+ }
328
+ const response = await authService.register(registerData);
329
+ return response;
330
+ } finally {
331
+ setLoading(false);
332
+ }
333
+ }, [authService]);
334
+ const login = react.useCallback(async (data) => {
335
+ setLoading(true);
336
+ try {
337
+ const response = await authService.login(data);
338
+ if (response.success && response.user) {
339
+ setUser(response.user);
340
+ setIsAuthenticated(true);
341
+ }
342
+ return response;
343
+ } finally {
344
+ setLoading(false);
345
+ }
346
+ }, [authService]);
347
+ const verify = react.useCallback(async (data) => {
348
+ setLoading(true);
349
+ try {
350
+ const response = await authService.verify(data);
351
+ if (response.success && response.user) {
352
+ setUser(response.user);
353
+ setIsAuthenticated(true);
354
+ }
355
+ return response;
356
+ } finally {
357
+ setLoading(false);
358
+ }
359
+ }, [authService]);
360
+ const verifyEmailToken = react.useCallback(async (token) => {
361
+ setLoading(true);
362
+ try {
363
+ const response = await authService.verifyEmailToken(token);
364
+ if (response.success && response.user) {
365
+ setUser(response.user);
366
+ setIsAuthenticated(true);
367
+ }
368
+ return response;
369
+ } finally {
370
+ setLoading(false);
371
+ }
372
+ }, [authService]);
373
+ const logout = react.useCallback(async () => {
374
+ setLoading(true);
375
+ try {
376
+ await authService.logout();
377
+ setUser(null);
378
+ setIsAuthenticated(false);
379
+ } finally {
380
+ setLoading(false);
381
+ }
382
+ }, [authService]);
383
+ const updateProfile = react.useCallback(async (data) => {
384
+ setLoading(true);
385
+ try {
386
+ const response = await authService.updateProfile(data);
387
+ if (response.success && response.user) {
388
+ setUser(response.user);
389
+ }
390
+ return response;
391
+ } finally {
392
+ setLoading(false);
393
+ }
394
+ }, [authService]);
395
+ const getProfile = react.useCallback(async () => {
396
+ setLoading(true);
397
+ try {
398
+ const userData = await authService.getProfile();
399
+ setUser(userData);
400
+ return userData;
401
+ } finally {
402
+ setLoading(false);
403
+ }
404
+ }, [authService]);
405
+ const getAllUsers = react.useCallback(async () => {
406
+ setLoading(true);
407
+ try {
408
+ return await authService.getAllUsers();
409
+ } finally {
410
+ setLoading(false);
411
+ }
412
+ }, [authService]);
413
+ const getUserById = react.useCallback(async (id) => {
414
+ setLoading(true);
415
+ try {
416
+ return await authService.getUserById(id);
417
+ } finally {
418
+ setLoading(false);
419
+ }
420
+ }, [authService]);
421
+ return {
422
+ user,
423
+ isAuthenticated,
424
+ loading,
425
+ register,
426
+ login,
427
+ verify,
428
+ verifyEmailToken,
429
+ logout,
430
+ updateProfile,
431
+ getProfile,
432
+ getAllUsers,
433
+ getUserById
434
+ };
435
+ };
436
+ var LoginForm = ({
437
+ onSuccess,
438
+ onLoginSuccess,
439
+ onRegisterClick,
440
+ showRegisterLink = true,
441
+ config,
442
+ oauthProviders = ["google", "github"],
443
+ showOAuthButtons = true
444
+ }) => {
445
+ const [email, setEmail] = react.useState("");
446
+ const [password, setPassword] = react.useState("");
447
+ const [usePassword, setUsePassword] = react.useState(false);
448
+ const [showPassword, setShowPassword] = react.useState(false);
449
+ const [isLoading, setIsLoading] = react.useState(false);
450
+ const [error, setError] = react.useState(null);
451
+ const [rememberMe, setRememberMe] = react.useState(false);
452
+ const { login } = useAuth({
453
+ baseUrl: config?.baseUrl || (typeof window !== "undefined" ? window.location.origin : "http://localhost:7000")
454
+ });
455
+ const handleSubmit = async (e) => {
456
+ e.preventDefault();
457
+ setIsLoading(true);
458
+ setError(null);
459
+ try {
460
+ let response;
461
+ if (usePassword) {
462
+ response = await login({ email, password });
463
+ } else {
464
+ response = await login({ email });
465
+ }
466
+ if (response.success) {
467
+ onSuccess?.(response);
468
+ if (onLoginSuccess) {
469
+ if (response.message === "OTP sent to your email.") {
470
+ onLoginSuccess(email, true);
471
+ } else if (response.token) {
472
+ onLoginSuccess(email, false);
473
+ } else {
474
+ onLoginSuccess(email, true);
475
+ }
476
+ }
477
+ } else {
478
+ setError(response.message || "Login failed");
479
+ }
480
+ } catch (err) {
481
+ setError(err instanceof Error ? err.message : "An unknown error occurred");
482
+ } finally {
483
+ setIsLoading(false);
484
+ }
485
+ };
486
+ const toggleAuthMethod = () => {
487
+ setUsePassword(!usePassword);
488
+ setError(null);
489
+ };
490
+ const togglePasswordVisibility = () => {
491
+ setShowPassword(!showPassword);
492
+ };
493
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
494
+ maxWidth: "400px",
495
+ margin: "0 auto",
496
+ padding: "30px",
497
+ borderRadius: "12px",
498
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
499
+ backgroundColor: "#ffffff",
500
+ border: "1px solid #eaeaea"
501
+ }, children: /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: handleSubmit, style: {
502
+ display: "flex",
503
+ flexDirection: "column"
504
+ }, children: [
505
+ /* @__PURE__ */ jsxRuntime.jsx("h2", { style: {
506
+ textAlign: "center",
507
+ marginBottom: "24px",
508
+ color: "#1f2937",
509
+ fontSize: "24px",
510
+ fontWeight: 600
511
+ }, children: usePassword ? "Login with Password" : "Login with OTP" }),
512
+ error && /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
513
+ padding: "12px 16px",
514
+ marginBottom: "20px",
515
+ backgroundColor: "#f8d7da",
516
+ color: "#721c24",
517
+ border: "1px solid #f5c6cb",
518
+ borderRadius: "8px",
519
+ fontSize: "14px",
520
+ fontWeight: 500
521
+ }, children: error }),
522
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
523
+ marginBottom: "20px"
524
+ }, children: [
525
+ /* @__PURE__ */ jsxRuntime.jsx("label", { htmlFor: "email", style: {
526
+ display: "block",
527
+ marginBottom: "8px",
528
+ fontWeight: 500,
529
+ color: "#374151",
530
+ fontSize: "14px"
531
+ }, children: "Email:" }),
532
+ /* @__PURE__ */ jsxRuntime.jsx(
533
+ "input",
534
+ {
535
+ id: "email",
536
+ type: "email",
537
+ value: email,
538
+ onChange: (e) => setEmail(e.target.value),
539
+ required: true,
540
+ disabled: isLoading,
541
+ style: {
542
+ width: "100%",
543
+ padding: "12px 16px",
544
+ border: "1px solid #ddd",
545
+ borderRadius: "8px",
546
+ fontSize: "16px",
547
+ boxSizing: "border-box",
548
+ color: "#111827",
549
+ transition: "all 0.2s ease",
550
+ backgroundColor: "#ffffff"
551
+ },
552
+ placeholder: "Enter your email"
553
+ }
554
+ )
555
+ ] }),
556
+ usePassword && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
557
+ marginBottom: "20px",
558
+ position: "relative"
559
+ }, children: [
560
+ /* @__PURE__ */ jsxRuntime.jsx("label", { htmlFor: "login-password", style: {
561
+ display: "block",
562
+ marginBottom: "8px",
563
+ fontWeight: 500,
564
+ color: "#555",
565
+ fontSize: "14px"
566
+ }, children: "Password:" }),
567
+ /* @__PURE__ */ jsxRuntime.jsx(
568
+ "input",
569
+ {
570
+ id: "login-password",
571
+ type: showPassword ? "text" : "password",
572
+ value: password,
573
+ onChange: (e) => setPassword(e.target.value),
574
+ required: true,
575
+ disabled: isLoading,
576
+ style: {
577
+ width: "100%",
578
+ padding: "12px 16px",
579
+ border: "1px solid #ddd",
580
+ borderRadius: "8px",
581
+ fontSize: "16px",
582
+ boxSizing: "border-box",
583
+ color: "#333",
584
+ transition: "all 0.2s ease",
585
+ backgroundColor: "#fafafa"
586
+ },
587
+ placeholder: "Enter your password"
588
+ }
589
+ ),
590
+ /* @__PURE__ */ jsxRuntime.jsx(
591
+ "button",
592
+ {
593
+ type: "button",
594
+ onClick: togglePasswordVisibility,
595
+ disabled: isLoading,
596
+ style: {
597
+ position: "absolute",
598
+ right: "12px",
599
+ top: "38px",
600
+ background: "none",
601
+ border: "none",
602
+ cursor: "pointer",
603
+ color: "#666",
604
+ fontSize: "14px"
605
+ },
606
+ children: showPassword ? "Hide" : "Show"
607
+ }
608
+ )
609
+ ] }),
610
+ usePassword && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
611
+ display: "flex",
612
+ alignItems: "center",
613
+ marginBottom: "16px",
614
+ marginTop: "8px"
615
+ }, children: [
616
+ /* @__PURE__ */ jsxRuntime.jsx(
617
+ "input",
618
+ {
619
+ type: "checkbox",
620
+ id: "remember-me",
621
+ checked: rememberMe,
622
+ onChange: (e) => setRememberMe(e.target.checked),
623
+ disabled: isLoading,
624
+ style: {
625
+ marginRight: "8px",
626
+ cursor: "pointer",
627
+ width: "16px",
628
+ height: "16px"
629
+ }
630
+ }
631
+ ),
632
+ /* @__PURE__ */ jsxRuntime.jsx(
633
+ "label",
634
+ {
635
+ htmlFor: "remember-me",
636
+ style: {
637
+ fontSize: "14px",
638
+ color: "#666",
639
+ cursor: "pointer",
640
+ userSelect: "none"
641
+ },
642
+ children: "Remember me"
643
+ }
644
+ )
645
+ ] }),
646
+ /* @__PURE__ */ jsxRuntime.jsx(
647
+ "button",
648
+ {
649
+ type: "submit",
650
+ disabled: isLoading,
651
+ style: {
652
+ padding: "14px",
653
+ backgroundColor: "#007bff",
654
+ color: "white",
655
+ border: "none",
656
+ borderRadius: "8px",
657
+ fontSize: "16px",
658
+ fontWeight: 600,
659
+ cursor: "pointer",
660
+ transition: "all 0.2s ease",
661
+ marginTop: "8px"
662
+ },
663
+ children: isLoading ? usePassword ? "Logging in..." : "Sending OTP..." : usePassword ? "Login" : "Send OTP"
664
+ }
665
+ ),
666
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
667
+ textAlign: "center",
668
+ marginTop: "20px",
669
+ paddingTop: "20px",
670
+ borderTop: "1px solid #eee"
671
+ }, children: /* @__PURE__ */ jsxRuntime.jsx(
672
+ "button",
673
+ {
674
+ type: "button",
675
+ onClick: toggleAuthMethod,
676
+ disabled: isLoading,
677
+ style: {
678
+ background: "none",
679
+ border: "none",
680
+ color: "#007bff",
681
+ textDecoration: "none",
682
+ cursor: "pointer",
683
+ fontSize: "14px",
684
+ fontWeight: 600,
685
+ padding: "0",
686
+ transition: "color 0.2s ease"
687
+ },
688
+ children: usePassword ? "Login with OTP instead" : "Login with password instead"
689
+ }
690
+ ) }),
691
+ showOAuthButtons && oauthProviders.length > 0 && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
692
+ marginTop: "20px",
693
+ paddingTop: "20px",
694
+ borderTop: "1px solid #eee"
695
+ }, children: [
696
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
697
+ position: "relative",
698
+ marginBottom: "16px"
699
+ }, children: [
700
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
701
+ position: "absolute",
702
+ top: "50%",
703
+ left: 0,
704
+ right: 0,
705
+ height: "1px",
706
+ backgroundColor: "#eee"
707
+ } }),
708
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
709
+ position: "relative",
710
+ textAlign: "center"
711
+ }, children: /* @__PURE__ */ jsxRuntime.jsx("span", { style: {
712
+ backgroundColor: "#ffffff",
713
+ padding: "0 12px",
714
+ color: "#666",
715
+ fontSize: "14px"
716
+ }, children: "Or continue with" }) })
717
+ ] }),
718
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
719
+ display: "grid",
720
+ gridTemplateColumns: oauthProviders.length === 1 ? "1fr" : "repeat(2, 1fr)",
721
+ gap: "12px"
722
+ }, children: [
723
+ oauthProviders.includes("google") && /* @__PURE__ */ jsxRuntime.jsxs(
724
+ "a",
725
+ {
726
+ href: `${config?.baseUrl || "http://localhost:7000"}/api/v1/auth/oauth/google`,
727
+ style: {
728
+ display: "flex",
729
+ alignItems: "center",
730
+ justifyContent: "center",
731
+ gap: "8px",
732
+ padding: "10px 16px",
733
+ backgroundColor: "#ffffff",
734
+ border: "1px solid #ddd",
735
+ borderRadius: "8px",
736
+ color: "#333",
737
+ textDecoration: "none",
738
+ fontSize: "14px",
739
+ fontWeight: 500,
740
+ cursor: "pointer",
741
+ transition: "all 0.2s ease"
742
+ },
743
+ onMouseEnter: (e) => {
744
+ e.currentTarget.style.backgroundColor = "#f8f8f8";
745
+ e.currentTarget.style.borderColor = "#007bff";
746
+ },
747
+ onMouseLeave: (e) => {
748
+ e.currentTarget.style.backgroundColor = "#ffffff";
749
+ e.currentTarget.style.borderColor = "#ddd";
750
+ },
751
+ children: [
752
+ /* @__PURE__ */ jsxRuntime.jsxs("svg", { style: { width: "18px", height: "18px" }, viewBox: "0 0 24 24", children: [
753
+ /* @__PURE__ */ jsxRuntime.jsx(
754
+ "path",
755
+ {
756
+ fill: "#4285F4",
757
+ d: "M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
758
+ }
759
+ ),
760
+ /* @__PURE__ */ jsxRuntime.jsx(
761
+ "path",
762
+ {
763
+ fill: "#34A853",
764
+ d: "M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
765
+ }
766
+ ),
767
+ /* @__PURE__ */ jsxRuntime.jsx(
768
+ "path",
769
+ {
770
+ fill: "#FBBC05",
771
+ d: "M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
772
+ }
773
+ ),
774
+ /* @__PURE__ */ jsxRuntime.jsx(
775
+ "path",
776
+ {
777
+ fill: "#EA4335",
778
+ d: "M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
779
+ }
780
+ )
781
+ ] }),
782
+ "Google"
783
+ ]
784
+ }
785
+ ),
786
+ oauthProviders.includes("github") && /* @__PURE__ */ jsxRuntime.jsxs(
787
+ "a",
788
+ {
789
+ href: `${config?.baseUrl || "http://localhost:7000"}/api/v1/auth/oauth/github`,
790
+ style: {
791
+ display: "flex",
792
+ alignItems: "center",
793
+ justifyContent: "center",
794
+ gap: "8px",
795
+ padding: "10px 16px",
796
+ backgroundColor: "#24292e",
797
+ border: "1px solid #24292e",
798
+ borderRadius: "8px",
799
+ color: "#ffffff",
800
+ textDecoration: "none",
801
+ fontSize: "14px",
802
+ fontWeight: 500,
803
+ cursor: "pointer",
804
+ transition: "all 0.2s ease"
805
+ },
806
+ onMouseEnter: (e) => {
807
+ e.currentTarget.style.backgroundColor = "#1b1f23";
808
+ e.currentTarget.style.borderColor = "#1b1f23";
809
+ },
810
+ onMouseLeave: (e) => {
811
+ e.currentTarget.style.backgroundColor = "#24292e";
812
+ e.currentTarget.style.borderColor = "#24292e";
813
+ },
814
+ children: [
815
+ /* @__PURE__ */ jsxRuntime.jsx("svg", { style: { width: "18px", height: "18px" }, fill: "currentColor", viewBox: "0 0 24 24", children: /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z" }) }),
816
+ "GitHub"
817
+ ]
818
+ }
819
+ )
820
+ ] })
821
+ ] }),
822
+ showRegisterLink && /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
823
+ textAlign: "center",
824
+ marginTop: "20px",
825
+ paddingTop: "20px",
826
+ borderTop: "1px solid #eee"
827
+ }, children: /* @__PURE__ */ jsxRuntime.jsxs("p", { style: {
828
+ color: "#666",
829
+ fontSize: "14px"
830
+ }, children: [
831
+ "Don't have an account?",
832
+ " ",
833
+ /* @__PURE__ */ jsxRuntime.jsx(
834
+ "button",
835
+ {
836
+ type: "button",
837
+ onClick: onRegisterClick,
838
+ disabled: isLoading,
839
+ style: {
840
+ background: "none",
841
+ border: "none",
842
+ color: "#007bff",
843
+ textDecoration: "none",
844
+ cursor: "pointer",
845
+ fontSize: "14px",
846
+ fontWeight: 600,
847
+ padding: "0",
848
+ transition: "color 0.2s ease"
849
+ },
850
+ children: "Register"
851
+ }
852
+ )
853
+ ] }) })
854
+ ] }) });
855
+ };
856
+ var RegisterForm = ({
857
+ onRegisterSuccess,
858
+ onLoginClick,
859
+ showLoginLink = true,
860
+ authConfig,
861
+ oauthProviders = ["google", "github"],
862
+ showOAuthButtons = true
863
+ }) => {
864
+ const [name, setName] = react.useState("");
865
+ const [email, setEmail] = react.useState("");
866
+ const [password, setPassword] = react.useState("");
867
+ const [confirmPassword, setConfirmPassword] = react.useState("");
868
+ const [isLoading, setIsLoading] = react.useState(false);
869
+ const [error, setError] = react.useState(null);
870
+ react.useState(false);
871
+ react.useState(false);
872
+ const getPasswordStrength = (pwd) => {
873
+ if (!pwd)
874
+ return { strength: "weak", score: 0, label: "" };
875
+ let score = 0;
876
+ if (pwd.length >= 6)
877
+ score++;
878
+ if (pwd.length >= 8)
879
+ score++;
880
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
881
+ score++;
882
+ if (/\d/.test(pwd))
883
+ score++;
884
+ if (/[^a-zA-Z\d]/.test(pwd))
885
+ score++;
886
+ if (score <= 2)
887
+ return { strength: "weak", score, label: "Weak" };
888
+ if (score <= 3)
889
+ return { strength: "medium", score, label: "Medium" };
890
+ return { strength: "strong", score, label: "Strong" };
891
+ };
892
+ getPasswordStrength(password);
893
+ const config = authConfig || {
894
+ baseUrl: typeof window !== "undefined" ? process.env.NEXT_PUBLIC_AUTH_API_URL || window.location.origin : "http://localhost:7000"
895
+ };
896
+ const { register } = useAuth(config);
897
+ const handleSubmit = async (e) => {
898
+ e.preventDefault();
899
+ setIsLoading(true);
900
+ setError(null);
901
+ if (password !== confirmPassword) {
902
+ setError("Passwords do not match");
903
+ setIsLoading(false);
904
+ return;
905
+ }
906
+ if (password.length < 6) {
907
+ setError("Password must be at least 6 characters long");
908
+ setIsLoading(false);
909
+ return;
910
+ }
911
+ try {
912
+ const registerData = {
913
+ name,
914
+ email,
915
+ password,
916
+ frontendBaseUrl: typeof window !== "undefined" ? process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || window.location.origin : void 0
917
+ };
918
+ const response = await register(registerData);
919
+ if (response.success) {
920
+ onRegisterSuccess?.();
921
+ } else {
922
+ setError(response.message || "Registration failed");
923
+ }
924
+ } catch (err) {
925
+ setError(err instanceof Error ? err.message : "An unknown error occurred");
926
+ } finally {
927
+ setIsLoading(false);
928
+ }
929
+ };
930
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
931
+ maxWidth: "400px",
932
+ margin: "0 auto",
933
+ padding: "30px",
934
+ borderRadius: "12px",
935
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
936
+ backgroundColor: "#ffffff",
937
+ border: "1px solid #eaeaea"
938
+ }, children: /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: handleSubmit, style: {
939
+ display: "flex",
940
+ flexDirection: "column"
941
+ }, children: [
942
+ /* @__PURE__ */ jsxRuntime.jsx("h2", { style: {
943
+ textAlign: "center",
944
+ marginBottom: "24px",
945
+ color: "#1f2937",
946
+ fontSize: "24px",
947
+ fontWeight: 600
948
+ }, children: "Register" }),
949
+ error && /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
950
+ padding: "12px 16px",
951
+ marginBottom: "20px",
952
+ backgroundColor: "#f8d7da",
953
+ color: "#721c24",
954
+ border: "1px solid #f5c6cb",
955
+ borderRadius: "8px",
956
+ fontSize: "14px",
957
+ fontWeight: 500
958
+ }, children: error }),
959
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
960
+ marginBottom: "20px"
961
+ }, children: [
962
+ /* @__PURE__ */ jsxRuntime.jsx("label", { htmlFor: "name", style: {
963
+ display: "block",
964
+ marginBottom: "8px",
965
+ fontWeight: 500,
966
+ color: "#374151",
967
+ fontSize: "14px"
968
+ }, children: "Name:" }),
969
+ /* @__PURE__ */ jsxRuntime.jsx(
970
+ "input",
971
+ {
972
+ id: "name",
973
+ type: "text",
974
+ value: name,
975
+ onChange: (e) => setName(e.target.value),
976
+ required: true,
977
+ disabled: isLoading,
978
+ style: {
979
+ width: "100%",
980
+ padding: "12px 16px",
981
+ border: "1px solid #ddd",
982
+ borderRadius: "8px",
983
+ fontSize: "16px",
984
+ boxSizing: "border-box",
985
+ color: "#111827",
986
+ transition: "all 0.2s ease",
987
+ backgroundColor: "#ffffff"
988
+ },
989
+ placeholder: "Enter your name"
990
+ }
991
+ )
992
+ ] }),
993
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
994
+ marginBottom: "20px"
995
+ }, children: [
996
+ /* @__PURE__ */ jsxRuntime.jsx("label", { htmlFor: "register-email", style: {
997
+ display: "block",
998
+ marginBottom: "8px",
999
+ fontWeight: 500,
1000
+ color: "#555",
1001
+ fontSize: "14px"
1002
+ }, children: "Email:" }),
1003
+ /* @__PURE__ */ jsxRuntime.jsx(
1004
+ "input",
1005
+ {
1006
+ id: "register-email",
1007
+ type: "email",
1008
+ value: email,
1009
+ onChange: (e) => setEmail(e.target.value),
1010
+ required: true,
1011
+ disabled: isLoading,
1012
+ style: {
1013
+ width: "100%",
1014
+ padding: "12px 16px",
1015
+ border: "1px solid #ddd",
1016
+ borderRadius: "8px",
1017
+ fontSize: "16px",
1018
+ boxSizing: "border-box",
1019
+ color: "#333",
1020
+ transition: "all 0.2s ease",
1021
+ backgroundColor: "#fafafa"
1022
+ },
1023
+ placeholder: "Enter your email"
1024
+ }
1025
+ )
1026
+ ] }),
1027
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
1028
+ marginBottom: "20px"
1029
+ }, children: [
1030
+ /* @__PURE__ */ jsxRuntime.jsx("label", { htmlFor: "password", style: {
1031
+ display: "block",
1032
+ marginBottom: "8px",
1033
+ fontWeight: 500,
1034
+ color: "#555",
1035
+ fontSize: "14px"
1036
+ }, children: "Password:" }),
1037
+ /* @__PURE__ */ jsxRuntime.jsx(
1038
+ "input",
1039
+ {
1040
+ id: "password",
1041
+ type: "password",
1042
+ value: password,
1043
+ onChange: (e) => setPassword(e.target.value),
1044
+ required: true,
1045
+ disabled: isLoading,
1046
+ style: {
1047
+ width: "100%",
1048
+ padding: "12px 16px",
1049
+ border: "1px solid #ddd",
1050
+ borderRadius: "8px",
1051
+ fontSize: "16px",
1052
+ boxSizing: "border-box",
1053
+ color: "#333",
1054
+ transition: "all 0.2s ease",
1055
+ backgroundColor: "#fafafa"
1056
+ },
1057
+ placeholder: "Enter your password",
1058
+ minLength: 6
1059
+ }
1060
+ )
1061
+ ] }),
1062
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
1063
+ marginBottom: "20px"
1064
+ }, children: [
1065
+ /* @__PURE__ */ jsxRuntime.jsx("label", { htmlFor: "confirm-password", style: {
1066
+ display: "block",
1067
+ marginBottom: "8px",
1068
+ fontWeight: 500,
1069
+ color: "#555",
1070
+ fontSize: "14px"
1071
+ }, children: "Confirm Password:" }),
1072
+ /* @__PURE__ */ jsxRuntime.jsx(
1073
+ "input",
1074
+ {
1075
+ id: "confirm-password",
1076
+ type: "password",
1077
+ value: confirmPassword,
1078
+ onChange: (e) => setConfirmPassword(e.target.value),
1079
+ required: true,
1080
+ disabled: isLoading,
1081
+ style: {
1082
+ width: "100%",
1083
+ padding: "12px 16px",
1084
+ border: "1px solid #ddd",
1085
+ borderRadius: "8px",
1086
+ fontSize: "16px",
1087
+ boxSizing: "border-box",
1088
+ color: "#333",
1089
+ transition: "all 0.2s ease",
1090
+ backgroundColor: "#fafafa"
1091
+ },
1092
+ placeholder: "Confirm your password"
1093
+ }
1094
+ )
1095
+ ] }),
1096
+ /* @__PURE__ */ jsxRuntime.jsx(
1097
+ "button",
1098
+ {
1099
+ type: "submit",
1100
+ disabled: isLoading,
1101
+ style: {
1102
+ padding: "14px",
1103
+ backgroundColor: "#007bff",
1104
+ color: "white",
1105
+ border: "none",
1106
+ borderRadius: "8px",
1107
+ fontSize: "16px",
1108
+ fontWeight: 600,
1109
+ cursor: "pointer",
1110
+ transition: "all 0.2s ease",
1111
+ marginTop: "8px"
1112
+ },
1113
+ children: isLoading ? "Registering..." : "Register"
1114
+ }
1115
+ ),
1116
+ showOAuthButtons && oauthProviders.length > 0 && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
1117
+ marginTop: "20px",
1118
+ paddingTop: "20px",
1119
+ borderTop: "1px solid #eee"
1120
+ }, children: [
1121
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
1122
+ position: "relative",
1123
+ marginBottom: "16px"
1124
+ }, children: [
1125
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1126
+ position: "absolute",
1127
+ top: "50%",
1128
+ left: 0,
1129
+ right: 0,
1130
+ height: "1px",
1131
+ backgroundColor: "#eee"
1132
+ } }),
1133
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1134
+ position: "relative",
1135
+ textAlign: "center"
1136
+ }, children: /* @__PURE__ */ jsxRuntime.jsx("span", { style: {
1137
+ backgroundColor: "#ffffff",
1138
+ padding: "0 12px",
1139
+ color: "#666",
1140
+ fontSize: "14px"
1141
+ }, children: "Or continue with" }) })
1142
+ ] }),
1143
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
1144
+ display: "grid",
1145
+ gridTemplateColumns: oauthProviders.length === 1 ? "1fr" : "repeat(2, 1fr)",
1146
+ gap: "12px"
1147
+ }, children: [
1148
+ oauthProviders.includes("google") && /* @__PURE__ */ jsxRuntime.jsxs(
1149
+ "a",
1150
+ {
1151
+ href: `${config.baseUrl}/api/v1/auth/oauth/google`,
1152
+ style: {
1153
+ display: "flex",
1154
+ alignItems: "center",
1155
+ justifyContent: "center",
1156
+ gap: "8px",
1157
+ padding: "10px 16px",
1158
+ backgroundColor: "#ffffff",
1159
+ border: "1px solid #ddd",
1160
+ borderRadius: "8px",
1161
+ color: "#333",
1162
+ textDecoration: "none",
1163
+ fontSize: "14px",
1164
+ fontWeight: 500,
1165
+ cursor: "pointer",
1166
+ transition: "all 0.2s ease"
1167
+ },
1168
+ onMouseEnter: (e) => {
1169
+ e.currentTarget.style.backgroundColor = "#f8f8f8";
1170
+ e.currentTarget.style.borderColor = "#007bff";
1171
+ },
1172
+ onMouseLeave: (e) => {
1173
+ e.currentTarget.style.backgroundColor = "#ffffff";
1174
+ e.currentTarget.style.borderColor = "#ddd";
1175
+ },
1176
+ children: [
1177
+ /* @__PURE__ */ jsxRuntime.jsxs("svg", { style: { width: "18px", height: "18px" }, viewBox: "0 0 24 24", children: [
1178
+ /* @__PURE__ */ jsxRuntime.jsx(
1179
+ "path",
1180
+ {
1181
+ fill: "#4285F4",
1182
+ d: "M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
1183
+ }
1184
+ ),
1185
+ /* @__PURE__ */ jsxRuntime.jsx(
1186
+ "path",
1187
+ {
1188
+ fill: "#34A853",
1189
+ d: "M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
1190
+ }
1191
+ ),
1192
+ /* @__PURE__ */ jsxRuntime.jsx(
1193
+ "path",
1194
+ {
1195
+ fill: "#FBBC05",
1196
+ d: "M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
1197
+ }
1198
+ ),
1199
+ /* @__PURE__ */ jsxRuntime.jsx(
1200
+ "path",
1201
+ {
1202
+ fill: "#EA4335",
1203
+ d: "M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
1204
+ }
1205
+ )
1206
+ ] }),
1207
+ "Google"
1208
+ ]
1209
+ }
1210
+ ),
1211
+ oauthProviders.includes("github") && /* @__PURE__ */ jsxRuntime.jsxs(
1212
+ "a",
1213
+ {
1214
+ href: `${config.baseUrl}/api/v1/auth/oauth/github`,
1215
+ style: {
1216
+ display: "flex",
1217
+ alignItems: "center",
1218
+ justifyContent: "center",
1219
+ gap: "8px",
1220
+ padding: "10px 16px",
1221
+ backgroundColor: "#24292e",
1222
+ border: "1px solid #24292e",
1223
+ borderRadius: "8px",
1224
+ color: "#ffffff",
1225
+ textDecoration: "none",
1226
+ fontSize: "14px",
1227
+ fontWeight: 500,
1228
+ cursor: "pointer",
1229
+ transition: "all 0.2s ease"
1230
+ },
1231
+ onMouseEnter: (e) => {
1232
+ e.currentTarget.style.backgroundColor = "#1b1f23";
1233
+ e.currentTarget.style.borderColor = "#1b1f23";
1234
+ },
1235
+ onMouseLeave: (e) => {
1236
+ e.currentTarget.style.backgroundColor = "#24292e";
1237
+ e.currentTarget.style.borderColor = "#24292e";
1238
+ },
1239
+ children: [
1240
+ /* @__PURE__ */ jsxRuntime.jsx("svg", { style: { width: "18px", height: "18px" }, fill: "currentColor", viewBox: "0 0 24 24", children: /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z" }) }),
1241
+ "GitHub"
1242
+ ]
1243
+ }
1244
+ )
1245
+ ] })
1246
+ ] }),
1247
+ showLoginLink && /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1248
+ textAlign: "center",
1249
+ marginTop: "20px",
1250
+ paddingTop: "20px",
1251
+ borderTop: "1px solid #eee"
1252
+ }, children: /* @__PURE__ */ jsxRuntime.jsxs("p", { style: {
1253
+ color: "#666",
1254
+ fontSize: "14px"
1255
+ }, children: [
1256
+ "Already have an account?",
1257
+ " ",
1258
+ /* @__PURE__ */ jsxRuntime.jsx(
1259
+ "button",
1260
+ {
1261
+ type: "button",
1262
+ onClick: onLoginClick,
1263
+ disabled: isLoading,
1264
+ style: {
1265
+ background: "none",
1266
+ border: "none",
1267
+ color: "#007bff",
1268
+ textDecoration: "none",
1269
+ cursor: "pointer",
1270
+ fontSize: "14px",
1271
+ fontWeight: 600,
1272
+ padding: "0",
1273
+ transition: "color 0.2s ease"
1274
+ },
1275
+ children: "Login"
1276
+ }
1277
+ )
1278
+ ] }) })
1279
+ ] }) });
1280
+ };
1281
+ var OtpForm = ({
1282
+ email,
1283
+ onVerifySuccess,
1284
+ onBackToLogin
1285
+ }) => {
1286
+ const [otp, setOtp] = react.useState("");
1287
+ const [isLoading, setIsLoading] = react.useState(false);
1288
+ const [error, setError] = react.useState(null);
1289
+ const [resendCooldown, setResendCooldown] = react.useState(0);
1290
+ const [resendLoading, setResendLoading] = react.useState(false);
1291
+ const { verify, login } = useAuth({
1292
+ baseUrl: typeof window !== "undefined" ? window.location.origin : "http://localhost:7000"
1293
+ });
1294
+ const handleSubmit = async (e) => {
1295
+ e.preventDefault();
1296
+ setIsLoading(true);
1297
+ setError(null);
1298
+ if (!/^\d{6}$/.test(otp)) {
1299
+ setError("Please enter a valid 6-digit OTP");
1300
+ setIsLoading(false);
1301
+ return;
1302
+ }
1303
+ try {
1304
+ const response = await verify({ email, otp });
1305
+ if (response.success) {
1306
+ onVerifySuccess?.();
1307
+ } else {
1308
+ setError(response.message || "Verification failed");
1309
+ }
1310
+ } catch (err) {
1311
+ setError(err instanceof Error ? err.message : "An unknown error occurred");
1312
+ } finally {
1313
+ setIsLoading(false);
1314
+ }
1315
+ };
1316
+ const handleOtpChange = (e) => {
1317
+ const value = e.target.value;
1318
+ if (/^\d{0,6}$/.test(value)) {
1319
+ setOtp(value);
1320
+ }
1321
+ };
1322
+ const handleResendOtp = async () => {
1323
+ if (resendCooldown > 0 || resendLoading)
1324
+ return;
1325
+ setResendLoading(true);
1326
+ setError(null);
1327
+ try {
1328
+ const response = await login({ email });
1329
+ if (response.success) {
1330
+ setResendCooldown(60);
1331
+ const interval = setInterval(() => {
1332
+ setResendCooldown((prev) => {
1333
+ if (prev <= 1) {
1334
+ clearInterval(interval);
1335
+ return 0;
1336
+ }
1337
+ return prev - 1;
1338
+ });
1339
+ }, 1e3);
1340
+ } else {
1341
+ setError(response.message || "Failed to resend OTP");
1342
+ }
1343
+ } catch (err) {
1344
+ setError(err instanceof Error ? err.message : "Failed to resend OTP");
1345
+ } finally {
1346
+ setResendLoading(false);
1347
+ }
1348
+ };
1349
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1350
+ maxWidth: "400px",
1351
+ margin: "0 auto",
1352
+ padding: "30px",
1353
+ borderRadius: "12px",
1354
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
1355
+ backgroundColor: "#ffffff",
1356
+ border: "1px solid #eaeaea"
1357
+ }, children: /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: handleSubmit, style: {
1358
+ display: "flex",
1359
+ flexDirection: "column"
1360
+ }, children: [
1361
+ /* @__PURE__ */ jsxRuntime.jsx("h2", { style: {
1362
+ textAlign: "center",
1363
+ marginBottom: "24px",
1364
+ color: "#1f2937",
1365
+ fontSize: "24px",
1366
+ fontWeight: 600
1367
+ }, children: "Verify OTP" }),
1368
+ /* @__PURE__ */ jsxRuntime.jsxs("p", { style: {
1369
+ textAlign: "center",
1370
+ marginBottom: "24px",
1371
+ color: "#4b5563",
1372
+ fontSize: "14px"
1373
+ }, children: [
1374
+ "Enter the 6-digit code sent to ",
1375
+ /* @__PURE__ */ jsxRuntime.jsx("strong", { style: { color: "#111827" }, children: email })
1376
+ ] }),
1377
+ error && /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1378
+ padding: "12px 16px",
1379
+ marginBottom: "20px",
1380
+ backgroundColor: "#f8d7da",
1381
+ color: "#721c24",
1382
+ border: "1px solid #f5c6cb",
1383
+ borderRadius: "8px",
1384
+ fontSize: "14px",
1385
+ fontWeight: 500
1386
+ }, children: error }),
1387
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
1388
+ marginBottom: "20px"
1389
+ }, children: [
1390
+ /* @__PURE__ */ jsxRuntime.jsx("label", { htmlFor: "otp", style: {
1391
+ display: "block",
1392
+ marginBottom: "8px",
1393
+ fontWeight: 500,
1394
+ color: "#374151",
1395
+ fontSize: "14px"
1396
+ }, children: "OTP Code:" }),
1397
+ /* @__PURE__ */ jsxRuntime.jsx(
1398
+ "input",
1399
+ {
1400
+ id: "otp",
1401
+ type: "text",
1402
+ value: otp,
1403
+ onChange: handleOtpChange,
1404
+ required: true,
1405
+ disabled: isLoading,
1406
+ style: {
1407
+ width: "100%",
1408
+ padding: "12px 16px",
1409
+ border: "1px solid #ddd",
1410
+ borderRadius: "8px",
1411
+ fontSize: "20px",
1412
+ boxSizing: "border-box",
1413
+ color: "#111827",
1414
+ transition: "all 0.2s ease",
1415
+ backgroundColor: "#ffffff",
1416
+ textAlign: "center",
1417
+ letterSpacing: "5px"
1418
+ },
1419
+ maxLength: 6,
1420
+ placeholder: "123456",
1421
+ autoFocus: true
1422
+ }
1423
+ )
1424
+ ] }),
1425
+ /* @__PURE__ */ jsxRuntime.jsx(
1426
+ "button",
1427
+ {
1428
+ type: "submit",
1429
+ disabled: isLoading || otp.length !== 6,
1430
+ style: {
1431
+ padding: "14px",
1432
+ backgroundColor: "#007bff",
1433
+ color: "white",
1434
+ border: "none",
1435
+ borderRadius: "8px",
1436
+ fontSize: "16px",
1437
+ fontWeight: 600,
1438
+ cursor: "pointer",
1439
+ transition: "all 0.2s ease",
1440
+ marginTop: "8px"
1441
+ },
1442
+ children: isLoading ? "Verifying..." : "Verify OTP"
1443
+ }
1444
+ ),
1445
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
1446
+ textAlign: "center",
1447
+ marginTop: "20px",
1448
+ paddingTop: "20px",
1449
+ borderTop: "1px solid #eee"
1450
+ }, children: [
1451
+ /* @__PURE__ */ jsxRuntime.jsx(
1452
+ "button",
1453
+ {
1454
+ type: "button",
1455
+ onClick: handleResendOtp,
1456
+ disabled: isLoading || resendLoading || resendCooldown > 0,
1457
+ style: {
1458
+ background: "none",
1459
+ border: "none",
1460
+ color: resendCooldown > 0 ? "#999" : "#007bff",
1461
+ textDecoration: "none",
1462
+ cursor: resendCooldown > 0 ? "not-allowed" : "pointer",
1463
+ fontSize: "14px",
1464
+ fontWeight: 600,
1465
+ padding: "0",
1466
+ marginBottom: "12px",
1467
+ transition: "color 0.2s ease",
1468
+ display: "block",
1469
+ width: "100%"
1470
+ },
1471
+ children: resendLoading ? "Sending..." : resendCooldown > 0 ? `Resend OTP in ${resendCooldown}s` : "Resend OTP"
1472
+ }
1473
+ ),
1474
+ /* @__PURE__ */ jsxRuntime.jsx(
1475
+ "button",
1476
+ {
1477
+ type: "button",
1478
+ onClick: onBackToLogin,
1479
+ disabled: isLoading,
1480
+ style: {
1481
+ background: "none",
1482
+ border: "none",
1483
+ color: "#007bff",
1484
+ textDecoration: "none",
1485
+ cursor: "pointer",
1486
+ fontSize: "14px",
1487
+ fontWeight: 600,
1488
+ padding: "0",
1489
+ transition: "color 0.2s ease"
1490
+ },
1491
+ children: "Back to Login"
1492
+ }
1493
+ )
1494
+ ] })
1495
+ ] }) });
1496
+ };
1497
+ var AuthFlow = ({
1498
+ onAuthComplete,
1499
+ initialStep = "login",
1500
+ showTitle = true
1501
+ }) => {
1502
+ const [step, setStep] = react.useState(initialStep);
1503
+ const [email, setEmail] = react.useState("");
1504
+ const [message, setMessage] = react.useState(null);
1505
+ const handleLoginSuccess = (email2, needsOtpVerification) => {
1506
+ setEmail(email2);
1507
+ if (needsOtpVerification) {
1508
+ setStep("otp");
1509
+ setMessage(null);
1510
+ } else {
1511
+ setMessage(null);
1512
+ onAuthComplete?.();
1513
+ }
1514
+ };
1515
+ const handleRegisterSuccess = () => {
1516
+ setMessage("Registration successful! Please check your email for verification.");
1517
+ setTimeout(() => {
1518
+ setStep("login");
1519
+ setMessage(null);
1520
+ }, 3e3);
1521
+ };
1522
+ const handleVerifySuccess = () => {
1523
+ setMessage(null);
1524
+ onAuthComplete?.();
1525
+ };
1526
+ const handleBackToLogin = () => {
1527
+ setStep("login");
1528
+ setMessage(null);
1529
+ };
1530
+ const renderCurrentStep = () => {
1531
+ switch (step) {
1532
+ case "login":
1533
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1534
+ showTitle && /* @__PURE__ */ jsxRuntime.jsx("h1", { style: {
1535
+ textAlign: "center",
1536
+ marginBottom: "24px",
1537
+ color: "#333",
1538
+ fontSize: "32px",
1539
+ fontWeight: 700
1540
+ }, children: "Welcome Back" }),
1541
+ message && /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1542
+ padding: "12px 16px",
1543
+ marginBottom: "20px",
1544
+ backgroundColor: "#d4edda",
1545
+ color: "#155724",
1546
+ border: "1px solid #c3e6cb",
1547
+ borderRadius: "8px",
1548
+ fontSize: "14px",
1549
+ fontWeight: 500
1550
+ }, children: message }),
1551
+ /* @__PURE__ */ jsxRuntime.jsx(
1552
+ LoginForm,
1553
+ {
1554
+ onLoginSuccess: handleLoginSuccess,
1555
+ onRegisterClick: () => setStep("register"),
1556
+ showRegisterLink: true
1557
+ }
1558
+ )
1559
+ ] });
1560
+ case "register":
1561
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1562
+ showTitle && /* @__PURE__ */ jsxRuntime.jsx("h1", { style: {
1563
+ textAlign: "center",
1564
+ marginBottom: "24px",
1565
+ color: "#333",
1566
+ fontSize: "32px",
1567
+ fontWeight: 700
1568
+ }, children: "Create Account" }),
1569
+ message && /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1570
+ padding: "12px 16px",
1571
+ marginBottom: "20px",
1572
+ backgroundColor: "#d4edda",
1573
+ color: "#155724",
1574
+ border: "1px solid #c3e6cb",
1575
+ borderRadius: "8px",
1576
+ fontSize: "14px",
1577
+ fontWeight: 500
1578
+ }, children: message }),
1579
+ /* @__PURE__ */ jsxRuntime.jsx(
1580
+ RegisterForm,
1581
+ {
1582
+ onRegisterSuccess: handleRegisterSuccess,
1583
+ onLoginClick: () => setStep("login"),
1584
+ showLoginLink: true
1585
+ }
1586
+ )
1587
+ ] });
1588
+ case "otp":
1589
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1590
+ showTitle && /* @__PURE__ */ jsxRuntime.jsx("h1", { style: {
1591
+ textAlign: "center",
1592
+ marginBottom: "24px",
1593
+ color: "#333",
1594
+ fontSize: "32px",
1595
+ fontWeight: 700
1596
+ }, children: "Verify Your Email" }),
1597
+ /* @__PURE__ */ jsxRuntime.jsx(
1598
+ OtpForm,
1599
+ {
1600
+ email,
1601
+ onVerifySuccess: handleVerifySuccess,
1602
+ onBackToLogin: handleBackToLogin
1603
+ }
1604
+ )
1605
+ ] });
1606
+ default:
1607
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
1608
+ showTitle && /* @__PURE__ */ jsxRuntime.jsx("h1", { style: {
1609
+ textAlign: "center",
1610
+ marginBottom: "24px",
1611
+ color: "#333",
1612
+ fontSize: "32px",
1613
+ fontWeight: 700
1614
+ }, children: "Welcome Back" }),
1615
+ /* @__PURE__ */ jsxRuntime.jsx(
1616
+ LoginForm,
1617
+ {
1618
+ onLoginSuccess: handleLoginSuccess,
1619
+ onRegisterClick: () => setStep("register"),
1620
+ showRegisterLink: true
1621
+ }
1622
+ )
1623
+ ] });
1624
+ }
1625
+ };
1626
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1627
+ minHeight: "300px",
1628
+ display: "flex",
1629
+ alignItems: "center",
1630
+ justifyContent: "center"
1631
+ }, children: renderCurrentStep() });
1632
+ };
1633
+ var EmailVerificationPage = ({
1634
+ token,
1635
+ onVerificationSuccess,
1636
+ onVerificationError,
1637
+ baseUrl
1638
+ }) => {
1639
+ const [isLoading, setIsLoading] = react.useState(true);
1640
+ const [message, setMessage] = react.useState("");
1641
+ const [isSuccess, setIsSuccess] = react.useState(false);
1642
+ const { verifyEmailToken } = useAuth({
1643
+ baseUrl: baseUrl || (typeof window !== "undefined" ? window.location.origin : "http://localhost:7000")
1644
+ });
1645
+ react.useEffect(() => {
1646
+ const verifyEmail = async () => {
1647
+ if (!token) {
1648
+ setIsLoading(false);
1649
+ setMessage("Invalid verification token");
1650
+ setIsSuccess(false);
1651
+ onVerificationError?.("Invalid verification token");
1652
+ return;
1653
+ }
1654
+ try {
1655
+ const response = await verifyEmailToken(token);
1656
+ if (response.success) {
1657
+ setIsLoading(false);
1658
+ setMessage(response.message || "Email verified successfully!");
1659
+ setIsSuccess(true);
1660
+ onVerificationSuccess?.();
1661
+ } else {
1662
+ setIsLoading(false);
1663
+ setMessage(response.message || "Email verification failed");
1664
+ setIsSuccess(false);
1665
+ onVerificationError?.(response.message || "Email verification failed");
1666
+ }
1667
+ } catch (err) {
1668
+ setIsLoading(false);
1669
+ const errorMessage = err instanceof Error ? err.message : "An unknown error occurred";
1670
+ setMessage(errorMessage);
1671
+ setIsSuccess(false);
1672
+ onVerificationError?.(errorMessage);
1673
+ }
1674
+ };
1675
+ verifyEmail();
1676
+ }, [token, verifyEmailToken, onVerificationSuccess, onVerificationError]);
1677
+ if (isLoading) {
1678
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1679
+ maxWidth: "500px",
1680
+ margin: "0 auto",
1681
+ padding: "30px",
1682
+ borderRadius: "12px",
1683
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
1684
+ backgroundColor: "#ffffff",
1685
+ textAlign: "center",
1686
+ border: "1px solid #eaeaea"
1687
+ }, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
1688
+ padding: "20px"
1689
+ }, children: [
1690
+ /* @__PURE__ */ jsxRuntime.jsx("h2", { style: { color: "black" }, children: "Verifying your email..." }),
1691
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1692
+ border: "4px solid #f3f3f3",
1693
+ borderTop: "4px solid #007bff",
1694
+ borderRadius: "50%",
1695
+ width: "40px",
1696
+ height: "40px",
1697
+ animation: "spin 2s linear infinite",
1698
+ margin: "20px auto"
1699
+ } })
1700
+ ] }) });
1701
+ }
1702
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1703
+ maxWidth: "500px",
1704
+ margin: "0 auto",
1705
+ padding: "30px",
1706
+ borderRadius: "12px",
1707
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
1708
+ backgroundColor: "#ffffff",
1709
+ textAlign: "center",
1710
+ border: "1px solid #eaeaea"
1711
+ }, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
1712
+ padding: "20px"
1713
+ }, children: [
1714
+ /* @__PURE__ */ jsxRuntime.jsx("h2", { style: { color: "black" }, children: "Email Verification" }),
1715
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: {
1716
+ padding: "16px 20px",
1717
+ margin: "24px 0",
1718
+ borderRadius: "8px",
1719
+ fontSize: "15px",
1720
+ fontWeight: 500,
1721
+ backgroundColor: isSuccess ? "#d4edda" : "#f8d7da",
1722
+ color: isSuccess ? "#155724" : "#721c24",
1723
+ border: isSuccess ? "1px solid #c3e6cb" : "1px solid #f5c6cb"
1724
+ }, children: message }),
1725
+ isSuccess && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: {
1726
+ marginTop: "24px"
1727
+ }, children: [
1728
+ /* @__PURE__ */ jsxRuntime.jsx("p", { style: { color: "black" }, children: "Your email has been successfully verified!" }),
1729
+ /* @__PURE__ */ jsxRuntime.jsx(
1730
+ "button",
1731
+ {
1732
+ onClick: () => window.location.href = "/login",
1733
+ style: {
1734
+ padding: "12px 24px",
1735
+ backgroundColor: "#007bff",
1736
+ color: "white",
1737
+ border: "none",
1738
+ borderRadius: "8px",
1739
+ fontSize: "16px",
1740
+ fontWeight: 600,
1741
+ cursor: "pointer",
1742
+ transition: "all 0.2s ease"
1743
+ },
1744
+ children: "Go to Login"
1745
+ }
1746
+ )
1747
+ ] })
1748
+ ] }) });
1749
+ };
1750
+
1751
+ exports.AuthFlow = AuthFlow;
1752
+ exports.EmailVerificationPage = EmailVerificationPage;
1753
+ exports.LoginForm = LoginForm;
1754
+ exports.OtpForm = OtpForm;
1755
+ exports.RegisterForm = RegisterForm;
1756
+ //# sourceMappingURL=out.js.map
1757
+ //# sourceMappingURL=index.components.js.map