propro-utils 1.4.19 → 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.19",
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
 
@@ -151,16 +151,7 @@ function proproAuthMiddleware(options = {}, userSchema) {
151
151
 
152
152
  console.log('redirectUrl', redirectUrl);
153
153
 
154
- let urlToRedirect;
155
-
156
- if (!redirectUrl.startsWith('http://') && !redirectUrl.startsWith('https://')) {
157
- if (redirectUrl.includes('localhost')) {
158
- urlToRedirect = `http://${redirectUrl}`;
159
- } else {
160
- urlToRedirect = `https://${redirectUrl}`;
161
- }
162
- }
163
-
154
+ const urlToRedirect = formatRedirectUrl(clientUrl);
164
155
 
165
156
  console.log('urlToRedirect', urlToRedirect);
166
157
 
@@ -185,15 +176,9 @@ function proproAuthMiddleware(options = {}, userSchema) {
185
176
  function constructRedirectUrl(clientUrl, appName, clientId, redirectUri) {
186
177
  console.log('constructRedirectUrl', clientUrl, appName, clientId, redirectUri);
187
178
 
188
- if (!clientUrl.startsWith('http://') && !clientUrl.startsWith('https://')) {
189
- if (clientUrl.includes('localhost')) {
190
- clientUrl = `http://${clientUrl}`;
191
- } else {
192
- clientUrl = `https://${clientUrl}`;
193
- }
194
- }
179
+ const urlToRedirect = formatRedirectUrl(clientUrl)
195
180
 
196
- return `${clientUrl}/signin?response_type=code&appName=${appName}&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}`;
181
+ return `${urlToRedirect}/signin?response_type=code&appName=${appName}&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}`;
197
182
  }
198
183
 
199
184
 
@@ -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
  };