propro-utils 1.4.20 → 1.4.21

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.4.20",
3
+ "version": "1.4.21",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,5 +1,5 @@
1
1
  require('dotenv').config();
2
- const {exchangeToken} = require('./middleware/verifyToken');
2
+ const {exchangeToken, formatRedirectUrl} = require('./middleware/verifyToken');
3
3
  const {checkIfUserExists} = require('../../middlewares/account_info');
4
4
  const {post} = require('axios');
5
5
 
@@ -181,27 +181,5 @@ function constructRedirectUrl(clientUrl, appName, clientId, redirectUri) {
181
181
  return `${urlToRedirect}/signin?response_type=code&appName=${appName}&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}`;
182
182
  }
183
183
 
184
- /**
185
- * Formats the redirect URL by adding 'http://' or 'https://' if necessary.
186
- *
187
- * @param {string} redirectUrl - The URL to be redirected.
188
- * @return {string} The formatted redirect URL.
189
- */
190
- function formatRedirectUrl(redirectUrl) {
191
- let urlToRedirect;
192
-
193
- if (!redirectUrl.startsWith('http://') && !redirectUrl.startsWith('https://')) {
194
- if (redirectUrl.includes('localhost')) {
195
- urlToRedirect = `http://${redirectUrl}`;
196
- } else {
197
- urlToRedirect = `https://${redirectUrl}`;
198
- }
199
- } else {
200
- urlToRedirect = redirectUrl;
201
- }
202
-
203
- return urlToRedirect;
204
- }
205
-
206
184
 
207
185
  module.exports = proproAuthMiddleware;
@@ -49,8 +49,9 @@ async function exchangeToken(
49
49
  redirectUri
50
50
  ) {
51
51
  try {
52
+ const formattedRedirectUri = formatRedirectUrl(authUrl)
52
53
  const response = await axios.post(
53
- authUrl + "/api/v1/auth/authorize",
54
+ formattedRedirectUri + "/api/v1/auth/authorize",
54
55
  {
55
56
  grantType: "authorization_code",
56
57
  code: code,
@@ -133,8 +134,31 @@ function isValid(decodedToken, requiredPermissions) {
133
134
  );
134
135
  }
135
136
 
137
+ /**
138
+ * Formats the redirect URL by adding 'http://' or 'https://' if necessary.
139
+ *
140
+ * @param {string} redirectUrl - The URL to be redirected.
141
+ * @return {string} The formatted redirect URL.
142
+ */
143
+ function formatRedirectUrl(redirectUrl) {
144
+ let urlToRedirect;
145
+
146
+ if (!redirectUrl.startsWith('http://') && !redirectUrl.startsWith('https://')) {
147
+ if (redirectUrl.includes('localhost')) {
148
+ urlToRedirect = `http://${redirectUrl}`;
149
+ } else {
150
+ urlToRedirect = `https://${redirectUrl}`;
151
+ }
152
+ } else {
153
+ urlToRedirect = redirectUrl;
154
+ }
155
+
156
+ return urlToRedirect;
157
+ }
158
+
136
159
  module.exports = {
137
160
  VerifyAccount,
138
161
  exchangeToken,
139
162
  verifyJWT,
163
+ formatRedirectUrl,
140
164
  };