cca-auth-module 0.1.81 → 0.1.83

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -704,16 +704,19 @@ var _AuthController = class _AuthController {
704
704
  try {
705
705
  const loginDTO = req.body;
706
706
  const result = await this.loginUseCase.execute(loginDTO);
707
- res.status(200).json({
708
- status: "success",
709
- message: "Login successful",
710
- data: {
707
+ const response = this.createSuccessResponse(
708
+ "Login successful",
709
+ {
711
710
  accessToken: result.accessToken,
712
711
  userId: result.id,
713
712
  expiresAt: result.expiresAt,
714
713
  enabled: result.enabled
714
+ },
715
+ {
716
+ status: "success"
715
717
  }
716
- });
718
+ );
719
+ res.status(200).json(response);
717
720
  } catch (error) {
718
721
  next(error);
719
722
  }
@@ -725,7 +728,11 @@ var _AuthController = class _AuthController {
725
728
  throw new ForbiddenError("Admin password is required");
726
729
  }
727
730
  const result = await this.adminLoginUseCase.execute(loginDTO, adminPassword);
728
- res.status(201).json(result);
731
+ const response = this.createSuccessResponse(
732
+ "Admin login successful",
733
+ result
734
+ );
735
+ res.status(201).json(response);
729
736
  } catch (error) {
730
737
  next(error);
731
738
  }
@@ -733,10 +740,8 @@ var _AuthController = class _AuthController {
733
740
  this.logout = /* @__PURE__ */ __name(async (req, res, next) => {
734
741
  try {
735
742
  await this.logoutUseCase.execute(req.body.id);
736
- res.status(200).json({
737
- status: "success",
738
- message: "Logged out successfully"
739
- });
743
+ const response = this.createSuccessResponse("Logged out successfully");
744
+ res.status(200).json(response);
740
745
  } catch (error) {
741
746
  next(error);
742
747
  }
@@ -745,37 +750,47 @@ var _AuthController = class _AuthController {
745
750
  try {
746
751
  const { email, name, password, role, adminPassword } = req.body;
747
752
  await this.registerUseCase.execute(email, name, password, role, adminPassword);
748
- res.status(200).json({ status: "success" });
753
+ const response = this.createSuccessResponse(
754
+ "User registered successfully",
755
+ null,
756
+ { status: "success" }
757
+ );
758
+ res.status(200).json(response);
749
759
  } catch (error) {
750
760
  next(error);
751
761
  }
752
762
  }, "register");
753
- this.refreshToken = /* @__PURE__ */ __name(async (req, res) => {
754
- const { refreshToken } = req.body;
755
- const result = await this.refreshTokenUseCase.execute(refreshToken);
756
- res.json(result);
763
+ this.refreshToken = /* @__PURE__ */ __name(async (req, res, next) => {
764
+ try {
765
+ const { refreshToken } = req.body;
766
+ const result = await this.refreshTokenUseCase.execute(refreshToken);
767
+ const response = this.createSuccessResponse(
768
+ "Token refreshed successfully",
769
+ result
770
+ );
771
+ res.json(response);
772
+ } catch (error) {
773
+ next(error);
774
+ }
757
775
  }, "refreshToken");
758
776
  this.setup2FA = /* @__PURE__ */ __name(async (req, res, next) => {
759
777
  try {
760
- if (!req.auth?.id) throw new ForbiddenError("User authentication required");
778
+ if (!req.auth?.id) {
779
+ throw new ForbiddenError("User authentication required");
780
+ }
761
781
  const result = await this.twoFactorSetupUseCase.execute(req.auth.id);
762
- res.status(200).json({
763
- success: true,
764
- message: "Two-factor authentication setup initiated",
765
- data: {
782
+ const response = this.createSuccessResponse(
783
+ "Two-factor authentication setup initiated",
784
+ {
766
785
  qrCode: result.qrCodeUrl,
767
- auth: {
768
- hasAccessToken: true,
769
- enable: false,
770
- status: "needs_setup"
771
- }
786
+ auth: this.createAuthData(true, false, "needs_setup")
772
787
  },
773
- meta: {
774
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
788
+ {
775
789
  nextStep: "Scan the QR code and enter your first code to verify",
776
790
  redirectTo: "/2fa-setup"
777
791
  }
778
- });
792
+ );
793
+ res.status(200).json(response);
779
794
  } catch (error) {
780
795
  next(error);
781
796
  }
@@ -784,24 +799,19 @@ var _AuthController = class _AuthController {
784
799
  try {
785
800
  const dto = { ...req.body, userId: req.auth?.id };
786
801
  await this.twoFactorEnableUseCase.execute(dto);
787
- res.status(200).json({
788
- success: true,
789
- message: "Two-factor authentication enabled",
790
- data: {
802
+ const response = this.createSuccessResponse(
803
+ "Two-factor authentication enabled",
804
+ {
791
805
  isEnabled: true,
792
806
  enabledAt: (/* @__PURE__ */ new Date()).toISOString(),
793
- auth: {
794
- hasAccessToken: true,
795
- enable: true,
796
- status: "pending_verification"
797
- }
807
+ auth: this.createAuthData(true, true, "pending_verification")
798
808
  },
799
- meta: {
800
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
809
+ {
801
810
  nextStep: "Proceed to verify with a valid 2FA token",
802
811
  redirectTo: "/verify-2fa"
803
812
  }
804
- });
813
+ );
814
+ res.status(200).json(response);
805
815
  } catch (error) {
806
816
  next(error);
807
817
  }
@@ -810,31 +820,25 @@ var _AuthController = class _AuthController {
810
820
  try {
811
821
  const dto = req.body;
812
822
  const result = await this.twoFactorVerifyUseCase.execute(dto);
813
- res.status(200).json({
814
- success: true,
815
- message: "Two-factor authentication verified successfully",
816
- data: {
817
- verified: true,
823
+ const response = this.createSuccessResponse(
824
+ "Two-factor authentication verified successfully",
825
+ {
818
826
  token: result?.token,
819
827
  refreshToken: result?.refreshToken,
820
828
  user: {
821
829
  id: result?.data?.id,
822
830
  email: result?.data?.email,
831
+ name: result?.data?.name,
823
832
  role: result?.data?.role
824
833
  },
825
- auth: {
826
- hasAccessToken: true,
827
- enable: true,
828
- verified: true,
829
- status: "full_auth"
830
- }
834
+ auth: this.createAuthData(true, true, "full_auth", true)
831
835
  },
832
- meta: {
833
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
836
+ {
834
837
  recommendation: "You're fully authenticated",
835
838
  redirectTo: "/"
836
839
  }
837
- });
840
+ );
841
+ res.status(200).json(response);
838
842
  } catch (error) {
839
843
  next(error);
840
844
  }
@@ -844,25 +848,18 @@ var _AuthController = class _AuthController {
844
848
  const userId = req.auth.id;
845
849
  const dto = req.body;
846
850
  await this.twoFactorDisableUseCase.execute(userId, dto);
847
- res.status(200).json({
848
- success: true,
849
- message: "Two-factor authentication disabled",
850
- data: {
851
- isEnabled: false,
851
+ const response = this.createSuccessResponse(
852
+ "Two-factor authentication disabled",
853
+ {
852
854
  disabledAt: (/* @__PURE__ */ new Date()).toISOString(),
853
- auth: {
854
- hasAccessToken: true,
855
- enable: false,
856
- verified: false,
857
- status: "basic_auth"
858
- }
855
+ auth: this.createAuthData(true, false, "basic_auth", false)
859
856
  },
860
- meta: {
861
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
857
+ {
862
858
  securityNote: "Account now relies only on password. Re-enable 2FA for better security.",
863
859
  redirectTo: "/login"
864
860
  }
865
- });
861
+ );
862
+ res.status(200).json(response);
866
863
  } catch (error) {
867
864
  next(error);
868
865
  }
@@ -877,6 +874,25 @@ var _AuthController = class _AuthController {
877
874
  this.twoFactorVerifyUseCase = twoFactorVerifyUseCase;
878
875
  this.twoFactorDisableUseCase = twoFactorDisableUseCase;
879
876
  }
877
+ createSuccessResponse(message, data, meta) {
878
+ return {
879
+ success: true,
880
+ message,
881
+ data,
882
+ meta: {
883
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
884
+ ...meta
885
+ }
886
+ };
887
+ }
888
+ createAuthData(hasAccessToken, enable, status, verified) {
889
+ return {
890
+ hasAccessToken,
891
+ enable,
892
+ status,
893
+ ...verified !== void 0 && { verified }
894
+ };
895
+ }
880
896
  };
881
897
  __name(_AuthController, "AuthController");
882
898
  var AuthController = _AuthController;