@vivinkv28/strapi-2fa-admin-plugin 0.1.12 → 0.1.14

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.
@@ -74,6 +74,26 @@ const runtimeRequire = createRequire(__filename);
74
74
  var strapiSessionAuth = runtimeRequire(resolveSessionAuthPath());
75
75
  const sessionAuth$1 = strapiSessionAuth;
76
76
  const getService = () => strapi.plugin("admin-2fa").service("auth");
77
+ const APPLICATION_ERROR_STATUS = {
78
+ ApplicationError: 400,
79
+ ValidationError: 400,
80
+ UnauthorizedError: 400,
81
+ ForbiddenError: 400,
82
+ NotFoundError: 404,
83
+ PayloadTooLargeError: 413,
84
+ RateLimitError: 429,
85
+ NotImplementedError: 501
86
+ };
87
+ const deriveApplicationErrorStatus = (error2) => {
88
+ if (typeof error2?.status === "number" && error2.status >= 400 && error2.status < 500) {
89
+ return error2.status;
90
+ }
91
+ const message = typeof error2?.message === "string" ? error2.message.toLowerCase() : "";
92
+ if (message.includes("session not found") || message.includes("please log in again") || message.includes("otp expired") || message.includes("expired otp")) {
93
+ return 409;
94
+ }
95
+ return APPLICATION_ERROR_STATUS[error2?.name] ?? 400;
96
+ };
77
97
  const setRefreshCookie = (ctx, refreshToken, cookieOptions) => {
78
98
  ctx.cookies.set(sessionAuth$1.REFRESH_COOKIE_NAME, refreshToken, cookieOptions);
79
99
  };
@@ -87,32 +107,69 @@ const getClientIp = (ctx) => {
87
107
  }
88
108
  return String(ctx.request.ip ?? ctx.ip ?? "").trim();
89
109
  };
110
+ const sendApplicationError = (ctx, error2) => {
111
+ const derivedStatus = deriveApplicationErrorStatus(error2);
112
+ ctx.status = derivedStatus;
113
+ ctx.body = {
114
+ data: null,
115
+ error: {
116
+ status: derivedStatus,
117
+ name: error2?.name ?? "ApplicationError",
118
+ message: error2?.message ?? "Request failed",
119
+ details: error2?.details ?? {}
120
+ }
121
+ };
122
+ };
90
123
  var auth$3 = {
91
124
  async login(ctx) {
92
- const result = await getService().createChallenge(ctx.request.body ?? {}, {
93
- clientIp: getClientIp(ctx)
94
- });
95
- ctx.body = { data: result };
125
+ try {
126
+ const result = await getService().createChallenge(ctx.request.body ?? {}, {
127
+ clientIp: getClientIp(ctx)
128
+ });
129
+ ctx.body = { data: result };
130
+ } catch (error2) {
131
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
132
+ sendApplicationError(ctx, error2);
133
+ return;
134
+ }
135
+ throw error2;
136
+ }
96
137
  },
97
138
  async resend(ctx) {
98
- const result = await getService().resendChallenge(ctx.request.body ?? {}, {
99
- clientIp: getClientIp(ctx)
100
- });
101
- ctx.body = { data: result };
139
+ try {
140
+ const result = await getService().resendChallenge(ctx.request.body ?? {}, {
141
+ clientIp: getClientIp(ctx)
142
+ });
143
+ ctx.body = { data: result };
144
+ } catch (error2) {
145
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
146
+ sendApplicationError(ctx, error2);
147
+ return;
148
+ }
149
+ throw error2;
150
+ }
102
151
  },
103
152
  async verify(ctx) {
104
- const result = await getService().verifyChallenge(ctx.request.body ?? {}, {
105
- secureRequest: ctx.request.secure,
106
- clientIp: getClientIp(ctx)
107
- });
108
- setRefreshCookie(ctx, result.refreshToken, result.cookieOptions);
109
- ctx.body = {
110
- data: {
111
- token: result.accessToken,
112
- accessToken: result.accessToken,
113
- user: result.user
153
+ try {
154
+ const result = await getService().verifyChallenge(ctx.request.body ?? {}, {
155
+ secureRequest: ctx.request.secure,
156
+ clientIp: getClientIp(ctx)
157
+ });
158
+ setRefreshCookie(ctx, result.refreshToken, result.cookieOptions);
159
+ ctx.body = {
160
+ data: {
161
+ token: result.accessToken,
162
+ accessToken: result.accessToken,
163
+ user: result.user
164
+ }
165
+ };
166
+ } catch (error2) {
167
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
168
+ sendApplicationError(ctx, error2);
169
+ return;
114
170
  }
115
- };
171
+ throw error2;
172
+ }
116
173
  }
117
174
  };
118
175
  const auth$2 = auth$3;
@@ -60,6 +60,26 @@ const runtimeRequire = createRequire(__filename);
60
60
  var strapiSessionAuth = runtimeRequire(resolveSessionAuthPath());
61
61
  const sessionAuth$1 = strapiSessionAuth;
62
62
  const getService = () => strapi.plugin("admin-2fa").service("auth");
63
+ const APPLICATION_ERROR_STATUS = {
64
+ ApplicationError: 400,
65
+ ValidationError: 400,
66
+ UnauthorizedError: 400,
67
+ ForbiddenError: 400,
68
+ NotFoundError: 404,
69
+ PayloadTooLargeError: 413,
70
+ RateLimitError: 429,
71
+ NotImplementedError: 501
72
+ };
73
+ const deriveApplicationErrorStatus = (error2) => {
74
+ if (typeof error2?.status === "number" && error2.status >= 400 && error2.status < 500) {
75
+ return error2.status;
76
+ }
77
+ const message = typeof error2?.message === "string" ? error2.message.toLowerCase() : "";
78
+ if (message.includes("session not found") || message.includes("please log in again") || message.includes("otp expired") || message.includes("expired otp")) {
79
+ return 409;
80
+ }
81
+ return APPLICATION_ERROR_STATUS[error2?.name] ?? 400;
82
+ };
63
83
  const setRefreshCookie = (ctx, refreshToken, cookieOptions) => {
64
84
  ctx.cookies.set(sessionAuth$1.REFRESH_COOKIE_NAME, refreshToken, cookieOptions);
65
85
  };
@@ -73,32 +93,69 @@ const getClientIp = (ctx) => {
73
93
  }
74
94
  return String(ctx.request.ip ?? ctx.ip ?? "").trim();
75
95
  };
96
+ const sendApplicationError = (ctx, error2) => {
97
+ const derivedStatus = deriveApplicationErrorStatus(error2);
98
+ ctx.status = derivedStatus;
99
+ ctx.body = {
100
+ data: null,
101
+ error: {
102
+ status: derivedStatus,
103
+ name: error2?.name ?? "ApplicationError",
104
+ message: error2?.message ?? "Request failed",
105
+ details: error2?.details ?? {}
106
+ }
107
+ };
108
+ };
76
109
  var auth$3 = {
77
110
  async login(ctx) {
78
- const result = await getService().createChallenge(ctx.request.body ?? {}, {
79
- clientIp: getClientIp(ctx)
80
- });
81
- ctx.body = { data: result };
111
+ try {
112
+ const result = await getService().createChallenge(ctx.request.body ?? {}, {
113
+ clientIp: getClientIp(ctx)
114
+ });
115
+ ctx.body = { data: result };
116
+ } catch (error2) {
117
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
118
+ sendApplicationError(ctx, error2);
119
+ return;
120
+ }
121
+ throw error2;
122
+ }
82
123
  },
83
124
  async resend(ctx) {
84
- const result = await getService().resendChallenge(ctx.request.body ?? {}, {
85
- clientIp: getClientIp(ctx)
86
- });
87
- ctx.body = { data: result };
125
+ try {
126
+ const result = await getService().resendChallenge(ctx.request.body ?? {}, {
127
+ clientIp: getClientIp(ctx)
128
+ });
129
+ ctx.body = { data: result };
130
+ } catch (error2) {
131
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
132
+ sendApplicationError(ctx, error2);
133
+ return;
134
+ }
135
+ throw error2;
136
+ }
88
137
  },
89
138
  async verify(ctx) {
90
- const result = await getService().verifyChallenge(ctx.request.body ?? {}, {
91
- secureRequest: ctx.request.secure,
92
- clientIp: getClientIp(ctx)
93
- });
94
- setRefreshCookie(ctx, result.refreshToken, result.cookieOptions);
95
- ctx.body = {
96
- data: {
97
- token: result.accessToken,
98
- accessToken: result.accessToken,
99
- user: result.user
139
+ try {
140
+ const result = await getService().verifyChallenge(ctx.request.body ?? {}, {
141
+ secureRequest: ctx.request.secure,
142
+ clientIp: getClientIp(ctx)
143
+ });
144
+ setRefreshCookie(ctx, result.refreshToken, result.cookieOptions);
145
+ ctx.body = {
146
+ data: {
147
+ token: result.accessToken,
148
+ accessToken: result.accessToken,
149
+ user: result.user
150
+ }
151
+ };
152
+ } catch (error2) {
153
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
154
+ sendApplicationError(ctx, error2);
155
+ return;
100
156
  }
101
- };
157
+ throw error2;
158
+ }
102
159
  }
103
160
  };
104
161
  const auth$2 = auth$3;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vivinkv28/strapi-2fa-admin-plugin",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "Reusable Strapi admin 2FA plugin",
5
5
  "type": "commonjs",
6
6
  "keywords": [