@thetechfossil/auth2 1.2.1 → 1.2.2

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