@thetechfossil/auth2 1.2.1 → 1.2.3

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.
@@ -1,11 +1,13 @@
1
+ "use client";
1
2
  import axios from 'axios';
2
- import { useState, useCallback, useEffect } from 'react';
3
+ import { createContext, useState, useCallback, useEffect, useContext, useRef } from 'react';
3
4
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
4
5
 
5
6
  // src/core/http-client.ts
6
7
  var HttpClient = class {
7
8
  constructor(baseUrl, defaultHeaders = {}) {
8
9
  this.csrfToken = null;
10
+ this.frontendBaseUrl = null;
9
11
  this.axiosInstance = axios.create({
10
12
  baseURL: baseUrl.replace(/\/$/, ""),
11
13
  headers: {
@@ -22,6 +24,9 @@ var HttpClient = class {
22
24
  if (this.csrfToken && ["post", "put", "delete", "patch"].includes(config.method?.toLowerCase() || "")) {
23
25
  config.headers["x-csrf-token"] = this.csrfToken;
24
26
  }
27
+ if (this.frontendBaseUrl) {
28
+ config.headers["X-Frontend-URL"] = this.frontendBaseUrl;
29
+ }
25
30
  return config;
26
31
  },
27
32
  (error) => Promise.reject(error)
@@ -77,6 +82,15 @@ var HttpClient = class {
77
82
  removeCsrfToken() {
78
83
  this.csrfToken = null;
79
84
  }
85
+ setFrontendBaseUrl(url) {
86
+ this.frontendBaseUrl = url;
87
+ }
88
+ getFrontendBaseUrl() {
89
+ return this.frontendBaseUrl;
90
+ }
91
+ removeFrontendBaseUrl() {
92
+ this.frontendBaseUrl = null;
93
+ }
80
94
  async refreshCsrfToken() {
81
95
  try {
82
96
  const response = await this.axiosInstance.get("/api/v1/auth/csrf-token");
@@ -99,6 +113,12 @@ var AuthService = class {
99
113
  };
100
114
  this.httpClient = new HttpClient(this.config.baseUrl);
101
115
  this.loadTokenFromStorage();
116
+ if (typeof window !== "undefined") {
117
+ const frontendBaseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || process.env.NEXT_PUBLIC_APP_URL || window.location.origin;
118
+ if (frontendBaseUrl) {
119
+ this.httpClient.setFrontendBaseUrl(frontendBaseUrl);
120
+ }
121
+ }
102
122
  if (this.config.csrfEnabled && typeof window !== "undefined") {
103
123
  this.refreshCsrfToken();
104
124
  }
@@ -215,11 +235,7 @@ var AuthService = class {
215
235
  throw new Error(response.message || "Login failed");
216
236
  }
217
237
  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);
238
+ const response = await this.httpClient.post("/api/v1/auth/register", data);
223
239
  if (response.success && response.message === "Registration data saved. Verification email sent. Please check your inbox.") {
224
240
  return response;
225
241
  }
@@ -235,15 +251,33 @@ var AuthService = class {
235
251
  return response;
236
252
  }
237
253
  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);
254
+ try {
255
+ const response = await this.httpClient.get(`/api/v1/auth/verify-email?token=${token}`);
256
+ if (response.success && response.token) {
257
+ this.token = response.token;
258
+ this.httpClient.setAuthToken(response.token);
259
+ this.saveTokenToStorage(response.token);
260
+ }
261
+ return response;
262
+ } catch (error) {
263
+ if (error.response?.data) {
264
+ return {
265
+ success: false,
266
+ message: error.response.data.message || "Email verification failed"
267
+ };
268
+ }
269
+ return {
270
+ success: false,
271
+ message: error.message || "Network error occurred"
272
+ };
243
273
  }
244
- return response;
245
274
  }
246
275
  async logout() {
276
+ try {
277
+ await this.httpClient.post("/api/v1/auth/logout", {});
278
+ } catch (error) {
279
+ console.warn("Failed to call logout endpoint:", error);
280
+ }
247
281
  this.token = null;
248
282
  this.httpClient.removeAuthToken();
249
283
  this.httpClient.removeCsrfToken();
@@ -280,9 +314,6 @@ var AuthService = class {
280
314
  return response.user;
281
315
  }
282
316
  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
317
  const response = await this.httpClient.post("/api/v1/auth/forgot-password", { email });
287
318
  return response;
288
319
  }
@@ -290,6 +321,142 @@ var AuthService = class {
290
321
  const response = await this.httpClient.post("/api/v1/auth/reset-password", { token, password });
291
322
  return response;
292
323
  }
324
+ async changePassword(oldPassword, newPassword) {
325
+ if (!this.token) {
326
+ throw new Error("Not authenticated");
327
+ }
328
+ const response = await this.httpClient.post("/api/v1/user/change-password", {
329
+ oldPassword,
330
+ newPassword
331
+ });
332
+ return response;
333
+ }
334
+ async updateAvatar(avatar) {
335
+ if (!this.token) {
336
+ throw new Error("Not authenticated");
337
+ }
338
+ const response = await this.httpClient.post("/api/v1/user/update/avatar", { avatar });
339
+ if (response.success && response.token) {
340
+ this.token = response.token;
341
+ this.httpClient.setAuthToken(response.token);
342
+ this.saveTokenToStorage(response.token);
343
+ }
344
+ return response;
345
+ }
346
+ async requestEmailChange(newEmail) {
347
+ if (!this.token) {
348
+ throw new Error("Not authenticated");
349
+ }
350
+ const response = await this.httpClient.post("/api/v1/user/request-email-change", {
351
+ newEmail
352
+ });
353
+ return response;
354
+ }
355
+ async verifyEmailChange(token) {
356
+ const response = await this.httpClient.get(`/api/v1/user/verify-email-change?token=${token}`);
357
+ if (response.success && response.token) {
358
+ this.token = response.token;
359
+ this.httpClient.setAuthToken(response.token);
360
+ this.saveTokenToStorage(response.token);
361
+ }
362
+ return response;
363
+ }
364
+ // 2FA / MFA Methods
365
+ async generate2FA() {
366
+ if (!this.token) {
367
+ throw new Error("Not authenticated");
368
+ }
369
+ const response = await this.httpClient.post(
370
+ "/api/v1/mfa/generate",
371
+ {}
372
+ );
373
+ return response;
374
+ }
375
+ async enable2FA(token) {
376
+ if (!this.token) {
377
+ throw new Error("Not authenticated");
378
+ }
379
+ const response = await this.httpClient.post("/api/v1/mfa/enable", { token });
380
+ return response;
381
+ }
382
+ async disable2FA(token) {
383
+ if (!this.token) {
384
+ throw new Error("Not authenticated");
385
+ }
386
+ const response = await this.httpClient.post("/api/v1/mfa/disable", { token });
387
+ return response;
388
+ }
389
+ async validate2FA(token) {
390
+ if (!this.token) {
391
+ throw new Error("Not authenticated");
392
+ }
393
+ const response = await this.httpClient.post("/api/v1/mfa/validate", { token });
394
+ return response;
395
+ }
396
+ // Session Management Methods
397
+ async getSessions() {
398
+ if (!this.token) {
399
+ throw new Error("Not authenticated");
400
+ }
401
+ const response = await this.httpClient.get("/api/v1/sessions");
402
+ return response;
403
+ }
404
+ async revokeSession(sessionId) {
405
+ if (!this.token) {
406
+ throw new Error("Not authenticated");
407
+ }
408
+ const response = await this.httpClient.delete(`/api/v1/sessions/${sessionId}`);
409
+ return response;
410
+ }
411
+ async revokeAllSessions() {
412
+ if (!this.token) {
413
+ throw new Error("Not authenticated");
414
+ }
415
+ const response = await this.httpClient.delete("/api/v1/sessions/revoke/all");
416
+ this.token = null;
417
+ this.httpClient.removeAuthToken();
418
+ this.removeTokenFromStorage();
419
+ return response;
420
+ }
421
+ // Admin Methods
422
+ async getAuditLogs(filters) {
423
+ if (!this.token) {
424
+ throw new Error("Not authenticated");
425
+ }
426
+ const response = await this.httpClient.get(
427
+ "/api/v1/admin/audit-logs",
428
+ filters
429
+ );
430
+ return response;
431
+ }
432
+ async adminVerifyUser(userId) {
433
+ if (!this.token) {
434
+ throw new Error("Not authenticated");
435
+ }
436
+ const response = await this.httpClient.post(`/api/v1/admin/verify-user/${userId}`, {});
437
+ return response;
438
+ }
439
+ async adminForcePasswordReset(userId) {
440
+ if (!this.token) {
441
+ throw new Error("Not authenticated");
442
+ }
443
+ const response = await this.httpClient.post(`/api/v1/admin/force-password-reset/${userId}`, {});
444
+ return response;
445
+ }
446
+ async adminSuspendUser(userId) {
447
+ if (!this.token) {
448
+ throw new Error("Not authenticated");
449
+ }
450
+ const response = await this.httpClient.post(`/api/v1/admin/suspend-user/${userId}`, {});
451
+ return response;
452
+ }
453
+ async adminActivateUser(userId) {
454
+ if (!this.token) {
455
+ throw new Error("Not authenticated");
456
+ }
457
+ const response = await this.httpClient.post(`/api/v1/admin/activate-user/${userId}`, {});
458
+ return response;
459
+ }
293
460
  };
294
461
  var useAuth = (config) => {
295
462
  const [authService] = useState(() => new AuthService(config));
@@ -313,11 +480,7 @@ var useAuth = (config) => {
313
480
  const register = useCallback(async (data) => {
314
481
  setLoading(true);
315
482
  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);
483
+ const response = await authService.register(data);
321
484
  return response;
322
485
  } finally {
323
486
  setLoading(false);
@@ -425,99 +588,371 @@ var useAuth = (config) => {
425
588
  getUserById
426
589
  };
427
590
  };
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);
591
+ var AuthContext = createContext(void 0);
592
+ var AuthProvider = ({ children, config }) => {
593
+ const authConfig = {
594
+ baseUrl: config?.baseUrl || (typeof window !== "undefined" ? process.env.NEXT_PUBLIC_AUTH_API_URL || process.env.REACT_APP_AUTH_API_URL || "http://localhost:7000" : "http://localhost:7000"),
595
+ localStorageKey: config?.localStorageKey || "auth_token",
596
+ csrfEnabled: config?.csrfEnabled !== void 0 ? config.csrfEnabled : true
597
+ };
598
+ const [authService] = useState(() => new AuthService(authConfig));
599
+ const [user, setUser] = useState(null);
600
+ const [isLoaded, setIsLoaded] = useState(false);
601
+ const [loading, setLoading] = useState(false);
602
+ const checkAuthStatus = useCallback(async () => {
603
+ const authenticated = authService.isAuthenticated();
604
+ if (authenticated) {
605
+ try {
606
+ const currentUser = authService.getCurrentUser();
607
+ setUser(currentUser);
608
+ } catch (error) {
609
+ console.error("Failed to get current user:", error);
610
+ setUser(null);
611
+ }
612
+ } else {
613
+ setUser(null);
614
+ }
615
+ setIsLoaded(true);
616
+ }, [authService]);
617
+ useEffect(() => {
618
+ checkAuthStatus();
619
+ }, [checkAuthStatus]);
620
+ const signIn = useCallback(async (data) => {
621
+ setLoading(true);
451
622
  try {
452
- let response;
453
- if (usePassword) {
454
- response = await login({ email, password });
455
- } else {
456
- response = await login({ email });
623
+ const response = await authService.login(data);
624
+ if (response.success && response.user) {
625
+ setUser(response.user);
457
626
  }
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");
627
+ return response;
628
+ } finally {
629
+ setLoading(false);
630
+ }
631
+ }, [authService]);
632
+ const signUp = useCallback(async (data) => {
633
+ setLoading(true);
634
+ try {
635
+ const response = await authService.register(data);
636
+ return response;
637
+ } finally {
638
+ setLoading(false);
639
+ }
640
+ }, [authService]);
641
+ const signOut = useCallback(async () => {
642
+ setLoading(true);
643
+ try {
644
+ await authService.logout();
645
+ setUser(null);
646
+ } finally {
647
+ setLoading(false);
648
+ }
649
+ }, [authService]);
650
+ const verify = useCallback(async (data) => {
651
+ setLoading(true);
652
+ try {
653
+ const response = await authService.verify(data);
654
+ if (response.success && response.user) {
655
+ setUser(response.user);
471
656
  }
472
- } catch (err) {
473
- setError(err instanceof Error ? err.message : "An unknown error occurred");
657
+ return response;
474
658
  } finally {
475
- setIsLoading(false);
659
+ setLoading(false);
476
660
  }
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,
661
+ }, [authService]);
662
+ const verifyEmailToken = useCallback(async (token) => {
663
+ setLoading(true);
664
+ try {
665
+ const response = await authService.verifyEmailToken(token);
666
+ if (response.success && response.user) {
667
+ setUser(response.user);
668
+ }
669
+ return response;
670
+ } finally {
671
+ setLoading(false);
672
+ }
673
+ }, [authService]);
674
+ const updateProfile = useCallback(async (data) => {
675
+ setLoading(true);
676
+ try {
677
+ const response = await authService.updateProfile(data);
678
+ if (response.success && response.user) {
679
+ setUser(response.user);
680
+ }
681
+ return response;
682
+ } finally {
683
+ setLoading(false);
684
+ }
685
+ }, [authService]);
686
+ const getProfile = useCallback(async () => {
687
+ setLoading(true);
688
+ try {
689
+ const userData = await authService.getProfile();
690
+ setUser(userData);
691
+ return userData;
692
+ } finally {
693
+ setLoading(false);
694
+ }
695
+ }, [authService]);
696
+ const signInWithOAuth = useCallback((provider) => {
697
+ authService.loginWithOAuth(provider);
698
+ }, [authService]);
699
+ const linkOAuthProvider = useCallback((provider) => {
700
+ authService.linkOAuthProvider(provider);
701
+ }, [authService]);
702
+ const unlinkOAuthProvider = useCallback(async (provider) => {
703
+ setLoading(true);
704
+ try {
705
+ return await authService.unlinkOAuthProvider(provider);
706
+ } finally {
707
+ setLoading(false);
708
+ }
709
+ }, [authService]);
710
+ const forgotPassword = useCallback(async (email) => {
711
+ setLoading(true);
712
+ try {
713
+ return await authService.forgotPassword(email);
714
+ } finally {
715
+ setLoading(false);
716
+ }
717
+ }, [authService]);
718
+ const resetPassword = useCallback(async (token, password) => {
719
+ setLoading(true);
720
+ try {
721
+ return await authService.resetPassword(token, password);
722
+ } finally {
723
+ setLoading(false);
724
+ }
725
+ }, [authService]);
726
+ const changePassword = useCallback(async (oldPassword, newPassword) => {
727
+ setLoading(true);
728
+ try {
729
+ return await authService.changePassword(oldPassword, newPassword);
730
+ } finally {
731
+ setLoading(false);
732
+ }
733
+ }, [authService]);
734
+ const updateAvatar = useCallback(async (avatar) => {
735
+ setLoading(true);
736
+ try {
737
+ const response = await authService.updateAvatar(avatar);
738
+ if (response.success && response.user) {
739
+ setUser(response.user);
740
+ }
741
+ return response;
742
+ } finally {
743
+ setLoading(false);
744
+ }
745
+ }, [authService]);
746
+ const requestEmailChange = useCallback(async (newEmail) => {
747
+ setLoading(true);
748
+ try {
749
+ return await authService.requestEmailChange(newEmail);
750
+ } finally {
751
+ setLoading(false);
752
+ }
753
+ }, [authService]);
754
+ const verifyEmailChange = useCallback(async (token) => {
755
+ setLoading(true);
756
+ try {
757
+ const response = await authService.verifyEmailChange(token);
758
+ if (response.success && response.user) {
759
+ setUser(response.user);
760
+ }
761
+ return response;
762
+ } finally {
763
+ setLoading(false);
764
+ }
765
+ }, [authService]);
766
+ const generate2FA = useCallback(async () => {
767
+ setLoading(true);
768
+ try {
769
+ return await authService.generate2FA();
770
+ } finally {
771
+ setLoading(false);
772
+ }
773
+ }, [authService]);
774
+ const enable2FA = useCallback(async (token) => {
775
+ setLoading(true);
776
+ try {
777
+ return await authService.enable2FA(token);
778
+ } finally {
779
+ setLoading(false);
780
+ }
781
+ }, [authService]);
782
+ const disable2FA = useCallback(async (token) => {
783
+ setLoading(true);
784
+ try {
785
+ return await authService.disable2FA(token);
786
+ } finally {
787
+ setLoading(false);
788
+ }
789
+ }, [authService]);
790
+ const validate2FA = useCallback(async (token) => {
791
+ setLoading(true);
792
+ try {
793
+ return await authService.validate2FA(token);
794
+ } finally {
795
+ setLoading(false);
796
+ }
797
+ }, [authService]);
798
+ const getSessions = useCallback(async () => {
799
+ setLoading(true);
800
+ try {
801
+ return await authService.getSessions();
802
+ } finally {
803
+ setLoading(false);
804
+ }
805
+ }, [authService]);
806
+ const revokeSession = useCallback(async (sessionId) => {
807
+ setLoading(true);
808
+ try {
809
+ return await authService.revokeSession(sessionId);
810
+ } finally {
811
+ setLoading(false);
812
+ }
813
+ }, [authService]);
814
+ const revokeAllSessions = useCallback(async () => {
815
+ setLoading(true);
816
+ try {
817
+ const response = await authService.revokeAllSessions();
818
+ setUser(null);
819
+ return response;
820
+ } finally {
821
+ setLoading(false);
822
+ }
823
+ }, [authService]);
824
+ const value = {
825
+ user,
826
+ isLoaded,
827
+ isSignedIn: !!user,
828
+ loading,
829
+ signIn,
830
+ signUp,
831
+ signOut,
832
+ verify,
833
+ verifyEmailToken,
834
+ updateProfile,
835
+ getProfile,
836
+ signInWithOAuth,
837
+ linkOAuthProvider,
838
+ unlinkOAuthProvider,
839
+ forgotPassword,
840
+ resetPassword,
841
+ changePassword,
842
+ updateAvatar,
843
+ requestEmailChange,
844
+ verifyEmailChange,
845
+ generate2FA,
846
+ enable2FA,
847
+ disable2FA,
848
+ validate2FA,
849
+ getSessions,
850
+ revokeSession,
851
+ revokeAllSessions,
852
+ authService
853
+ };
854
+ return /* @__PURE__ */ jsx(AuthContext.Provider, { value, children });
855
+ };
856
+ var useAuth2 = () => {
857
+ const context = useContext(AuthContext);
858
+ if (context === void 0) {
859
+ throw new Error("useAuth must be used within an AuthProvider");
860
+ }
861
+ return context;
862
+ };
863
+ var LoginForm = ({
864
+ onSuccess,
865
+ onLoginSuccess,
866
+ onRegisterClick,
867
+ showRegisterLink = true,
868
+ config,
869
+ oauthProviders = ["google", "github"],
870
+ showOAuthButtons = true
871
+ }) => {
872
+ const [email, setEmail] = useState("");
873
+ const [password, setPassword] = useState("");
874
+ const [usePassword, setUsePassword] = useState(false);
875
+ const [showPassword, setShowPassword] = useState(false);
876
+ const [isLoading, setIsLoading] = useState(false);
877
+ const [error, setError] = useState(null);
878
+ const [rememberMe, setRememberMe] = useState(false);
879
+ const { login } = useAuth({
880
+ baseUrl: config?.baseUrl || (typeof window !== "undefined" ? window.location.origin : "http://localhost:7000")
881
+ });
882
+ const handleSubmit = async (e) => {
883
+ e.preventDefault();
884
+ setIsLoading(true);
885
+ setError(null);
886
+ try {
887
+ let response;
888
+ if (usePassword) {
889
+ response = await login({ email, password });
890
+ } else {
891
+ response = await login({ email });
892
+ }
893
+ if (response.success) {
894
+ onSuccess?.(response);
895
+ if (onLoginSuccess) {
896
+ if (response.message === "OTP sent to your email.") {
897
+ onLoginSuccess(email, true);
898
+ } else if (response.token) {
899
+ onLoginSuccess(email, false);
900
+ } else {
901
+ onLoginSuccess(email, true);
902
+ }
903
+ }
904
+ } else {
905
+ setError(response.message || "Login failed");
906
+ }
907
+ } catch (err) {
908
+ setError(err instanceof Error ? err.message : "An unknown error occurred");
909
+ } finally {
910
+ setIsLoading(false);
911
+ }
912
+ };
913
+ const toggleAuthMethod = () => {
914
+ setUsePassword(!usePassword);
915
+ setError(null);
916
+ };
917
+ const togglePasswordVisibility = () => {
918
+ setShowPassword(!showPassword);
919
+ };
920
+ return /* @__PURE__ */ jsx("div", { style: {
921
+ maxWidth: "400px",
922
+ margin: "0 auto",
923
+ padding: "30px",
924
+ borderRadius: "12px",
925
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
926
+ backgroundColor: "#ffffff",
927
+ border: "1px solid #eaeaea"
928
+ }, children: /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, style: {
929
+ display: "flex",
930
+ flexDirection: "column"
931
+ }, children: [
932
+ /* @__PURE__ */ jsx("h2", { style: {
933
+ textAlign: "center",
934
+ marginBottom: "24px",
935
+ color: "#1f2937",
936
+ fontSize: "24px",
937
+ fontWeight: 600
938
+ }, children: usePassword ? "Login with Password" : "Login with OTP" }),
939
+ error && /* @__PURE__ */ jsx("div", { style: {
940
+ padding: "12px 16px",
941
+ marginBottom: "20px",
942
+ backgroundColor: "#f8d7da",
943
+ color: "#721c24",
944
+ border: "1px solid #f5c6cb",
945
+ borderRadius: "8px",
946
+ fontSize: "14px",
947
+ fontWeight: 500
948
+ }, children: error }),
949
+ /* @__PURE__ */ jsxs("div", { style: {
950
+ marginBottom: "20px"
951
+ }, children: [
952
+ /* @__PURE__ */ jsx("label", { htmlFor: "email", style: {
953
+ display: "block",
954
+ marginBottom: "8px",
955
+ fontWeight: 500,
521
956
  color: "#374151",
522
957
  fontSize: "14px"
523
958
  }, children: "Email:" }),
@@ -1739,17 +2174,1812 @@ var EmailVerificationPage = ({
1739
2174
  ] })
1740
2175
  ] }) });
1741
2176
  };
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);
2177
+ var SignIn = ({ redirectUrl, appearance }) => {
2178
+ const { signIn, isSignedIn, loading: authLoading } = useAuth2();
2179
+ const [email, setEmail] = useState("");
2180
+ const [password, setPassword] = useState("");
2181
+ const [otp, setOtp] = useState("");
2182
+ const [usePassword, setUsePassword] = useState(false);
2183
+ const [showPassword, setShowPassword] = useState(false);
2184
+ const [isLoading, setIsLoading] = useState(false);
2185
+ const [error, setError] = useState(null);
2186
+ const [needsOtp, setNeedsOtp] = useState(false);
2187
+ const [success, setSuccess] = useState(null);
2188
+ useEffect(() => {
2189
+ if (isSignedIn && redirectUrl) {
2190
+ const redirect = redirectUrl || process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_LOGIN || process.env.REACT_APP_AUTH_REDIRECT_AFTER_LOGIN || "/dashboard";
2191
+ const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
2192
+ window.location.href = `${baseUrl}${redirect}`;
1749
2193
  }
1750
- return service;
1751
- });
1752
- const [user, setUser] = useState(null);
2194
+ }, [isSignedIn, redirectUrl]);
2195
+ const handleSubmit = async (e) => {
2196
+ e.preventDefault();
2197
+ setIsLoading(true);
2198
+ setError(null);
2199
+ setSuccess(null);
2200
+ try {
2201
+ if (needsOtp) {
2202
+ const response = await signIn({ email, otp });
2203
+ if (response.success) {
2204
+ setSuccess("Login successful!");
2205
+ } else {
2206
+ setError(response.message || "OTP verification failed");
2207
+ }
2208
+ } else if (usePassword) {
2209
+ const response = await signIn({ email, password });
2210
+ if (response.success) {
2211
+ setSuccess("Login successful!");
2212
+ } else {
2213
+ setError(response.message || "Login failed");
2214
+ }
2215
+ } else {
2216
+ const response = await signIn({ email });
2217
+ if (response.success && response.message === "OTP sent to your email.") {
2218
+ setNeedsOtp(true);
2219
+ setSuccess("OTP sent to your email. Please check your inbox.");
2220
+ } else {
2221
+ setError(response.message || "Failed to send OTP");
2222
+ }
2223
+ }
2224
+ } catch (err) {
2225
+ setError(err instanceof Error ? err.message : "An error occurred");
2226
+ } finally {
2227
+ setIsLoading(false);
2228
+ }
2229
+ };
2230
+ const toggleAuthMethod = () => {
2231
+ setUsePassword(!usePassword);
2232
+ setNeedsOtp(false);
2233
+ setError(null);
2234
+ setSuccess(null);
2235
+ setOtp("");
2236
+ };
2237
+ if (authLoading) {
2238
+ return /* @__PURE__ */ jsx("div", { style: { textAlign: "center", padding: "40px" }, children: /* @__PURE__ */ jsx("div", { children: "Loading..." }) });
2239
+ }
2240
+ return /* @__PURE__ */ jsx("div", { style: {
2241
+ maxWidth: "400px",
2242
+ margin: "0 auto",
2243
+ padding: "30px",
2244
+ borderRadius: "12px",
2245
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
2246
+ backgroundColor: "#ffffff",
2247
+ border: "1px solid #eaeaea",
2248
+ ...appearance?.elements?.card
2249
+ }, children: /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, children: [
2250
+ /* @__PURE__ */ jsx("h2", { style: {
2251
+ textAlign: "center",
2252
+ marginBottom: "24px",
2253
+ color: "#1f2937",
2254
+ fontSize: "24px",
2255
+ fontWeight: 600,
2256
+ ...appearance?.elements?.headerTitle
2257
+ }, children: needsOtp ? "Enter OTP" : usePassword ? "Sign in with password" : "Sign in" }),
2258
+ error && /* @__PURE__ */ jsx("div", { style: {
2259
+ padding: "12px 16px",
2260
+ marginBottom: "20px",
2261
+ backgroundColor: "#fee",
2262
+ color: "#c33",
2263
+ border: "1px solid #fcc",
2264
+ borderRadius: "8px",
2265
+ fontSize: "14px"
2266
+ }, children: error }),
2267
+ success && /* @__PURE__ */ jsx("div", { style: {
2268
+ padding: "12px 16px",
2269
+ marginBottom: "20px",
2270
+ backgroundColor: "#efe",
2271
+ color: "#3c3",
2272
+ border: "1px solid #cfc",
2273
+ borderRadius: "8px",
2274
+ fontSize: "14px"
2275
+ }, children: success }),
2276
+ !needsOtp && /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px" }, children: [
2277
+ /* @__PURE__ */ jsx("label", { htmlFor: "email", style: {
2278
+ display: "block",
2279
+ marginBottom: "8px",
2280
+ fontWeight: 500,
2281
+ color: "#374151",
2282
+ fontSize: "14px"
2283
+ }, children: "Email" }),
2284
+ /* @__PURE__ */ jsx(
2285
+ "input",
2286
+ {
2287
+ id: "email",
2288
+ type: "email",
2289
+ value: email,
2290
+ onChange: (e) => setEmail(e.target.value),
2291
+ required: true,
2292
+ disabled: isLoading,
2293
+ style: {
2294
+ width: "100%",
2295
+ padding: "12px 16px",
2296
+ border: "1px solid #ddd",
2297
+ borderRadius: "8px",
2298
+ fontSize: "16px",
2299
+ boxSizing: "border-box",
2300
+ ...appearance?.elements?.formFieldInput
2301
+ },
2302
+ placeholder: "Enter your email"
2303
+ }
2304
+ )
2305
+ ] }),
2306
+ usePassword && !needsOtp && /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px", position: "relative" }, children: [
2307
+ /* @__PURE__ */ jsx("label", { htmlFor: "password", style: {
2308
+ display: "block",
2309
+ marginBottom: "8px",
2310
+ fontWeight: 500,
2311
+ color: "#374151",
2312
+ fontSize: "14px"
2313
+ }, children: "Password" }),
2314
+ /* @__PURE__ */ jsx(
2315
+ "input",
2316
+ {
2317
+ id: "password",
2318
+ type: showPassword ? "text" : "password",
2319
+ value: password,
2320
+ onChange: (e) => setPassword(e.target.value),
2321
+ required: true,
2322
+ disabled: isLoading,
2323
+ style: {
2324
+ width: "100%",
2325
+ padding: "12px 16px",
2326
+ border: "1px solid #ddd",
2327
+ borderRadius: "8px",
2328
+ fontSize: "16px",
2329
+ boxSizing: "border-box",
2330
+ ...appearance?.elements?.formFieldInput
2331
+ },
2332
+ placeholder: "Enter your password"
2333
+ }
2334
+ ),
2335
+ /* @__PURE__ */ jsx(
2336
+ "button",
2337
+ {
2338
+ type: "button",
2339
+ onClick: () => setShowPassword(!showPassword),
2340
+ style: {
2341
+ position: "absolute",
2342
+ right: "12px",
2343
+ top: "38px",
2344
+ background: "none",
2345
+ border: "none",
2346
+ cursor: "pointer",
2347
+ color: "#666",
2348
+ fontSize: "14px"
2349
+ },
2350
+ children: showPassword ? "Hide" : "Show"
2351
+ }
2352
+ )
2353
+ ] }),
2354
+ needsOtp && /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px" }, children: [
2355
+ /* @__PURE__ */ jsx("label", { htmlFor: "otp", style: {
2356
+ display: "block",
2357
+ marginBottom: "8px",
2358
+ fontWeight: 500,
2359
+ color: "#374151",
2360
+ fontSize: "14px"
2361
+ }, children: "One-Time Password" }),
2362
+ /* @__PURE__ */ jsx(
2363
+ "input",
2364
+ {
2365
+ id: "otp",
2366
+ type: "text",
2367
+ value: otp,
2368
+ onChange: (e) => setOtp(e.target.value),
2369
+ required: true,
2370
+ disabled: isLoading,
2371
+ maxLength: 6,
2372
+ style: {
2373
+ width: "100%",
2374
+ padding: "12px 16px",
2375
+ border: "1px solid #ddd",
2376
+ borderRadius: "8px",
2377
+ fontSize: "16px",
2378
+ boxSizing: "border-box",
2379
+ letterSpacing: "0.5em",
2380
+ textAlign: "center",
2381
+ ...appearance?.elements?.formFieldInput
2382
+ },
2383
+ placeholder: "000000"
2384
+ }
2385
+ )
2386
+ ] }),
2387
+ /* @__PURE__ */ jsx(
2388
+ "button",
2389
+ {
2390
+ type: "submit",
2391
+ disabled: isLoading,
2392
+ style: {
2393
+ width: "100%",
2394
+ padding: "14px",
2395
+ backgroundColor: "#007bff",
2396
+ color: "white",
2397
+ border: "none",
2398
+ borderRadius: "8px",
2399
+ fontSize: "16px",
2400
+ fontWeight: 600,
2401
+ cursor: "pointer",
2402
+ transition: "all 0.2s ease",
2403
+ ...appearance?.elements?.formButtonPrimary
2404
+ },
2405
+ children: isLoading ? "Please wait..." : needsOtp ? "Verify OTP" : usePassword ? "Sign in" : "Continue with email"
2406
+ }
2407
+ ),
2408
+ !needsOtp && /* @__PURE__ */ jsx("div", { style: { textAlign: "center", marginTop: "16px" }, children: /* @__PURE__ */ jsx(
2409
+ "button",
2410
+ {
2411
+ type: "button",
2412
+ onClick: toggleAuthMethod,
2413
+ disabled: isLoading,
2414
+ style: {
2415
+ background: "none",
2416
+ border: "none",
2417
+ color: "#007bff",
2418
+ cursor: "pointer",
2419
+ fontSize: "14px",
2420
+ fontWeight: 600
2421
+ },
2422
+ children: usePassword ? "Use email code instead" : "Use password instead"
2423
+ }
2424
+ ) }),
2425
+ needsOtp && /* @__PURE__ */ jsx("div", { style: { textAlign: "center", marginTop: "16px" }, children: /* @__PURE__ */ jsx(
2426
+ "button",
2427
+ {
2428
+ type: "button",
2429
+ onClick: () => {
2430
+ setNeedsOtp(false);
2431
+ setOtp("");
2432
+ setError(null);
2433
+ setSuccess(null);
2434
+ },
2435
+ disabled: isLoading,
2436
+ style: {
2437
+ background: "none",
2438
+ border: "none",
2439
+ color: "#007bff",
2440
+ cursor: "pointer",
2441
+ fontSize: "14px",
2442
+ fontWeight: 600
2443
+ },
2444
+ children: "Back to sign in"
2445
+ }
2446
+ ) })
2447
+ ] }) });
2448
+ };
2449
+ var SignUp = ({ redirectUrl, appearance }) => {
2450
+ const { signUp, isSignedIn } = useAuth2();
2451
+ const [name, setName] = useState("");
2452
+ const [email, setEmail] = useState("");
2453
+ const [password, setPassword] = useState("");
2454
+ const [confirmPassword, setConfirmPassword] = useState("");
2455
+ const [showPassword, setShowPassword] = useState(false);
2456
+ const [isLoading, setIsLoading] = useState(false);
2457
+ const [error, setError] = useState(null);
2458
+ const [success, setSuccess] = useState(null);
2459
+ useEffect(() => {
2460
+ if (isSignedIn && redirectUrl) {
2461
+ const redirect = redirectUrl || process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_REGISTER || process.env.REACT_APP_AUTH_REDIRECT_AFTER_REGISTER || "/dashboard";
2462
+ const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
2463
+ window.location.href = `${baseUrl}${redirect}`;
2464
+ }
2465
+ }, [isSignedIn, redirectUrl]);
2466
+ const getPasswordStrength = (pwd) => {
2467
+ if (!pwd)
2468
+ return { strength: "weak", color: "#ddd" };
2469
+ let score = 0;
2470
+ if (pwd.length >= 6)
2471
+ score++;
2472
+ if (pwd.length >= 8)
2473
+ score++;
2474
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
2475
+ score++;
2476
+ if (/\d/.test(pwd))
2477
+ score++;
2478
+ if (/[^a-zA-Z\d]/.test(pwd))
2479
+ score++;
2480
+ if (score <= 2)
2481
+ return { strength: "weak", color: "#f44" };
2482
+ if (score <= 3)
2483
+ return { strength: "medium", color: "#fa4" };
2484
+ return { strength: "strong", color: "#4f4" };
2485
+ };
2486
+ const passwordStrength = getPasswordStrength(password);
2487
+ const handleSubmit = async (e) => {
2488
+ e.preventDefault();
2489
+ setIsLoading(true);
2490
+ setError(null);
2491
+ setSuccess(null);
2492
+ if (password !== confirmPassword) {
2493
+ setError("Passwords do not match");
2494
+ setIsLoading(false);
2495
+ return;
2496
+ }
2497
+ if (password.length < 6) {
2498
+ setError("Password must be at least 6 characters");
2499
+ setIsLoading(false);
2500
+ return;
2501
+ }
2502
+ try {
2503
+ const response = await signUp({ name, email, password });
2504
+ if (response.success) {
2505
+ setSuccess("Registration successful! Please check your email to verify your account.");
2506
+ } else {
2507
+ setError(response.message || "Registration failed");
2508
+ }
2509
+ } catch (err) {
2510
+ setError(err instanceof Error ? err.message : "An error occurred");
2511
+ } finally {
2512
+ setIsLoading(false);
2513
+ }
2514
+ };
2515
+ return /* @__PURE__ */ jsx("div", { style: {
2516
+ maxWidth: "400px",
2517
+ margin: "0 auto",
2518
+ padding: "30px",
2519
+ borderRadius: "12px",
2520
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
2521
+ backgroundColor: "#ffffff",
2522
+ border: "1px solid #eaeaea",
2523
+ ...appearance?.elements?.card
2524
+ }, children: /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, children: [
2525
+ /* @__PURE__ */ jsx("h2", { style: {
2526
+ textAlign: "center",
2527
+ marginBottom: "24px",
2528
+ color: "#1f2937",
2529
+ fontSize: "24px",
2530
+ fontWeight: 600,
2531
+ ...appearance?.elements?.headerTitle
2532
+ }, children: "Create your account" }),
2533
+ error && /* @__PURE__ */ jsx("div", { style: {
2534
+ padding: "12px 16px",
2535
+ marginBottom: "20px",
2536
+ backgroundColor: "#fee",
2537
+ color: "#c33",
2538
+ border: "1px solid #fcc",
2539
+ borderRadius: "8px",
2540
+ fontSize: "14px"
2541
+ }, children: error }),
2542
+ success && /* @__PURE__ */ jsx("div", { style: {
2543
+ padding: "12px 16px",
2544
+ marginBottom: "20px",
2545
+ backgroundColor: "#efe",
2546
+ color: "#3c3",
2547
+ border: "1px solid #cfc",
2548
+ borderRadius: "8px",
2549
+ fontSize: "14px"
2550
+ }, children: success }),
2551
+ /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px" }, children: [
2552
+ /* @__PURE__ */ jsx("label", { htmlFor: "name", style: {
2553
+ display: "block",
2554
+ marginBottom: "8px",
2555
+ fontWeight: 500,
2556
+ color: "#374151",
2557
+ fontSize: "14px"
2558
+ }, children: "Full name" }),
2559
+ /* @__PURE__ */ jsx(
2560
+ "input",
2561
+ {
2562
+ id: "name",
2563
+ type: "text",
2564
+ value: name,
2565
+ onChange: (e) => setName(e.target.value),
2566
+ required: true,
2567
+ disabled: isLoading,
2568
+ style: {
2569
+ width: "100%",
2570
+ padding: "12px 16px",
2571
+ border: "1px solid #ddd",
2572
+ borderRadius: "8px",
2573
+ fontSize: "16px",
2574
+ boxSizing: "border-box",
2575
+ ...appearance?.elements?.formFieldInput
2576
+ },
2577
+ placeholder: "Enter your full name"
2578
+ }
2579
+ )
2580
+ ] }),
2581
+ /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px" }, children: [
2582
+ /* @__PURE__ */ jsx("label", { htmlFor: "email", style: {
2583
+ display: "block",
2584
+ marginBottom: "8px",
2585
+ fontWeight: 500,
2586
+ color: "#374151",
2587
+ fontSize: "14px"
2588
+ }, children: "Email" }),
2589
+ /* @__PURE__ */ jsx(
2590
+ "input",
2591
+ {
2592
+ id: "email",
2593
+ type: "email",
2594
+ value: email,
2595
+ onChange: (e) => setEmail(e.target.value),
2596
+ required: true,
2597
+ disabled: isLoading,
2598
+ style: {
2599
+ width: "100%",
2600
+ padding: "12px 16px",
2601
+ border: "1px solid #ddd",
2602
+ borderRadius: "8px",
2603
+ fontSize: "16px",
2604
+ boxSizing: "border-box",
2605
+ ...appearance?.elements?.formFieldInput
2606
+ },
2607
+ placeholder: "Enter your email"
2608
+ }
2609
+ )
2610
+ ] }),
2611
+ /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px", position: "relative" }, children: [
2612
+ /* @__PURE__ */ jsx("label", { htmlFor: "password", style: {
2613
+ display: "block",
2614
+ marginBottom: "8px",
2615
+ fontWeight: 500,
2616
+ color: "#374151",
2617
+ fontSize: "14px"
2618
+ }, children: "Password" }),
2619
+ /* @__PURE__ */ jsx(
2620
+ "input",
2621
+ {
2622
+ id: "password",
2623
+ type: showPassword ? "text" : "password",
2624
+ value: password,
2625
+ onChange: (e) => setPassword(e.target.value),
2626
+ required: true,
2627
+ disabled: isLoading,
2628
+ minLength: 6,
2629
+ style: {
2630
+ width: "100%",
2631
+ padding: "12px 16px",
2632
+ border: "1px solid #ddd",
2633
+ borderRadius: "8px",
2634
+ fontSize: "16px",
2635
+ boxSizing: "border-box",
2636
+ ...appearance?.elements?.formFieldInput
2637
+ },
2638
+ placeholder: "Create a password"
2639
+ }
2640
+ ),
2641
+ /* @__PURE__ */ jsx(
2642
+ "button",
2643
+ {
2644
+ type: "button",
2645
+ onClick: () => setShowPassword(!showPassword),
2646
+ style: {
2647
+ position: "absolute",
2648
+ right: "12px",
2649
+ top: "38px",
2650
+ background: "none",
2651
+ border: "none",
2652
+ cursor: "pointer",
2653
+ color: "#666",
2654
+ fontSize: "14px"
2655
+ },
2656
+ children: showPassword ? "Hide" : "Show"
2657
+ }
2658
+ ),
2659
+ password && /* @__PURE__ */ jsxs("div", { style: { marginTop: "8px" }, children: [
2660
+ /* @__PURE__ */ jsx("div", { style: {
2661
+ height: "4px",
2662
+ backgroundColor: "#eee",
2663
+ borderRadius: "2px",
2664
+ overflow: "hidden"
2665
+ }, children: /* @__PURE__ */ jsx("div", { style: {
2666
+ height: "100%",
2667
+ width: passwordStrength.strength === "weak" ? "33%" : passwordStrength.strength === "medium" ? "66%" : "100%",
2668
+ backgroundColor: passwordStrength.color,
2669
+ transition: "all 0.3s ease"
2670
+ } }) }),
2671
+ /* @__PURE__ */ jsxs("p", { style: {
2672
+ fontSize: "12px",
2673
+ color: passwordStrength.color,
2674
+ marginTop: "4px",
2675
+ textTransform: "capitalize"
2676
+ }, children: [
2677
+ passwordStrength.strength,
2678
+ " password"
2679
+ ] })
2680
+ ] })
2681
+ ] }),
2682
+ /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px" }, children: [
2683
+ /* @__PURE__ */ jsx("label", { htmlFor: "confirmPassword", style: {
2684
+ display: "block",
2685
+ marginBottom: "8px",
2686
+ fontWeight: 500,
2687
+ color: "#374151",
2688
+ fontSize: "14px"
2689
+ }, children: "Confirm password" }),
2690
+ /* @__PURE__ */ jsx(
2691
+ "input",
2692
+ {
2693
+ id: "confirmPassword",
2694
+ type: showPassword ? "text" : "password",
2695
+ value: confirmPassword,
2696
+ onChange: (e) => setConfirmPassword(e.target.value),
2697
+ required: true,
2698
+ disabled: isLoading,
2699
+ style: {
2700
+ width: "100%",
2701
+ padding: "12px 16px",
2702
+ border: "1px solid #ddd",
2703
+ borderRadius: "8px",
2704
+ fontSize: "16px",
2705
+ boxSizing: "border-box",
2706
+ ...appearance?.elements?.formFieldInput
2707
+ },
2708
+ placeholder: "Confirm your password"
2709
+ }
2710
+ )
2711
+ ] }),
2712
+ /* @__PURE__ */ jsx(
2713
+ "button",
2714
+ {
2715
+ type: "submit",
2716
+ disabled: isLoading,
2717
+ style: {
2718
+ width: "100%",
2719
+ padding: "14px",
2720
+ backgroundColor: "#007bff",
2721
+ color: "white",
2722
+ border: "none",
2723
+ borderRadius: "8px",
2724
+ fontSize: "16px",
2725
+ fontWeight: 600,
2726
+ cursor: "pointer",
2727
+ transition: "all 0.2s ease",
2728
+ ...appearance?.elements?.formButtonPrimary
2729
+ },
2730
+ children: isLoading ? "Creating account..." : "Sign up"
2731
+ }
2732
+ )
2733
+ ] }) });
2734
+ };
2735
+ var SignOut = ({ redirectUrl }) => {
2736
+ const { signOut } = useAuth2();
2737
+ useEffect(() => {
2738
+ const performSignOut = async () => {
2739
+ await signOut();
2740
+ const redirect = redirectUrl || process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_LOGOUT || process.env.REACT_APP_AUTH_REDIRECT_AFTER_LOGOUT || "/";
2741
+ const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
2742
+ window.location.href = `${baseUrl}${redirect}`;
2743
+ };
2744
+ performSignOut();
2745
+ }, [signOut, redirectUrl]);
2746
+ return /* @__PURE__ */ jsx("div", { style: { textAlign: "center", padding: "40px" }, children: /* @__PURE__ */ jsx("div", { children: "Signing out..." }) });
2747
+ };
2748
+ var UserButton = ({ showName = false, appearance }) => {
2749
+ const { user, signOut } = useAuth2();
2750
+ const [isOpen, setIsOpen] = useState(false);
2751
+ const dropdownRef = useRef(null);
2752
+ useEffect(() => {
2753
+ const handleClickOutside = (event) => {
2754
+ if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
2755
+ setIsOpen(false);
2756
+ }
2757
+ };
2758
+ document.addEventListener("mousedown", handleClickOutside);
2759
+ return () => document.removeEventListener("mousedown", handleClickOutside);
2760
+ }, []);
2761
+ if (!user)
2762
+ return null;
2763
+ const getInitials = (name) => {
2764
+ return name.split(" ").map((n) => n[0]).join("").toUpperCase().slice(0, 2);
2765
+ };
2766
+ const handleSignOut = async () => {
2767
+ await signOut();
2768
+ const redirect = process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_LOGOUT || process.env.REACT_APP_AUTH_REDIRECT_AFTER_LOGOUT || "/";
2769
+ const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
2770
+ window.location.href = `${baseUrl}${redirect}`;
2771
+ };
2772
+ return /* @__PURE__ */ jsxs("div", { style: { position: "relative", ...appearance?.elements?.userButtonBox }, ref: dropdownRef, children: [
2773
+ /* @__PURE__ */ jsxs(
2774
+ "button",
2775
+ {
2776
+ onClick: () => setIsOpen(!isOpen),
2777
+ style: {
2778
+ display: "flex",
2779
+ alignItems: "center",
2780
+ gap: "8px",
2781
+ padding: "6px",
2782
+ backgroundColor: "transparent",
2783
+ border: "none",
2784
+ borderRadius: "8px",
2785
+ cursor: "pointer",
2786
+ transition: "background-color 0.2s",
2787
+ ...appearance?.elements?.userButtonTrigger
2788
+ },
2789
+ onMouseEnter: (e) => {
2790
+ e.currentTarget.style.backgroundColor = "#f5f5f5";
2791
+ },
2792
+ onMouseLeave: (e) => {
2793
+ e.currentTarget.style.backgroundColor = "transparent";
2794
+ },
2795
+ children: [
2796
+ /* @__PURE__ */ jsx("div", { style: {
2797
+ width: "36px",
2798
+ height: "36px",
2799
+ borderRadius: "50%",
2800
+ backgroundColor: "#007bff",
2801
+ color: "white",
2802
+ display: "flex",
2803
+ alignItems: "center",
2804
+ justifyContent: "center",
2805
+ fontSize: "14px",
2806
+ fontWeight: 600
2807
+ }, children: getInitials(user.name) }),
2808
+ showName && /* @__PURE__ */ jsx("span", { style: { fontSize: "14px", fontWeight: 500, color: "#333" }, children: user.name })
2809
+ ]
2810
+ }
2811
+ ),
2812
+ isOpen && /* @__PURE__ */ jsxs("div", { style: {
2813
+ position: "absolute",
2814
+ top: "100%",
2815
+ right: 0,
2816
+ marginTop: "8px",
2817
+ width: "240px",
2818
+ backgroundColor: "white",
2819
+ borderRadius: "12px",
2820
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.15)",
2821
+ border: "1px solid #eaeaea",
2822
+ zIndex: 1e3,
2823
+ ...appearance?.elements?.userButtonPopoverCard
2824
+ }, children: [
2825
+ /* @__PURE__ */ jsx("div", { style: {
2826
+ padding: "16px",
2827
+ borderBottom: "1px solid #eee"
2828
+ }, children: /* @__PURE__ */ jsxs("div", { style: {
2829
+ display: "flex",
2830
+ alignItems: "center",
2831
+ gap: "12px"
2832
+ }, children: [
2833
+ /* @__PURE__ */ jsx("div", { style: {
2834
+ width: "48px",
2835
+ height: "48px",
2836
+ borderRadius: "50%",
2837
+ backgroundColor: "#007bff",
2838
+ color: "white",
2839
+ display: "flex",
2840
+ alignItems: "center",
2841
+ justifyContent: "center",
2842
+ fontSize: "18px",
2843
+ fontWeight: 600
2844
+ }, children: getInitials(user.name) }),
2845
+ /* @__PURE__ */ jsxs("div", { style: { flex: 1, minWidth: 0 }, children: [
2846
+ /* @__PURE__ */ jsx("div", { style: {
2847
+ fontSize: "14px",
2848
+ fontWeight: 600,
2849
+ color: "#333",
2850
+ overflow: "hidden",
2851
+ textOverflow: "ellipsis",
2852
+ whiteSpace: "nowrap"
2853
+ }, children: user.name }),
2854
+ /* @__PURE__ */ jsx("div", { style: {
2855
+ fontSize: "12px",
2856
+ color: "#666",
2857
+ overflow: "hidden",
2858
+ textOverflow: "ellipsis",
2859
+ whiteSpace: "nowrap"
2860
+ }, children: user.email })
2861
+ ] })
2862
+ ] }) }),
2863
+ /* @__PURE__ */ jsx("div", { style: { padding: "8px" }, children: /* @__PURE__ */ jsx(
2864
+ "button",
2865
+ {
2866
+ onClick: handleSignOut,
2867
+ style: {
2868
+ width: "100%",
2869
+ padding: "10px 16px",
2870
+ backgroundColor: "transparent",
2871
+ border: "none",
2872
+ borderRadius: "8px",
2873
+ textAlign: "left",
2874
+ cursor: "pointer",
2875
+ fontSize: "14px",
2876
+ color: "#333",
2877
+ fontWeight: 500,
2878
+ transition: "background-color 0.2s"
2879
+ },
2880
+ onMouseEnter: (e) => {
2881
+ e.currentTarget.style.backgroundColor = "#f5f5f5";
2882
+ },
2883
+ onMouseLeave: (e) => {
2884
+ e.currentTarget.style.backgroundColor = "transparent";
2885
+ },
2886
+ children: "Sign out"
2887
+ }
2888
+ ) })
2889
+ ] })
2890
+ ] });
2891
+ };
2892
+ var ProtectedRoute = ({
2893
+ children,
2894
+ fallback,
2895
+ redirectTo
2896
+ }) => {
2897
+ const { isSignedIn, isLoaded } = useAuth2();
2898
+ useEffect(() => {
2899
+ if (isLoaded && !isSignedIn) {
2900
+ const loginPath = redirectTo || process.env.NEXT_PUBLIC_AUTH_REDIRECT_TO_LOGIN || process.env.REACT_APP_AUTH_REDIRECT_TO_LOGIN || "/auth/login";
2901
+ const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
2902
+ window.location.href = `${baseUrl}${loginPath}`;
2903
+ }
2904
+ }, [isSignedIn, isLoaded, redirectTo]);
2905
+ if (!isLoaded) {
2906
+ return fallback || /* @__PURE__ */ jsx("div", { style: {
2907
+ display: "flex",
2908
+ justifyContent: "center",
2909
+ alignItems: "center",
2910
+ minHeight: "100vh"
2911
+ }, children: /* @__PURE__ */ jsx("div", { children: "Loading..." }) });
2912
+ }
2913
+ if (!isSignedIn) {
2914
+ return fallback || null;
2915
+ }
2916
+ return /* @__PURE__ */ jsx(Fragment, { children });
2917
+ };
2918
+ var PublicRoute = ({
2919
+ children,
2920
+ redirectTo
2921
+ }) => {
2922
+ const { isSignedIn, isLoaded } = useAuth2();
2923
+ useEffect(() => {
2924
+ if (isLoaded && isSignedIn) {
2925
+ const dashboardPath = redirectTo || process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_LOGIN || process.env.REACT_APP_AUTH_REDIRECT_AFTER_LOGIN || "/dashboard";
2926
+ const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
2927
+ window.location.href = `${baseUrl}${dashboardPath}`;
2928
+ }
2929
+ }, [isSignedIn, isLoaded, redirectTo]);
2930
+ if (!isLoaded) {
2931
+ return /* @__PURE__ */ jsx("div", { style: {
2932
+ display: "flex",
2933
+ justifyContent: "center",
2934
+ alignItems: "center",
2935
+ minHeight: "100vh"
2936
+ }, children: /* @__PURE__ */ jsx("div", { children: "Loading..." }) });
2937
+ }
2938
+ if (isSignedIn) {
2939
+ return null;
2940
+ }
2941
+ return /* @__PURE__ */ jsx(Fragment, { children });
2942
+ };
2943
+ var VerifyEmail = ({ token, onSuccess, onError }) => {
2944
+ const { verifyEmailToken } = useAuth2();
2945
+ const [status, setStatus] = useState("loading");
2946
+ const [message, setMessage] = useState("");
2947
+ useEffect(() => {
2948
+ const verify = async () => {
2949
+ const verifyToken = token || (typeof window !== "undefined" ? new URLSearchParams(window.location.search).get("token") : null);
2950
+ if (!verifyToken) {
2951
+ setStatus("error");
2952
+ setMessage("No verification token provided");
2953
+ onError?.("No verification token provided");
2954
+ return;
2955
+ }
2956
+ try {
2957
+ const response = await verifyEmailToken(verifyToken);
2958
+ if (response.success) {
2959
+ setStatus("success");
2960
+ setMessage("Email verified successfully! Redirecting...");
2961
+ onSuccess?.();
2962
+ setTimeout(() => {
2963
+ const redirect = process.env.NEXT_PUBLIC_AUTH_REDIRECT_AFTER_VERIFY || process.env.REACT_APP_AUTH_REDIRECT_AFTER_VERIFY || "/dashboard";
2964
+ const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
2965
+ window.location.href = `${baseUrl}${redirect}`;
2966
+ }, 2e3);
2967
+ } else {
2968
+ setStatus("error");
2969
+ setMessage(response.message || "Verification failed");
2970
+ onError?.(response.message || "Verification failed");
2971
+ }
2972
+ } catch (err) {
2973
+ setStatus("error");
2974
+ const errorMsg = err instanceof Error ? err.message : "An error occurred";
2975
+ setMessage(errorMsg);
2976
+ onError?.(errorMsg);
2977
+ }
2978
+ };
2979
+ verify();
2980
+ }, [token, verifyEmailToken, onSuccess, onError]);
2981
+ return /* @__PURE__ */ jsxs("div", { style: {
2982
+ maxWidth: "400px",
2983
+ margin: "40px auto",
2984
+ padding: "30px",
2985
+ borderRadius: "12px",
2986
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
2987
+ backgroundColor: "#ffffff",
2988
+ border: "1px solid #eaeaea",
2989
+ textAlign: "center"
2990
+ }, children: [
2991
+ status === "loading" && /* @__PURE__ */ jsxs(Fragment, { children: [
2992
+ /* @__PURE__ */ jsx("div", { style: {
2993
+ width: "48px",
2994
+ height: "48px",
2995
+ margin: "0 auto 20px",
2996
+ border: "4px solid #f3f3f3",
2997
+ borderTop: "4px solid #007bff",
2998
+ borderRadius: "50%",
2999
+ animation: "spin 1s linear infinite"
3000
+ } }),
3001
+ /* @__PURE__ */ jsx("style", { children: `
3002
+ @keyframes spin {
3003
+ 0% { transform: rotate(0deg); }
3004
+ 100% { transform: rotate(360deg); }
3005
+ }
3006
+ ` }),
3007
+ /* @__PURE__ */ jsx("h2", { style: {
3008
+ fontSize: "20px",
3009
+ fontWeight: 600,
3010
+ color: "#333",
3011
+ marginBottom: "12px"
3012
+ }, children: "Verifying your email..." }),
3013
+ /* @__PURE__ */ jsx("p", { style: { fontSize: "14px", color: "#666" }, children: "Please wait while we verify your email address." })
3014
+ ] }),
3015
+ status === "success" && /* @__PURE__ */ jsxs(Fragment, { children: [
3016
+ /* @__PURE__ */ jsx("div", { style: {
3017
+ width: "64px",
3018
+ height: "64px",
3019
+ margin: "0 auto 20px",
3020
+ backgroundColor: "#4caf50",
3021
+ borderRadius: "50%",
3022
+ display: "flex",
3023
+ alignItems: "center",
3024
+ justifyContent: "center"
3025
+ }, children: /* @__PURE__ */ jsx("svg", { width: "32", height: "32", viewBox: "0 0 24 24", fill: "none", stroke: "white", strokeWidth: "3", children: /* @__PURE__ */ jsx("polyline", { points: "20 6 9 17 4 12" }) }) }),
3026
+ /* @__PURE__ */ jsx("h2", { style: {
3027
+ fontSize: "20px",
3028
+ fontWeight: 600,
3029
+ color: "#333",
3030
+ marginBottom: "12px"
3031
+ }, children: "Email Verified!" }),
3032
+ /* @__PURE__ */ jsx("p", { style: { fontSize: "14px", color: "#666" }, children: message })
3033
+ ] }),
3034
+ status === "error" && /* @__PURE__ */ jsxs(Fragment, { children: [
3035
+ /* @__PURE__ */ jsx("div", { style: {
3036
+ width: "64px",
3037
+ height: "64px",
3038
+ margin: "0 auto 20px",
3039
+ backgroundColor: "#f44336",
3040
+ borderRadius: "50%",
3041
+ display: "flex",
3042
+ alignItems: "center",
3043
+ justifyContent: "center"
3044
+ }, children: /* @__PURE__ */ jsxs("svg", { width: "32", height: "32", viewBox: "0 0 24 24", fill: "none", stroke: "white", strokeWidth: "3", children: [
3045
+ /* @__PURE__ */ jsx("line", { x1: "18", y1: "6", x2: "6", y2: "18" }),
3046
+ /* @__PURE__ */ jsx("line", { x1: "6", y1: "6", x2: "18", y2: "18" })
3047
+ ] }) }),
3048
+ /* @__PURE__ */ jsx("h2", { style: {
3049
+ fontSize: "20px",
3050
+ fontWeight: 600,
3051
+ color: "#333",
3052
+ marginBottom: "12px"
3053
+ }, children: "Verification Failed" }),
3054
+ /* @__PURE__ */ jsx("p", { style: { fontSize: "14px", color: "#666", marginBottom: "20px" }, children: message }),
3055
+ /* @__PURE__ */ jsx(
3056
+ "button",
3057
+ {
3058
+ onClick: () => {
3059
+ const loginPath = process.env.NEXT_PUBLIC_AUTH_REDIRECT_TO_LOGIN || process.env.REACT_APP_AUTH_REDIRECT_TO_LOGIN || "/auth/login";
3060
+ const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
3061
+ window.location.href = `${baseUrl}${loginPath}`;
3062
+ },
3063
+ style: {
3064
+ padding: "10px 20px",
3065
+ backgroundColor: "#007bff",
3066
+ color: "white",
3067
+ border: "none",
3068
+ borderRadius: "8px",
3069
+ fontSize: "14px",
3070
+ fontWeight: 600,
3071
+ cursor: "pointer"
3072
+ },
3073
+ children: "Go to Login"
3074
+ }
3075
+ )
3076
+ ] })
3077
+ ] });
3078
+ };
3079
+ var ForgotPassword = ({ appearance }) => {
3080
+ const { forgotPassword } = useAuth2();
3081
+ const [email, setEmail] = useState("");
3082
+ const [isLoading, setIsLoading] = useState(false);
3083
+ const [error, setError] = useState(null);
3084
+ const [success, setSuccess] = useState(null);
3085
+ const handleSubmit = async (e) => {
3086
+ e.preventDefault();
3087
+ setIsLoading(true);
3088
+ setError(null);
3089
+ setSuccess(null);
3090
+ try {
3091
+ const response = await forgotPassword(email);
3092
+ if (response.success) {
3093
+ setSuccess("Password reset link sent! Please check your email.");
3094
+ setEmail("");
3095
+ } else {
3096
+ setError(response.message || "Failed to send reset link");
3097
+ }
3098
+ } catch (err) {
3099
+ setError(err instanceof Error ? err.message : "An error occurred");
3100
+ } finally {
3101
+ setIsLoading(false);
3102
+ }
3103
+ };
3104
+ return /* @__PURE__ */ jsx("div", { style: {
3105
+ maxWidth: "400px",
3106
+ margin: "0 auto",
3107
+ padding: "30px",
3108
+ borderRadius: "12px",
3109
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
3110
+ backgroundColor: "#ffffff",
3111
+ border: "1px solid #eaeaea",
3112
+ ...appearance?.elements?.card
3113
+ }, children: /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, children: [
3114
+ /* @__PURE__ */ jsx("h2", { style: {
3115
+ textAlign: "center",
3116
+ marginBottom: "12px",
3117
+ color: "#1f2937",
3118
+ fontSize: "24px",
3119
+ fontWeight: 600,
3120
+ ...appearance?.elements?.headerTitle
3121
+ }, children: "Forgot password?" }),
3122
+ /* @__PURE__ */ jsx("p", { style: {
3123
+ textAlign: "center",
3124
+ marginBottom: "24px",
3125
+ color: "#666",
3126
+ fontSize: "14px"
3127
+ }, children: "Enter your email address and we'll send you a link to reset your password." }),
3128
+ error && /* @__PURE__ */ jsx("div", { style: {
3129
+ padding: "12px 16px",
3130
+ marginBottom: "20px",
3131
+ backgroundColor: "#fee",
3132
+ color: "#c33",
3133
+ border: "1px solid #fcc",
3134
+ borderRadius: "8px",
3135
+ fontSize: "14px"
3136
+ }, children: error }),
3137
+ success && /* @__PURE__ */ jsx("div", { style: {
3138
+ padding: "12px 16px",
3139
+ marginBottom: "20px",
3140
+ backgroundColor: "#efe",
3141
+ color: "#3c3",
3142
+ border: "1px solid #cfc",
3143
+ borderRadius: "8px",
3144
+ fontSize: "14px"
3145
+ }, children: success }),
3146
+ /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px" }, children: [
3147
+ /* @__PURE__ */ jsx("label", { htmlFor: "email", style: {
3148
+ display: "block",
3149
+ marginBottom: "8px",
3150
+ fontWeight: 500,
3151
+ color: "#374151",
3152
+ fontSize: "14px"
3153
+ }, children: "Email" }),
3154
+ /* @__PURE__ */ jsx(
3155
+ "input",
3156
+ {
3157
+ id: "email",
3158
+ type: "email",
3159
+ value: email,
3160
+ onChange: (e) => setEmail(e.target.value),
3161
+ required: true,
3162
+ disabled: isLoading,
3163
+ style: {
3164
+ width: "100%",
3165
+ padding: "12px 16px",
3166
+ border: "1px solid #ddd",
3167
+ borderRadius: "8px",
3168
+ fontSize: "16px",
3169
+ boxSizing: "border-box",
3170
+ ...appearance?.elements?.formFieldInput
3171
+ },
3172
+ placeholder: "Enter your email"
3173
+ }
3174
+ )
3175
+ ] }),
3176
+ /* @__PURE__ */ jsx(
3177
+ "button",
3178
+ {
3179
+ type: "submit",
3180
+ disabled: isLoading,
3181
+ style: {
3182
+ width: "100%",
3183
+ padding: "14px",
3184
+ backgroundColor: "#007bff",
3185
+ color: "white",
3186
+ border: "none",
3187
+ borderRadius: "8px",
3188
+ fontSize: "16px",
3189
+ fontWeight: 600,
3190
+ cursor: "pointer",
3191
+ transition: "all 0.2s ease",
3192
+ marginBottom: "16px",
3193
+ ...appearance?.elements?.formButtonPrimary
3194
+ },
3195
+ children: isLoading ? "Sending..." : "Send reset link"
3196
+ }
3197
+ ),
3198
+ /* @__PURE__ */ jsx("div", { style: { textAlign: "center" }, children: /* @__PURE__ */ jsx(
3199
+ "button",
3200
+ {
3201
+ type: "button",
3202
+ onClick: () => {
3203
+ const loginPath = process.env.NEXT_PUBLIC_AUTH_REDIRECT_TO_LOGIN || process.env.REACT_APP_AUTH_REDIRECT_TO_LOGIN || "/auth/login";
3204
+ const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
3205
+ window.location.href = `${baseUrl}${loginPath}`;
3206
+ },
3207
+ disabled: isLoading,
3208
+ style: {
3209
+ background: "none",
3210
+ border: "none",
3211
+ color: "#007bff",
3212
+ cursor: "pointer",
3213
+ fontSize: "14px",
3214
+ fontWeight: 600
3215
+ },
3216
+ children: "Back to sign in"
3217
+ }
3218
+ ) })
3219
+ ] }) });
3220
+ };
3221
+ var ResetPassword = ({ token, appearance }) => {
3222
+ const { resetPassword } = useAuth2();
3223
+ const [resetToken, setResetToken] = useState(token || "");
3224
+ const [password, setPassword] = useState("");
3225
+ const [confirmPassword, setConfirmPassword] = useState("");
3226
+ const [showPassword, setShowPassword] = useState(false);
3227
+ const [isLoading, setIsLoading] = useState(false);
3228
+ const [error, setError] = useState(null);
3229
+ const [success, setSuccess] = useState(false);
3230
+ useEffect(() => {
3231
+ if (!resetToken && typeof window !== "undefined") {
3232
+ const urlToken = new URLSearchParams(window.location.search).get("token");
3233
+ if (urlToken) {
3234
+ setResetToken(urlToken);
3235
+ }
3236
+ }
3237
+ }, [resetToken]);
3238
+ const getPasswordStrength = (pwd) => {
3239
+ if (!pwd)
3240
+ return { strength: "weak", color: "#ddd" };
3241
+ let score = 0;
3242
+ if (pwd.length >= 6)
3243
+ score++;
3244
+ if (pwd.length >= 8)
3245
+ score++;
3246
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
3247
+ score++;
3248
+ if (/\d/.test(pwd))
3249
+ score++;
3250
+ if (/[^a-zA-Z\d]/.test(pwd))
3251
+ score++;
3252
+ if (score <= 2)
3253
+ return { strength: "weak", color: "#f44" };
3254
+ if (score <= 3)
3255
+ return { strength: "medium", color: "#fa4" };
3256
+ return { strength: "strong", color: "#4f4" };
3257
+ };
3258
+ const passwordStrength = getPasswordStrength(password);
3259
+ const handleSubmit = async (e) => {
3260
+ e.preventDefault();
3261
+ setIsLoading(true);
3262
+ setError(null);
3263
+ if (password !== confirmPassword) {
3264
+ setError("Passwords do not match");
3265
+ setIsLoading(false);
3266
+ return;
3267
+ }
3268
+ if (password.length < 6) {
3269
+ setError("Password must be at least 6 characters");
3270
+ setIsLoading(false);
3271
+ return;
3272
+ }
3273
+ if (!resetToken) {
3274
+ setError("Invalid reset token");
3275
+ setIsLoading(false);
3276
+ return;
3277
+ }
3278
+ try {
3279
+ const response = await resetPassword(resetToken, password);
3280
+ if (response.success) {
3281
+ setSuccess(true);
3282
+ setTimeout(() => {
3283
+ const loginPath = process.env.NEXT_PUBLIC_AUTH_REDIRECT_TO_LOGIN || process.env.REACT_APP_AUTH_REDIRECT_TO_LOGIN || "/auth/login";
3284
+ const baseUrl = process.env.NEXT_PUBLIC_FRONTEND_BASE_URL || process.env.REACT_APP_FRONTEND_BASE_URL || (typeof window !== "undefined" ? window.location.origin : "");
3285
+ window.location.href = `${baseUrl}${loginPath}`;
3286
+ }, 2e3);
3287
+ } else {
3288
+ setError(response.message || "Failed to reset password");
3289
+ }
3290
+ } catch (err) {
3291
+ setError(err instanceof Error ? err.message : "An error occurred");
3292
+ } finally {
3293
+ setIsLoading(false);
3294
+ }
3295
+ };
3296
+ if (success) {
3297
+ return /* @__PURE__ */ jsxs("div", { style: {
3298
+ maxWidth: "400px",
3299
+ margin: "40px auto",
3300
+ padding: "30px",
3301
+ borderRadius: "12px",
3302
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
3303
+ backgroundColor: "#ffffff",
3304
+ border: "1px solid #eaeaea",
3305
+ textAlign: "center"
3306
+ }, children: [
3307
+ /* @__PURE__ */ jsx("div", { style: {
3308
+ width: "64px",
3309
+ height: "64px",
3310
+ margin: "0 auto 20px",
3311
+ backgroundColor: "#4caf50",
3312
+ borderRadius: "50%",
3313
+ display: "flex",
3314
+ alignItems: "center",
3315
+ justifyContent: "center"
3316
+ }, children: /* @__PURE__ */ jsx("svg", { width: "32", height: "32", viewBox: "0 0 24 24", fill: "none", stroke: "white", strokeWidth: "3", children: /* @__PURE__ */ jsx("polyline", { points: "20 6 9 17 4 12" }) }) }),
3317
+ /* @__PURE__ */ jsx("h2", { style: {
3318
+ fontSize: "20px",
3319
+ fontWeight: 600,
3320
+ color: "#333",
3321
+ marginBottom: "12px"
3322
+ }, children: "Password Reset Successful!" }),
3323
+ /* @__PURE__ */ jsx("p", { style: { fontSize: "14px", color: "#666" }, children: "Your password has been reset. Redirecting to login..." })
3324
+ ] });
3325
+ }
3326
+ return /* @__PURE__ */ jsx("div", { style: {
3327
+ maxWidth: "400px",
3328
+ margin: "0 auto",
3329
+ padding: "30px",
3330
+ borderRadius: "12px",
3331
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
3332
+ backgroundColor: "#ffffff",
3333
+ border: "1px solid #eaeaea",
3334
+ ...appearance?.elements?.card
3335
+ }, children: /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, children: [
3336
+ /* @__PURE__ */ jsx("h2", { style: {
3337
+ textAlign: "center",
3338
+ marginBottom: "12px",
3339
+ color: "#1f2937",
3340
+ fontSize: "24px",
3341
+ fontWeight: 600,
3342
+ ...appearance?.elements?.headerTitle
3343
+ }, children: "Reset your password" }),
3344
+ /* @__PURE__ */ jsx("p", { style: {
3345
+ textAlign: "center",
3346
+ marginBottom: "24px",
3347
+ color: "#666",
3348
+ fontSize: "14px"
3349
+ }, children: "Enter your new password below." }),
3350
+ error && /* @__PURE__ */ jsx("div", { style: {
3351
+ padding: "12px 16px",
3352
+ marginBottom: "20px",
3353
+ backgroundColor: "#fee",
3354
+ color: "#c33",
3355
+ border: "1px solid #fcc",
3356
+ borderRadius: "8px",
3357
+ fontSize: "14px"
3358
+ }, children: error }),
3359
+ /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px", position: "relative" }, children: [
3360
+ /* @__PURE__ */ jsx("label", { htmlFor: "password", style: {
3361
+ display: "block",
3362
+ marginBottom: "8px",
3363
+ fontWeight: 500,
3364
+ color: "#374151",
3365
+ fontSize: "14px"
3366
+ }, children: "New password" }),
3367
+ /* @__PURE__ */ jsx(
3368
+ "input",
3369
+ {
3370
+ id: "password",
3371
+ type: showPassword ? "text" : "password",
3372
+ value: password,
3373
+ onChange: (e) => setPassword(e.target.value),
3374
+ required: true,
3375
+ disabled: isLoading,
3376
+ minLength: 6,
3377
+ style: {
3378
+ width: "100%",
3379
+ padding: "12px 16px",
3380
+ border: "1px solid #ddd",
3381
+ borderRadius: "8px",
3382
+ fontSize: "16px",
3383
+ boxSizing: "border-box",
3384
+ ...appearance?.elements?.formFieldInput
3385
+ },
3386
+ placeholder: "Enter new password"
3387
+ }
3388
+ ),
3389
+ /* @__PURE__ */ jsx(
3390
+ "button",
3391
+ {
3392
+ type: "button",
3393
+ onClick: () => setShowPassword(!showPassword),
3394
+ style: {
3395
+ position: "absolute",
3396
+ right: "12px",
3397
+ top: "38px",
3398
+ background: "none",
3399
+ border: "none",
3400
+ cursor: "pointer",
3401
+ color: "#666",
3402
+ fontSize: "14px"
3403
+ },
3404
+ children: showPassword ? "Hide" : "Show"
3405
+ }
3406
+ ),
3407
+ password && /* @__PURE__ */ jsxs("div", { style: { marginTop: "8px" }, children: [
3408
+ /* @__PURE__ */ jsx("div", { style: {
3409
+ height: "4px",
3410
+ backgroundColor: "#eee",
3411
+ borderRadius: "2px",
3412
+ overflow: "hidden"
3413
+ }, children: /* @__PURE__ */ jsx("div", { style: {
3414
+ height: "100%",
3415
+ width: passwordStrength.strength === "weak" ? "33%" : passwordStrength.strength === "medium" ? "66%" : "100%",
3416
+ backgroundColor: passwordStrength.color,
3417
+ transition: "all 0.3s ease"
3418
+ } }) }),
3419
+ /* @__PURE__ */ jsxs("p", { style: {
3420
+ fontSize: "12px",
3421
+ color: passwordStrength.color,
3422
+ marginTop: "4px",
3423
+ textTransform: "capitalize"
3424
+ }, children: [
3425
+ passwordStrength.strength,
3426
+ " password"
3427
+ ] })
3428
+ ] })
3429
+ ] }),
3430
+ /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px" }, children: [
3431
+ /* @__PURE__ */ jsx("label", { htmlFor: "confirmPassword", style: {
3432
+ display: "block",
3433
+ marginBottom: "8px",
3434
+ fontWeight: 500,
3435
+ color: "#374151",
3436
+ fontSize: "14px"
3437
+ }, children: "Confirm password" }),
3438
+ /* @__PURE__ */ jsx(
3439
+ "input",
3440
+ {
3441
+ id: "confirmPassword",
3442
+ type: showPassword ? "text" : "password",
3443
+ value: confirmPassword,
3444
+ onChange: (e) => setConfirmPassword(e.target.value),
3445
+ required: true,
3446
+ disabled: isLoading,
3447
+ style: {
3448
+ width: "100%",
3449
+ padding: "12px 16px",
3450
+ border: "1px solid #ddd",
3451
+ borderRadius: "8px",
3452
+ fontSize: "16px",
3453
+ boxSizing: "border-box",
3454
+ ...appearance?.elements?.formFieldInput
3455
+ },
3456
+ placeholder: "Confirm new password"
3457
+ }
3458
+ )
3459
+ ] }),
3460
+ /* @__PURE__ */ jsx(
3461
+ "button",
3462
+ {
3463
+ type: "submit",
3464
+ disabled: isLoading,
3465
+ style: {
3466
+ width: "100%",
3467
+ padding: "14px",
3468
+ backgroundColor: "#007bff",
3469
+ color: "white",
3470
+ border: "none",
3471
+ borderRadius: "8px",
3472
+ fontSize: "16px",
3473
+ fontWeight: 600,
3474
+ cursor: "pointer",
3475
+ transition: "all 0.2s ease",
3476
+ ...appearance?.elements?.formButtonPrimary
3477
+ },
3478
+ children: isLoading ? "Resetting..." : "Reset password"
3479
+ }
3480
+ )
3481
+ ] }) });
3482
+ };
3483
+ var ChangePassword = ({ onSuccess, appearance }) => {
3484
+ const { changePassword } = useAuth2();
3485
+ const [oldPassword, setOldPassword] = useState("");
3486
+ const [newPassword, setNewPassword] = useState("");
3487
+ const [confirmPassword, setConfirmPassword] = useState("");
3488
+ const [showPasswords, setShowPasswords] = useState(false);
3489
+ const [isLoading, setIsLoading] = useState(false);
3490
+ const [error, setError] = useState(null);
3491
+ const [success, setSuccess] = useState(false);
3492
+ const getPasswordStrength = (pwd) => {
3493
+ if (!pwd)
3494
+ return { strength: "weak", color: "#ddd" };
3495
+ let score = 0;
3496
+ if (pwd.length >= 6)
3497
+ score++;
3498
+ if (pwd.length >= 8)
3499
+ score++;
3500
+ if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))
3501
+ score++;
3502
+ if (/\d/.test(pwd))
3503
+ score++;
3504
+ if (/[^a-zA-Z\d]/.test(pwd))
3505
+ score++;
3506
+ if (score <= 2)
3507
+ return { strength: "weak", color: "#f44" };
3508
+ if (score <= 3)
3509
+ return { strength: "medium", color: "#fa4" };
3510
+ return { strength: "strong", color: "#4f4" };
3511
+ };
3512
+ const passwordStrength = getPasswordStrength(newPassword);
3513
+ const handleSubmit = async (e) => {
3514
+ e.preventDefault();
3515
+ setIsLoading(true);
3516
+ setError(null);
3517
+ setSuccess(false);
3518
+ if (newPassword !== confirmPassword) {
3519
+ setError("New passwords do not match");
3520
+ setIsLoading(false);
3521
+ return;
3522
+ }
3523
+ if (newPassword.length < 6) {
3524
+ setError("New password must be at least 6 characters");
3525
+ setIsLoading(false);
3526
+ return;
3527
+ }
3528
+ try {
3529
+ const response = await changePassword(oldPassword, newPassword);
3530
+ if (response.success) {
3531
+ setSuccess(true);
3532
+ setOldPassword("");
3533
+ setNewPassword("");
3534
+ setConfirmPassword("");
3535
+ onSuccess?.();
3536
+ } else {
3537
+ setError(response.message || "Failed to change password");
3538
+ }
3539
+ } catch (err) {
3540
+ setError(err instanceof Error ? err.message : "An error occurred");
3541
+ } finally {
3542
+ setIsLoading(false);
3543
+ }
3544
+ };
3545
+ return /* @__PURE__ */ jsx("div", { style: {
3546
+ maxWidth: "400px",
3547
+ margin: "0 auto",
3548
+ padding: "30px",
3549
+ borderRadius: "12px",
3550
+ boxShadow: "0 4px 20px rgba(0, 0, 0, 0.1)",
3551
+ backgroundColor: "#ffffff",
3552
+ border: "1px solid #eaeaea",
3553
+ ...appearance?.elements?.card
3554
+ }, children: /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, children: [
3555
+ /* @__PURE__ */ jsx("h2", { style: {
3556
+ textAlign: "center",
3557
+ marginBottom: "24px",
3558
+ color: "#1f2937",
3559
+ fontSize: "24px",
3560
+ fontWeight: 600,
3561
+ ...appearance?.elements?.headerTitle
3562
+ }, children: "Change Password" }),
3563
+ error && /* @__PURE__ */ jsx("div", { style: {
3564
+ padding: "12px 16px",
3565
+ marginBottom: "20px",
3566
+ backgroundColor: "#fee",
3567
+ color: "#c33",
3568
+ border: "1px solid #fcc",
3569
+ borderRadius: "8px",
3570
+ fontSize: "14px"
3571
+ }, children: error }),
3572
+ success && /* @__PURE__ */ jsx("div", { style: {
3573
+ padding: "12px 16px",
3574
+ marginBottom: "20px",
3575
+ backgroundColor: "#efe",
3576
+ color: "#3c3",
3577
+ border: "1px solid #cfc",
3578
+ borderRadius: "8px",
3579
+ fontSize: "14px"
3580
+ }, children: "Password changed successfully!" }),
3581
+ /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px" }, children: [
3582
+ /* @__PURE__ */ jsx("label", { htmlFor: "oldPassword", style: {
3583
+ display: "block",
3584
+ marginBottom: "8px",
3585
+ fontWeight: 500,
3586
+ color: "#374151",
3587
+ fontSize: "14px"
3588
+ }, children: "Current password" }),
3589
+ /* @__PURE__ */ jsx(
3590
+ "input",
3591
+ {
3592
+ id: "oldPassword",
3593
+ type: showPasswords ? "text" : "password",
3594
+ value: oldPassword,
3595
+ onChange: (e) => setOldPassword(e.target.value),
3596
+ required: true,
3597
+ disabled: isLoading,
3598
+ style: {
3599
+ width: "100%",
3600
+ padding: "12px 16px",
3601
+ border: "1px solid #ddd",
3602
+ borderRadius: "8px",
3603
+ fontSize: "16px",
3604
+ boxSizing: "border-box",
3605
+ ...appearance?.elements?.formFieldInput
3606
+ },
3607
+ placeholder: "Enter current password"
3608
+ }
3609
+ )
3610
+ ] }),
3611
+ /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px" }, children: [
3612
+ /* @__PURE__ */ jsx("label", { htmlFor: "newPassword", style: {
3613
+ display: "block",
3614
+ marginBottom: "8px",
3615
+ fontWeight: 500,
3616
+ color: "#374151",
3617
+ fontSize: "14px"
3618
+ }, children: "New password" }),
3619
+ /* @__PURE__ */ jsx(
3620
+ "input",
3621
+ {
3622
+ id: "newPassword",
3623
+ type: showPasswords ? "text" : "password",
3624
+ value: newPassword,
3625
+ onChange: (e) => setNewPassword(e.target.value),
3626
+ required: true,
3627
+ disabled: isLoading,
3628
+ minLength: 6,
3629
+ style: {
3630
+ width: "100%",
3631
+ padding: "12px 16px",
3632
+ border: "1px solid #ddd",
3633
+ borderRadius: "8px",
3634
+ fontSize: "16px",
3635
+ boxSizing: "border-box",
3636
+ ...appearance?.elements?.formFieldInput
3637
+ },
3638
+ placeholder: "Enter new password"
3639
+ }
3640
+ ),
3641
+ newPassword && /* @__PURE__ */ jsxs("div", { style: { marginTop: "8px" }, children: [
3642
+ /* @__PURE__ */ jsx("div", { style: {
3643
+ height: "4px",
3644
+ backgroundColor: "#eee",
3645
+ borderRadius: "2px",
3646
+ overflow: "hidden"
3647
+ }, children: /* @__PURE__ */ jsx("div", { style: {
3648
+ height: "100%",
3649
+ width: passwordStrength.strength === "weak" ? "33%" : passwordStrength.strength === "medium" ? "66%" : "100%",
3650
+ backgroundColor: passwordStrength.color,
3651
+ transition: "all 0.3s ease"
3652
+ } }) }),
3653
+ /* @__PURE__ */ jsxs("p", { style: {
3654
+ fontSize: "12px",
3655
+ color: passwordStrength.color,
3656
+ marginTop: "4px",
3657
+ textTransform: "capitalize"
3658
+ }, children: [
3659
+ passwordStrength.strength,
3660
+ " password"
3661
+ ] })
3662
+ ] })
3663
+ ] }),
3664
+ /* @__PURE__ */ jsxs("div", { style: { marginBottom: "20px" }, children: [
3665
+ /* @__PURE__ */ jsx("label", { htmlFor: "confirmPassword", style: {
3666
+ display: "block",
3667
+ marginBottom: "8px",
3668
+ fontWeight: 500,
3669
+ color: "#374151",
3670
+ fontSize: "14px"
3671
+ }, children: "Confirm new password" }),
3672
+ /* @__PURE__ */ jsx(
3673
+ "input",
3674
+ {
3675
+ id: "confirmPassword",
3676
+ type: showPasswords ? "text" : "password",
3677
+ value: confirmPassword,
3678
+ onChange: (e) => setConfirmPassword(e.target.value),
3679
+ required: true,
3680
+ disabled: isLoading,
3681
+ style: {
3682
+ width: "100%",
3683
+ padding: "12px 16px",
3684
+ border: "1px solid #ddd",
3685
+ borderRadius: "8px",
3686
+ fontSize: "16px",
3687
+ boxSizing: "border-box",
3688
+ ...appearance?.elements?.formFieldInput
3689
+ },
3690
+ placeholder: "Confirm new password"
3691
+ }
3692
+ )
3693
+ ] }),
3694
+ /* @__PURE__ */ jsx("div", { style: { marginBottom: "20px" }, children: /* @__PURE__ */ jsxs("label", { style: { display: "flex", alignItems: "center", cursor: "pointer" }, children: [
3695
+ /* @__PURE__ */ jsx(
3696
+ "input",
3697
+ {
3698
+ type: "checkbox",
3699
+ checked: showPasswords,
3700
+ onChange: (e) => setShowPasswords(e.target.checked),
3701
+ style: { marginRight: "8px" }
3702
+ }
3703
+ ),
3704
+ /* @__PURE__ */ jsx("span", { style: { fontSize: "14px", color: "#666" }, children: "Show passwords" })
3705
+ ] }) }),
3706
+ /* @__PURE__ */ jsx(
3707
+ "button",
3708
+ {
3709
+ type: "submit",
3710
+ disabled: isLoading,
3711
+ style: {
3712
+ width: "100%",
3713
+ padding: "14px",
3714
+ backgroundColor: "#007bff",
3715
+ color: "white",
3716
+ border: "none",
3717
+ borderRadius: "8px",
3718
+ fontSize: "16px",
3719
+ fontWeight: 600,
3720
+ cursor: "pointer",
3721
+ transition: "all 0.2s ease",
3722
+ ...appearance?.elements?.formButtonPrimary
3723
+ },
3724
+ children: isLoading ? "Changing password..." : "Change password"
3725
+ }
3726
+ )
3727
+ ] }) });
3728
+ };
3729
+ var UserProfile = ({
3730
+ showAvatar = true,
3731
+ showEmailChange = true,
3732
+ showPasswordChange = true
3733
+ }) => {
3734
+ const { user, updateProfile, updateAvatar, requestEmailChange } = useAuth2();
3735
+ const [name, setName] = useState(user?.name || "");
3736
+ const [avatar, setAvatar] = useState("");
3737
+ const [newEmail, setNewEmail] = useState("");
3738
+ const [isLoading, setIsLoading] = useState(false);
3739
+ const [error, setError] = useState(null);
3740
+ const [success, setSuccess] = useState(null);
3741
+ const handleUpdateName = async (e) => {
3742
+ e.preventDefault();
3743
+ setIsLoading(true);
3744
+ setError(null);
3745
+ setSuccess(null);
3746
+ try {
3747
+ const response = await updateProfile({ name });
3748
+ if (response.success) {
3749
+ setSuccess("Name updated successfully!");
3750
+ } else {
3751
+ setError(response.message || "Failed to update name");
3752
+ }
3753
+ } catch (err) {
3754
+ setError(err instanceof Error ? err.message : "An error occurred");
3755
+ } finally {
3756
+ setIsLoading(false);
3757
+ }
3758
+ };
3759
+ const handleUpdateAvatar = async (e) => {
3760
+ e.preventDefault();
3761
+ setIsLoading(true);
3762
+ setError(null);
3763
+ setSuccess(null);
3764
+ try {
3765
+ const response = await updateAvatar(avatar);
3766
+ if (response.success) {
3767
+ setSuccess("Avatar updated successfully!");
3768
+ setAvatar("");
3769
+ } else {
3770
+ setError(response.message || "Failed to update avatar");
3771
+ }
3772
+ } catch (err) {
3773
+ setError(err instanceof Error ? err.message : "An error occurred");
3774
+ } finally {
3775
+ setIsLoading(false);
3776
+ }
3777
+ };
3778
+ const handleRequestEmailChange = async (e) => {
3779
+ e.preventDefault();
3780
+ setIsLoading(true);
3781
+ setError(null);
3782
+ setSuccess(null);
3783
+ try {
3784
+ const response = await requestEmailChange(newEmail);
3785
+ if (response.success) {
3786
+ setSuccess("Verification email sent! Please check your inbox.");
3787
+ setNewEmail("");
3788
+ } else {
3789
+ setError(response.message || "Failed to request email change");
3790
+ }
3791
+ } catch (err) {
3792
+ setError(err instanceof Error ? err.message : "An error occurred");
3793
+ } finally {
3794
+ setIsLoading(false);
3795
+ }
3796
+ };
3797
+ if (!user)
3798
+ return null;
3799
+ return /* @__PURE__ */ jsxs("div", { style: { maxWidth: "600px", margin: "0 auto", padding: "20px" }, children: [
3800
+ /* @__PURE__ */ jsx("h2", { style: { marginBottom: "24px", fontSize: "24px", fontWeight: 600 }, children: "Profile Settings" }),
3801
+ error && /* @__PURE__ */ jsx("div", { style: {
3802
+ padding: "12px 16px",
3803
+ marginBottom: "20px",
3804
+ backgroundColor: "#fee",
3805
+ color: "#c33",
3806
+ border: "1px solid #fcc",
3807
+ borderRadius: "8px",
3808
+ fontSize: "14px"
3809
+ }, children: error }),
3810
+ success && /* @__PURE__ */ jsx("div", { style: {
3811
+ padding: "12px 16px",
3812
+ marginBottom: "20px",
3813
+ backgroundColor: "#efe",
3814
+ color: "#3c3",
3815
+ border: "1px solid #cfc",
3816
+ borderRadius: "8px",
3817
+ fontSize: "14px"
3818
+ }, children: success }),
3819
+ /* @__PURE__ */ jsxs("div", { style: {
3820
+ padding: "20px",
3821
+ backgroundColor: "#fff",
3822
+ borderRadius: "8px",
3823
+ boxShadow: "0 2px 8px rgba(0,0,0,0.1)",
3824
+ marginBottom: "20px"
3825
+ }, children: [
3826
+ /* @__PURE__ */ jsx("h3", { style: { marginBottom: "16px", fontSize: "18px", fontWeight: 600 }, children: "Update Name" }),
3827
+ /* @__PURE__ */ jsxs("form", { onSubmit: handleUpdateName, children: [
3828
+ /* @__PURE__ */ jsx(
3829
+ "input",
3830
+ {
3831
+ type: "text",
3832
+ value: name,
3833
+ onChange: (e) => setName(e.target.value),
3834
+ required: true,
3835
+ disabled: isLoading,
3836
+ style: {
3837
+ width: "100%",
3838
+ padding: "12px 16px",
3839
+ border: "1px solid #ddd",
3840
+ borderRadius: "8px",
3841
+ fontSize: "16px",
3842
+ boxSizing: "border-box",
3843
+ marginBottom: "12px"
3844
+ },
3845
+ placeholder: "Your name"
3846
+ }
3847
+ ),
3848
+ /* @__PURE__ */ jsx(
3849
+ "button",
3850
+ {
3851
+ type: "submit",
3852
+ disabled: isLoading,
3853
+ style: {
3854
+ padding: "10px 20px",
3855
+ backgroundColor: "#007bff",
3856
+ color: "white",
3857
+ border: "none",
3858
+ borderRadius: "8px",
3859
+ fontSize: "14px",
3860
+ fontWeight: 600,
3861
+ cursor: "pointer"
3862
+ },
3863
+ children: "Update Name"
3864
+ }
3865
+ )
3866
+ ] })
3867
+ ] }),
3868
+ showAvatar && /* @__PURE__ */ jsxs("div", { style: {
3869
+ padding: "20px",
3870
+ backgroundColor: "#fff",
3871
+ borderRadius: "8px",
3872
+ boxShadow: "0 2px 8px rgba(0,0,0,0.1)",
3873
+ marginBottom: "20px"
3874
+ }, children: [
3875
+ /* @__PURE__ */ jsx("h3", { style: { marginBottom: "16px", fontSize: "18px", fontWeight: 600 }, children: "Update Avatar" }),
3876
+ /* @__PURE__ */ jsxs("form", { onSubmit: handleUpdateAvatar, children: [
3877
+ /* @__PURE__ */ jsx(
3878
+ "input",
3879
+ {
3880
+ type: "url",
3881
+ value: avatar,
3882
+ onChange: (e) => setAvatar(e.target.value),
3883
+ required: true,
3884
+ disabled: isLoading,
3885
+ style: {
3886
+ width: "100%",
3887
+ padding: "12px 16px",
3888
+ border: "1px solid #ddd",
3889
+ borderRadius: "8px",
3890
+ fontSize: "16px",
3891
+ boxSizing: "border-box",
3892
+ marginBottom: "12px"
3893
+ },
3894
+ placeholder: "Avatar URL"
3895
+ }
3896
+ ),
3897
+ /* @__PURE__ */ jsx(
3898
+ "button",
3899
+ {
3900
+ type: "submit",
3901
+ disabled: isLoading,
3902
+ style: {
3903
+ padding: "10px 20px",
3904
+ backgroundColor: "#007bff",
3905
+ color: "white",
3906
+ border: "none",
3907
+ borderRadius: "8px",
3908
+ fontSize: "14px",
3909
+ fontWeight: 600,
3910
+ cursor: "pointer"
3911
+ },
3912
+ children: "Update Avatar"
3913
+ }
3914
+ )
3915
+ ] })
3916
+ ] }),
3917
+ showEmailChange && /* @__PURE__ */ jsxs("div", { style: {
3918
+ padding: "20px",
3919
+ backgroundColor: "#fff",
3920
+ borderRadius: "8px",
3921
+ boxShadow: "0 2px 8px rgba(0,0,0,0.1)",
3922
+ marginBottom: "20px"
3923
+ }, children: [
3924
+ /* @__PURE__ */ jsx("h3", { style: { marginBottom: "16px", fontSize: "18px", fontWeight: 600 }, children: "Change Email" }),
3925
+ /* @__PURE__ */ jsxs("p", { style: { fontSize: "14px", color: "#666", marginBottom: "12px" }, children: [
3926
+ "Current email: ",
3927
+ /* @__PURE__ */ jsx("strong", { children: user.email })
3928
+ ] }),
3929
+ /* @__PURE__ */ jsxs("form", { onSubmit: handleRequestEmailChange, children: [
3930
+ /* @__PURE__ */ jsx(
3931
+ "input",
3932
+ {
3933
+ type: "email",
3934
+ value: newEmail,
3935
+ onChange: (e) => setNewEmail(e.target.value),
3936
+ required: true,
3937
+ disabled: isLoading,
3938
+ style: {
3939
+ width: "100%",
3940
+ padding: "12px 16px",
3941
+ border: "1px solid #ddd",
3942
+ borderRadius: "8px",
3943
+ fontSize: "16px",
3944
+ boxSizing: "border-box",
3945
+ marginBottom: "12px"
3946
+ },
3947
+ placeholder: "New email address"
3948
+ }
3949
+ ),
3950
+ /* @__PURE__ */ jsx(
3951
+ "button",
3952
+ {
3953
+ type: "submit",
3954
+ disabled: isLoading,
3955
+ style: {
3956
+ padding: "10px 20px",
3957
+ backgroundColor: "#007bff",
3958
+ color: "white",
3959
+ border: "none",
3960
+ borderRadius: "8px",
3961
+ fontSize: "14px",
3962
+ fontWeight: 600,
3963
+ cursor: "pointer"
3964
+ },
3965
+ children: "Request Email Change"
3966
+ }
3967
+ )
3968
+ ] })
3969
+ ] })
3970
+ ] });
3971
+ };
3972
+ var isServer = typeof window === "undefined";
3973
+ var useNextAuth = (config) => {
3974
+ const [authService] = useState(() => {
3975
+ const service = new AuthService(config);
3976
+ if (isServer && config.token) {
3977
+ service["token"] = config.token;
3978
+ service["httpClient"].setAuthToken(config.token);
3979
+ }
3980
+ return service;
3981
+ });
3982
+ const [user, setUser] = useState(null);
1753
3983
  const [isAuthenticated, setIsAuthenticated] = useState(false);
1754
3984
  const [loading, setLoading] = useState(true);
1755
3985
  const checkAuthStatus = useCallback(() => {
@@ -1786,11 +4016,7 @@ var useNextAuth = (config) => {
1786
4016
  const register = useCallback(async (data) => {
1787
4017
  setLoading(true);
1788
4018
  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);
4019
+ const response = await authService.register(data);
1794
4020
  return response;
1795
4021
  } finally {
1796
4022
  setLoading(false);
@@ -1935,144 +4161,6 @@ var useNextAuth = (config) => {
1935
4161
  };
1936
4162
  };
1937
4163
 
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 };
4164
+ export { AuthFlow, AuthProvider, AuthService, ChangePassword, EmailVerificationPage, ForgotPassword, HttpClient, LoginForm, OtpForm, ProtectedRoute, PublicRoute, RegisterForm, ResetPassword, SignIn, SignOut, SignUp, UserButton, UserProfile, VerifyEmail, useAuth2 as useAuth, useAuth as useAuthLegacy, useNextAuth };
2077
4165
  //# sourceMappingURL=out.js.map
2078
4166
  //# sourceMappingURL=index.next.mjs.map