@explita/cloud-auth-client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/README.md +238 -0
  2. package/dist/components/change-password.d.ts +11 -0
  3. package/dist/components/change-password.js +101 -0
  4. package/dist/components/loader.d.ts +2 -0
  5. package/dist/components/loader.js +11 -0
  6. package/dist/components/logged-in.d.ts +3 -0
  7. package/dist/components/logged-in.js +9 -0
  8. package/dist/components/logged-out.d.ts +3 -0
  9. package/dist/components/logged-out.js +9 -0
  10. package/dist/components/login-form.d.ts +2 -0
  11. package/dist/components/login-form.js +102 -0
  12. package/dist/components/message.d.ts +6 -0
  13. package/dist/components/message.js +15 -0
  14. package/dist/components/must-login.d.ts +4 -0
  15. package/dist/components/must-login.js +23 -0
  16. package/dist/components/optional-otp-wrapper.d.ts +3 -0
  17. package/dist/components/optional-otp-wrapper.js +52 -0
  18. package/dist/components/optional-otp.d.ts +3 -0
  19. package/dist/components/optional-otp.js +70 -0
  20. package/dist/components/reset-password.d.ts +8 -0
  21. package/dist/components/reset-password.js +118 -0
  22. package/dist/components/roles.d.ts +6 -0
  23. package/dist/components/roles.js +54 -0
  24. package/dist/components/settings.d.ts +9 -0
  25. package/dist/components/settings.js +26 -0
  26. package/dist/components/signup-form.d.ts +11 -0
  27. package/dist/components/signup-form.js +138 -0
  28. package/dist/components/toggle-2fa.d.ts +7 -0
  29. package/dist/components/toggle-2fa.js +81 -0
  30. package/dist/components/toggle-account-status.d.ts +11 -0
  31. package/dist/components/toggle-account-status.js +91 -0
  32. package/dist/components/ui/button.d.ts +10 -0
  33. package/dist/components/ui/button.js +68 -0
  34. package/dist/components/ui/card.d.ts +9 -0
  35. package/dist/components/ui/card.js +65 -0
  36. package/dist/components/ui/dialog.d.ts +15 -0
  37. package/dist/components/ui/dialog.js +86 -0
  38. package/dist/components/ui/dropdown-menu.d.ts +11 -0
  39. package/dist/components/ui/dropdown-menu.js +63 -0
  40. package/dist/components/ui/input.d.ts +3 -0
  41. package/dist/components/ui/input.js +41 -0
  42. package/dist/components/ui/label.d.ts +3 -0
  43. package/dist/components/ui/label.js +42 -0
  44. package/dist/components/ui/switch.d.ts +4 -0
  45. package/dist/components/ui/switch.js +44 -0
  46. package/dist/components/user-card.d.ts +10 -0
  47. package/dist/components/user-card.js +67 -0
  48. package/dist/components/x-icon.d.ts +2 -0
  49. package/dist/components/x-icon.js +11 -0
  50. package/dist/contexts/auth-provider.d.ts +4 -0
  51. package/dist/contexts/auth-provider.js +208 -0
  52. package/dist/hooks/use-token-refresher.d.ts +2 -0
  53. package/dist/hooks/use-token-refresher.js +118 -0
  54. package/dist/index.d.ts +4 -0
  55. package/dist/index.js +21 -0
  56. package/dist/lib/api-client.d.ts +2 -0
  57. package/dist/lib/api-client.js +38 -0
  58. package/dist/lib/api-server.d.ts +2 -0
  59. package/dist/lib/api-server.js +39 -0
  60. package/dist/lib/api.d.ts +2 -0
  61. package/dist/lib/api.js +10 -0
  62. package/dist/lib/error.d.ts +6 -0
  63. package/dist/lib/error.js +15 -0
  64. package/dist/lib/refresh-helper.d.ts +12 -0
  65. package/dist/lib/refresh-helper.js +82 -0
  66. package/dist/lib/utils.d.ts +21 -0
  67. package/dist/lib/utils.js +148 -0
  68. package/dist/server/cookie.d.ts +1 -0
  69. package/dist/server/cookie.js +29 -0
  70. package/dist/server/index.d.ts +7 -0
  71. package/dist/server/index.js +23 -0
  72. package/dist/server/reset-password.d.ts +3 -0
  73. package/dist/server/reset-password.js +41 -0
  74. package/dist/server/role.d.ts +7 -0
  75. package/dist/server/role.js +63 -0
  76. package/dist/server/server-session.d.ts +8 -0
  77. package/dist/server/server-session.js +31 -0
  78. package/dist/server/server-token.d.ts +4 -0
  79. package/dist/server/server-token.js +39 -0
  80. package/dist/server/toggle-2fa.d.ts +4 -0
  81. package/dist/server/toggle-2fa.js +33 -0
  82. package/dist/server/users-accounts.d.ts +11 -0
  83. package/dist/server/users-accounts.js +94 -0
  84. package/dist/styles.css +1799 -0
  85. package/dist/types.d.ts +284 -0
  86. package/dist/types.js +2 -0
  87. package/dist/ui/index.d.ts +11 -0
  88. package/dist/ui/index.js +27 -0
  89. package/package.json +65 -0
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cn = cn;
4
+ exports.unstuckPointerEvents = unstuckPointerEvents;
5
+ exports.buildRouteContext = buildRouteContext;
6
+ exports.getClientToken = getClientToken;
7
+ exports.hasPermission = hasPermission;
8
+ exports.parseMessage = parseMessage;
9
+ exports.parseJwt = parseJwt;
10
+ exports.isOtpAvailable = isOtpAvailable;
11
+ exports.shouldPassFullUserObject = shouldPassFullUserObject;
12
+ exports.getPublicTokenEnv = getPublicTokenEnv;
13
+ exports.getPrivateTokenEnv = getPrivateTokenEnv;
14
+ function cn(...classes) {
15
+ return classes.filter(Boolean).join(" ");
16
+ }
17
+ function unstuckPointerEvents() {
18
+ // Fix Radix edge case: pointer-events stuck after opening controlled Dialog from Dropdown
19
+ setTimeout(() => {
20
+ if (document.body.style.pointerEvents === "none") {
21
+ document.body.style.pointerEvents = "";
22
+ }
23
+ }, 10);
24
+ }
25
+ const defaultAuthConfig = {
26
+ loginPath: "/login",
27
+ signupPath: "/signup",
28
+ dashboardPath: "/dashboard",
29
+ resetPasswordPath: "/forgot-password",
30
+ excludedPaths: [],
31
+ };
32
+ function buildRouteContext(config, currentPath = "") {
33
+ var _a;
34
+ const finalConfig = { ...defaultAuthConfig, ...config };
35
+ const hostOverride = finalConfig.hostOverride;
36
+ currentPath =
37
+ typeof window !== "undefined" ? window.location.pathname : currentPath;
38
+ const normalize = (input) => {
39
+ try {
40
+ return new URL(input).pathname.replace(/\/+$/, "");
41
+ }
42
+ catch {
43
+ return input.replace(/\/+$/, "");
44
+ }
45
+ };
46
+ const loginUrl = getAbsoluteUrl(finalConfig.loginPath, hostOverride);
47
+ const signupUrl = getAbsoluteUrl(finalConfig.signupPath, hostOverride);
48
+ const dashboardUrl = getAbsoluteUrl(finalConfig.dashboardPath, hostOverride);
49
+ const resetPasswordUrl = getAbsoluteUrl(finalConfig.resetPasswordPath, hostOverride);
50
+ const excludedPaths = [
51
+ normalize(loginUrl),
52
+ normalize(signupUrl),
53
+ ...((_a = config === null || config === void 0 ? void 0 : config.excludedPaths) !== null && _a !== void 0 ? _a : []),
54
+ ];
55
+ try {
56
+ if (resetPasswordUrl) {
57
+ excludedPaths.push(normalize(resetPasswordUrl));
58
+ }
59
+ }
60
+ catch (e) {
61
+ console.warn("Invalid resetPasswordUrl:", e.message);
62
+ }
63
+ const normalizedCurrentPath = normalize(currentPath);
64
+ const isExcluded = excludedPaths.includes(normalizedCurrentPath);
65
+ const returnPath = typeof window !== "undefined"
66
+ ? encodeURIComponent(`${window.location.pathname}${window.location.search}`)
67
+ : encodeURIComponent(currentPath);
68
+ return {
69
+ loginUrl,
70
+ signupUrl,
71
+ dashboardUrl,
72
+ resetPasswordUrl,
73
+ returnPath,
74
+ currentPath: normalizedCurrentPath,
75
+ excludedPaths,
76
+ isExcluded,
77
+ };
78
+ }
79
+ function getAbsoluteUrl(pathOrUrl, hostOverride) {
80
+ try {
81
+ const url = new URL(pathOrUrl, hostOverride !== null && hostOverride !== void 0 ? hostOverride : "");
82
+ return url.toString();
83
+ }
84
+ catch {
85
+ if (typeof window !== "undefined") {
86
+ const base = hostOverride !== null && hostOverride !== void 0 ? hostOverride : window.location.origin;
87
+ return new URL(pathOrUrl.replace(/^\/+/, "/"), base).toString();
88
+ }
89
+ return `http://localhost${pathOrUrl.startsWith("/") ? "" : "/"}${pathOrUrl}`;
90
+ }
91
+ }
92
+ function getClientToken() {
93
+ var _a;
94
+ return typeof localStorage !== "undefined"
95
+ ? ((_a = localStorage.getItem("_ecp_auth_token")) !== null && _a !== void 0 ? _a : "")
96
+ : "";
97
+ }
98
+ function hasPermission(user, permission) {
99
+ var _a;
100
+ if (!user)
101
+ return false;
102
+ return ((_a = user.role.permissions) === null || _a === void 0 ? void 0 : _a.includes(permission)) || user.isSuperAdmin;
103
+ }
104
+ function parseMessage(message) {
105
+ return message == "fetch failed" || message == "Failed to fetch"
106
+ ? "Please check your internet connection"
107
+ : message;
108
+ }
109
+ function base64UrlDecode(b64url) {
110
+ // Replace URL-safe chars
111
+ let base64 = b64url.replace(/-/g, "+").replace(/_/g, "/");
112
+ // Pad with "=" if needed
113
+ while (base64.length % 4 !== 0) {
114
+ base64 += "=";
115
+ }
116
+ return atob(base64);
117
+ }
118
+ function parseJwt(token) {
119
+ const parts = token.split(".");
120
+ if (parts.length !== 3)
121
+ throw new Error("Invalid JWT format");
122
+ const payload = base64UrlDecode(parts[1]);
123
+ return JSON.parse(payload);
124
+ }
125
+ function isOtpAvailable() {
126
+ try {
127
+ require.resolve("@explita/cloud-otp-client");
128
+ return true;
129
+ }
130
+ catch {
131
+ return false;
132
+ }
133
+ }
134
+ function shouldPassFullUserObject() {
135
+ return (process.env.NEXT_PUBLIC_ECP_PASS_FULL_USER_OBJECT === "true" ||
136
+ process.env.VITE_ECP_PASS_FULL_USER_OBJECT === "true" ||
137
+ process.env.ECP_PASS_FULL_USER_OBJECT === "true");
138
+ }
139
+ function getPublicTokenEnv() {
140
+ return (process.env.NEXT_PUBLIC_ECP_AUTH_PUBLIC_TOKEN ||
141
+ process.env.VITE_ECP_AUTH_PUBLIC_TOKEN ||
142
+ process.env.ECP_AUTH_PUBLIC_TOKEN ||
143
+ "");
144
+ }
145
+ function getPrivateTokenEnv() {
146
+ var _a;
147
+ return (_a = process.env.ECP_AUTH_PRIVATE_TOKEN) !== null && _a !== void 0 ? _a : "";
148
+ }
@@ -0,0 +1 @@
1
+ export declare function setCookie(value?: string): Promise<void>;
@@ -0,0 +1,29 @@
1
+ "use server";
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.setCookie = setCookie;
5
+ async function setCookie(value) {
6
+ try {
7
+ // Dynamically import to avoid bundling next in non-Next environments
8
+ const { cookies } = require("next/headers");
9
+ const cookieStore = await cookies();
10
+ if (!value) {
11
+ cookieStore.delete("_ecp_auth_token");
12
+ }
13
+ else {
14
+ cookieStore.set("_ecp_auth_token", value, {
15
+ httpOnly: true,
16
+ sameSite: "lax",
17
+ secure: process.env.NODE_ENV === "production",
18
+ expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
19
+ path: "/",
20
+ });
21
+ }
22
+ }
23
+ catch (error) {
24
+ // In non-Next environments, this silently fails (as intended)
25
+ if (process.env.NODE_ENV === "development") {
26
+ console.warn("[setCookie] Could not set cookie:", error);
27
+ }
28
+ }
29
+ }
@@ -0,0 +1,7 @@
1
+ export * from "./toggle-2fa";
2
+ export * from "./reset-password";
3
+ export * from "./users-accounts";
4
+ export * from "./cookie";
5
+ export * from "./server-session";
6
+ export * from "./server-token";
7
+ export * from "./role";
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./toggle-2fa"), exports);
18
+ __exportStar(require("./reset-password"), exports);
19
+ __exportStar(require("./users-accounts"), exports);
20
+ __exportStar(require("./cookie"), exports);
21
+ __exportStar(require("./server-session"), exports);
22
+ __exportStar(require("./server-token"), exports);
23
+ __exportStar(require("./role"), exports);
@@ -0,0 +1,3 @@
1
+ import { ResetPasswordWithToken, GeneralResponse, ResetPasswordWithUserId, FormResponse } from "../types";
2
+ export declare function resetPassword(data: ResetPasswordWithToken): Promise<GeneralResponse>;
3
+ export declare function changePassword(data: ResetPasswordWithUserId): Promise<FormResponse>;
@@ -0,0 +1,41 @@
1
+ "use server";
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.resetPassword = resetPassword;
5
+ exports.changePassword = changePassword;
6
+ const utils_1 = require("../lib/utils");
7
+ const api_1 = require("../lib/api");
8
+ const error_1 = require("../lib/error");
9
+ async function resetPassword(data) {
10
+ try {
11
+ const res = await (0, api_1.apiFactory)("/reset-password", {
12
+ body: { ...data },
13
+ });
14
+ return res;
15
+ }
16
+ catch (error) {
17
+ if ((0, error_1.isAPIError)(error)) {
18
+ return error.data;
19
+ }
20
+ return { message: (0, utils_1.parseMessage)(error.message), status: "failure" };
21
+ }
22
+ }
23
+ async function changePassword(data) {
24
+ try {
25
+ const res = await (0, api_1.apiFactory)("/change-password", {
26
+ method: "PUT",
27
+ body: {
28
+ password: data.password,
29
+ confirmPassword: data.confirmPassword,
30
+ selectedUserId: data.userId,
31
+ },
32
+ });
33
+ return res;
34
+ }
35
+ catch (error) {
36
+ if ((0, error_1.isAPIError)(error)) {
37
+ return error.data;
38
+ }
39
+ return { message: (0, utils_1.parseMessage)(error.message), status: "failure" };
40
+ }
41
+ }
@@ -0,0 +1,7 @@
1
+ import { Role, NewRole, FormResponse } from "../types";
2
+ export declare function getRoles(options?: {
3
+ groupId: string;
4
+ }): Promise<Role[]>;
5
+ export declare function addRole(role: NewRole): Promise<FormResponse<Role>>;
6
+ export declare function updateRole(roleId: string, role: NewRole): Promise<FormResponse<Role>>;
7
+ export declare function assignPermission(roleId: string, permissions: string[]): Promise<FormResponse<Role>>;
@@ -0,0 +1,63 @@
1
+ "use server";
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.getRoles = getRoles;
5
+ exports.addRole = addRole;
6
+ exports.updateRole = updateRole;
7
+ exports.assignPermission = assignPermission;
8
+ const api_1 = require("../lib/api");
9
+ const error_1 = require("../lib/error");
10
+ async function getRoles(options) {
11
+ try {
12
+ const res = await (0, api_1.apiFactory)(`/roles?groupId=${(options === null || options === void 0 ? void 0 : options.groupId) || ""}`, {
13
+ method: "GET",
14
+ });
15
+ return res.data;
16
+ }
17
+ catch (error) {
18
+ console.log(error);
19
+ return [];
20
+ }
21
+ }
22
+ async function addRole(role) {
23
+ try {
24
+ const res = await (0, api_1.apiFactory)("/roles", { method: "POST", body: role });
25
+ return res;
26
+ }
27
+ catch (error) {
28
+ if ((0, error_1.isAPIError)(error)) {
29
+ return error.data;
30
+ }
31
+ return { message: error.message, status: "failure" };
32
+ }
33
+ }
34
+ async function updateRole(roleId, role) {
35
+ try {
36
+ const res = await (0, api_1.apiFactory)(`/roles/${roleId}`, {
37
+ method: "PUT",
38
+ body: role,
39
+ });
40
+ return res;
41
+ }
42
+ catch (error) {
43
+ if ((0, error_1.isAPIError)(error)) {
44
+ return error.data;
45
+ }
46
+ return { message: error.message, status: "failure" };
47
+ }
48
+ }
49
+ async function assignPermission(roleId, permissions) {
50
+ try {
51
+ const res = await (0, api_1.apiFactory)(`/assign-permissions/${roleId}`, {
52
+ method: "PUT",
53
+ body: { permissions },
54
+ });
55
+ return res;
56
+ }
57
+ catch (error) {
58
+ if ((0, error_1.isAPIError)(error)) {
59
+ return error.data;
60
+ }
61
+ return { message: error.message, status: "failure" };
62
+ }
63
+ }
@@ -0,0 +1,8 @@
1
+ import { User } from "../types";
2
+ export declare function getServerSession(): Promise<(User & {
3
+ meta: {
4
+ sessionId: string;
5
+ exp: number;
6
+ iat: number;
7
+ };
8
+ }) | null>;
@@ -0,0 +1,31 @@
1
+ "use server";
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.getServerSession = getServerSession;
5
+ const utils_1 = require("../lib/utils");
6
+ const server_token_1 = require("./server-token");
7
+ async function getServerSession() {
8
+ try {
9
+ const token = await (0, server_token_1.getServerToken)();
10
+ if (!token)
11
+ return null;
12
+ const session = (0, utils_1.parseJwt)(token);
13
+ return {
14
+ ...session.user,
15
+ meta: {
16
+ sessionId: session.sessionId,
17
+ exp: session.exp,
18
+ iat: session.iat,
19
+ },
20
+ };
21
+ // const res = await apiFactory("/me-server", {
22
+ // method: "GET",
23
+ // cache: "no-store",
24
+ // });
25
+ // return res.user;
26
+ }
27
+ catch (error) {
28
+ console.log(error);
29
+ return null;
30
+ }
31
+ }
@@ -0,0 +1,4 @@
1
+ import { NextAppCookies } from "../types";
2
+ export declare function getServerToken(source?: Request | {
3
+ cookies: NextAppCookies | Record<string, string>;
4
+ }): Promise<string | undefined>;
@@ -0,0 +1,39 @@
1
+ "use server";
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.getServerToken = getServerToken;
5
+ async function getServerToken(source) {
6
+ var _a, _b;
7
+ const cookieName = "_ecp_auth_token";
8
+ // Case 1: No source, assume App Router (next/headers)
9
+ if (!source) {
10
+ try {
11
+ // Dynamically import to avoid bundling next in non-Next environments
12
+ const { cookies } = require("next/headers");
13
+ const cookieStore = await cookies();
14
+ return (_a = cookieStore.get(cookieName)) === null || _a === void 0 ? void 0 : _a.value;
15
+ }
16
+ catch {
17
+ return undefined;
18
+ }
19
+ }
20
+ // Case 2: Middleware or Edge API
21
+ if (source instanceof Request) {
22
+ const cookieHeader = source.headers.get("cookie") || "";
23
+ const cookies = Object.fromEntries(cookieHeader.split("; ").map((c) => {
24
+ const [k, ...v] = c.split("=");
25
+ return [k, v.join("=")];
26
+ }));
27
+ return cookies[cookieName];
28
+ }
29
+ // Case 3: API routes or custom cookie objects
30
+ const cookieSource = source.cookies;
31
+ if (typeof (cookieSource === null || cookieSource === void 0 ? void 0 : cookieSource.get) === "function") {
32
+ return (_b = cookieSource.get(cookieName)) === null || _b === void 0 ? void 0 : _b.value; // next/headers-style
33
+ }
34
+ if (typeof cookieSource === "object") {
35
+ //@ts-ignore
36
+ return cookieSource[cookieName]; // plain object
37
+ }
38
+ return undefined;
39
+ }
@@ -0,0 +1,4 @@
1
+ import { GeneralResponse } from "../types";
2
+ export declare function toggle2FA(userId?: string): Promise<GeneralResponse>;
3
+ export declare function enable2FA(userId?: string): Promise<GeneralResponse>;
4
+ export declare function disable2FA(userId?: string): Promise<GeneralResponse>;
@@ -0,0 +1,33 @@
1
+ "use server";
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.toggle2FA = toggle2FA;
5
+ exports.enable2FA = enable2FA;
6
+ exports.disable2FA = disable2FA;
7
+ const utils_1 = require("../lib/utils");
8
+ const api_1 = require("../lib/api");
9
+ const error_1 = require("../lib/error");
10
+ async function toggle2FA(userId) {
11
+ try {
12
+ const res = await (0, api_1.apiFactory)("/2fa", {
13
+ method: "PUT",
14
+ body: {
15
+ selectedUserId: userId,
16
+ },
17
+ adminCall: !!userId,
18
+ });
19
+ return res;
20
+ }
21
+ catch (error) {
22
+ if ((0, error_1.isAPIError)(error)) {
23
+ return error.data;
24
+ }
25
+ return { message: (0, utils_1.parseMessage)(error.message), status: "failure" };
26
+ }
27
+ }
28
+ async function enable2FA(userId) {
29
+ return await toggle2FA(userId);
30
+ }
31
+ async function disable2FA(userId) {
32
+ return await toggle2FA(userId);
33
+ }
@@ -0,0 +1,11 @@
1
+ import { VerifyUser, VerifyUserResponse, Signup, UpdateUser, User, GeneralResponse, FormResponse } from "../types";
2
+ export declare function verifyUser(data: VerifyUser): Promise<VerifyUserResponse>;
3
+ export declare function registerUser(data: Signup, options?: {
4
+ isSuperAdmin?: boolean;
5
+ }): Promise<FormResponse<User>>;
6
+ export declare function updateUser(userId: string, data: UpdateUser): Promise<FormResponse<User>>;
7
+ export declare function getUsers(options?: {
8
+ groupId: string;
9
+ }): Promise<User[]>;
10
+ export declare function getUserById(userId: string): Promise<User | null>;
11
+ export declare function toggleUserStatus(userId: string): Promise<GeneralResponse>;
@@ -0,0 +1,94 @@
1
+ "use server";
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.verifyUser = verifyUser;
5
+ exports.registerUser = registerUser;
6
+ exports.updateUser = updateUser;
7
+ exports.getUsers = getUsers;
8
+ exports.getUserById = getUserById;
9
+ exports.toggleUserStatus = toggleUserStatus;
10
+ const utils_1 = require("../lib/utils");
11
+ const api_1 = require("../lib/api");
12
+ const error_1 = require("../lib/error");
13
+ async function verifyUser(data) {
14
+ try {
15
+ const res = await (0, api_1.apiFactory)("/users/verify", {
16
+ body: { actions: data },
17
+ });
18
+ return res;
19
+ }
20
+ catch (error) {
21
+ return error;
22
+ }
23
+ }
24
+ async function registerUser(data, options) {
25
+ try {
26
+ const res = await (0, api_1.apiFactory)(`/signup?isSuperAdmin=${(options === null || options === void 0 ? void 0 : options.isSuperAdmin) || false}`, {
27
+ body: { ...data },
28
+ });
29
+ return res;
30
+ }
31
+ catch (error) {
32
+ if ((0, error_1.isAPIError)(error)) {
33
+ return error.data;
34
+ }
35
+ return {
36
+ message: (0, utils_1.parseMessage)(error.message),
37
+ status: "failure",
38
+ };
39
+ }
40
+ }
41
+ async function updateUser(userId, data) {
42
+ try {
43
+ const res = await (0, api_1.apiFactory)(`/update-user/${userId}`, {
44
+ body: { ...data },
45
+ method: "PUT",
46
+ });
47
+ return res;
48
+ }
49
+ catch (error) {
50
+ if ((0, error_1.isAPIError)(error)) {
51
+ return error.data;
52
+ }
53
+ return { message: (0, utils_1.parseMessage)(error.message), status: "failure" };
54
+ }
55
+ }
56
+ async function getUsers(options) {
57
+ try {
58
+ const res = await (0, api_1.apiFactory)(`/users?groupId=${(options === null || options === void 0 ? void 0 : options.groupId) || ""}`, {
59
+ method: "GET",
60
+ });
61
+ return res.data;
62
+ }
63
+ catch (err) {
64
+ console.log(err.message);
65
+ return [];
66
+ }
67
+ }
68
+ async function getUserById(userId) {
69
+ try {
70
+ const res = await (0, api_1.apiFactory)(`/users/${userId}`, { method: "GET" });
71
+ return res.user;
72
+ }
73
+ catch (err) {
74
+ console.log(err.message);
75
+ return null;
76
+ }
77
+ }
78
+ async function toggleUserStatus(userId) {
79
+ try {
80
+ const res = await (0, api_1.apiFactory)("/toggle-user-status", {
81
+ method: "PUT",
82
+ body: {
83
+ selectedUserId: userId,
84
+ },
85
+ });
86
+ return res;
87
+ }
88
+ catch (error) {
89
+ if ((0, error_1.isAPIError)(error)) {
90
+ return error.data;
91
+ }
92
+ return { message: (0, utils_1.parseMessage)(error.message), status: "failure" };
93
+ }
94
+ }