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