propro-utils 1.7.26 → 1.7.28

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.
@@ -42,6 +42,7 @@ const authValidation = (requiredPermissions = []) => {
42
42
  req.headers.authorization?.split(' ')[1];
43
43
 
44
44
  if (!accessToken) {
45
+ console.log('Access token is required');
45
46
  return res.status(403).json({ error: 'Access token is required' });
46
47
  }
47
48
 
@@ -65,15 +66,21 @@ const authValidation = (requiredPermissions = []) => {
65
66
  );
66
67
 
67
68
  if (!validPermissions) {
69
+ console.log('Invalid permissions');
68
70
  return res.status(403).json({ error: 'Invalid permissions' });
69
71
  }
70
72
 
71
73
  req.account = accountId;
72
74
 
73
- const user = await checkIfUserExists(accountId);
74
- if (!user) {
75
- return res.status(403).json({ error: 'User not found' });
75
+ let user = null;
76
+ try {
77
+ user = await checkIfUserExists(account.accountId);
78
+ if (!user) throw new Error('User not found');
79
+ } catch (error) {
80
+ console.log('User not found 1');
81
+ return res.status(403).json({error: error?.message || 'User not found'});
76
82
  }
83
+
77
84
  req.user = user.id;
78
85
  next();
79
86
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.7.26",
3
+ "version": "1.7.28",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "private": false,
@@ -93,9 +93,15 @@ class AuthMiddleware {
93
93
  const response = await this.proxyToAuthServer(req, `/api/v1/auth/login`);
94
94
 
95
95
  const { account, tokens } = response.data;
96
- console.log('account:', account);
97
- const user = await checkIfUserExists(account.accountId);
98
- console.log('user:', user);
96
+
97
+ let user = null;
98
+ try {
99
+ user = await checkIfUserExists(account.accountId);
100
+ if (!user) throw new Error('User not found');
101
+ } catch (error) {
102
+ console.log('User not found 2');
103
+ return res.status(403).json({error: error?.message || 'User not found'});
104
+ }
99
105
 
100
106
  if (returnTokens === 'true') {
101
107
  res.status(response.status).json({ account, user, tokens });
@@ -160,10 +166,13 @@ class AuthMiddleware {
160
166
  this.options.redirectUri
161
167
  );
162
168
 
163
- const user = await checkIfUserExists(account.accountId);
164
-
165
- if (!user) {
166
- throw new Error('User not found');
169
+ let user = null;
170
+ try {
171
+ user = await checkIfUserExists(account.accountId);
172
+ if (!user) throw new Error('User not found');
173
+ } catch (error) {
174
+ console.log('User not found 3');
175
+ return res.status(403).json({error: error?.message || 'User not found'});
167
176
  }
168
177
 
169
178
  await setAuthCookies(res, tokens, account, user, this.options.appUrl);
@@ -96,6 +96,7 @@ const VerifyAccount = requiredPermissions => {
96
96
  try {
97
97
  const decoded = jwt.verify(accessToken, process.env.JWT_SECRET);
98
98
  if (!isValid(decoded, requiredPermissions)) {
99
+ console.log('Invalid permissions 1');
99
100
  return res.status(403).json({ error: 'Invalid permissions' });
100
101
  }
101
102
  tokenCache.set(accessToken, decoded);
@@ -113,14 +114,15 @@ const VerifyAccount = requiredPermissions => {
113
114
  );
114
115
 
115
116
  if (!isValid(userResponse.data, requiredPermissions)) {
116
- return res.status(403).json({ error: 'Invalid permissions' });
117
+ console.log('Invalid permissions 2');
118
+ return res.status(403).json({ error: 'Invalid permissions' });
117
119
  }
118
120
 
119
121
  tokenCache.set(accessToken, userResponse.data);
120
122
  req.user = userResponse.data;
121
123
  return next();
122
124
  } catch (networkError) {
123
- return res.status(500).json({ error: 'Error validating token' });
125
+ return res.status(401).json({ error: 'Error validating token' });
124
126
  }
125
127
  }
126
128
  };