powr-sdk-api 2.3.6 → 2.3.7
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/dist/middleware/jwtToken.js +6 -10
- package/dist/routes/auth.js +55 -3
- package/package.json +67 -67
|
@@ -5,10 +5,11 @@ const {
|
|
|
5
5
|
ObjectId
|
|
6
6
|
} = require('mongodb');
|
|
7
7
|
const config = require('../config');
|
|
8
|
-
const generateJWTToken = user => {
|
|
8
|
+
const generateJWTToken = (user, profile = null) => {
|
|
9
9
|
return jwt.sign({
|
|
10
10
|
userId: user._id,
|
|
11
|
-
fullName: user.fullName
|
|
11
|
+
fullName: user.fullName,
|
|
12
|
+
access: profile === null || profile === void 0 ? void 0 : profile.access
|
|
12
13
|
}, config.jwtToken, {
|
|
13
14
|
expiresIn: '24h'
|
|
14
15
|
});
|
|
@@ -32,16 +33,11 @@ const verifyToken = async (req, res, next) => {
|
|
|
32
33
|
// Verify JWT token
|
|
33
34
|
const decoded = jwt.verify(token, config.jwtToken);
|
|
34
35
|
console.log("JWT Decoded user data:", JSON.stringify(decoded, null, 2));
|
|
35
|
-
|
|
36
|
-
// Attach user info to request object
|
|
37
36
|
req.user = {
|
|
38
|
-
powrId:
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
powrId: decoded.userId,
|
|
38
|
+
fullName: decoded.fullName,
|
|
39
|
+
access: decoded.access
|
|
41
40
|
};
|
|
42
|
-
console.log("Authenticated user:", {
|
|
43
|
-
powrId: req.user.powrId
|
|
44
|
-
});
|
|
45
41
|
next();
|
|
46
42
|
} catch (error) {
|
|
47
43
|
console.error("Error in auth middleware:", error);
|
package/dist/routes/auth.js
CHANGED
|
@@ -23,7 +23,8 @@ router.post("/register", async (req, res) => {
|
|
|
23
23
|
fullName,
|
|
24
24
|
username,
|
|
25
25
|
phoneOrEmail,
|
|
26
|
-
password
|
|
26
|
+
password,
|
|
27
|
+
projectId
|
|
27
28
|
} = req.body;
|
|
28
29
|
try {
|
|
29
30
|
if (!(username || phoneOrEmail)) {
|
|
@@ -44,6 +45,14 @@ router.post("/register", async (req, res) => {
|
|
|
44
45
|
message: "Full name is required"
|
|
45
46
|
});
|
|
46
47
|
}
|
|
48
|
+
|
|
49
|
+
// if (!projectId) {
|
|
50
|
+
// return res.status(400).json({
|
|
51
|
+
// success: false,
|
|
52
|
+
// message: "Project ID is required"
|
|
53
|
+
// });
|
|
54
|
+
// }
|
|
55
|
+
|
|
47
56
|
let q = username ? {
|
|
48
57
|
username: username
|
|
49
58
|
} : {
|
|
@@ -72,6 +81,15 @@ router.post("/register", async (req, res) => {
|
|
|
72
81
|
createdAt: new Date()
|
|
73
82
|
};
|
|
74
83
|
const result = await getDb().collection("users").insertOne(newUser);
|
|
84
|
+
if (projectId) {
|
|
85
|
+
const newProfile = {
|
|
86
|
+
userId: result.insertedId,
|
|
87
|
+
projectId: projectId,
|
|
88
|
+
createdAt: new Date(),
|
|
89
|
+
updatedAt: new Date()
|
|
90
|
+
};
|
|
91
|
+
await getDb().collection("profiles").insertOne(newProfile);
|
|
92
|
+
}
|
|
75
93
|
return res.status(201).json({
|
|
76
94
|
success: true,
|
|
77
95
|
message: "User registered successfully",
|
|
@@ -91,7 +109,8 @@ router.post("/login", async (req, res) => {
|
|
|
91
109
|
const {
|
|
92
110
|
username,
|
|
93
111
|
phoneOrEmail,
|
|
94
|
-
password
|
|
112
|
+
password,
|
|
113
|
+
projectId
|
|
95
114
|
} = req.body;
|
|
96
115
|
try {
|
|
97
116
|
if (!(username || phoneOrEmail)) {
|
|
@@ -131,7 +150,39 @@ router.post("/login", async (req, res) => {
|
|
|
131
150
|
message: "Invalid username or password."
|
|
132
151
|
});
|
|
133
152
|
}
|
|
134
|
-
|
|
153
|
+
|
|
154
|
+
// Fetch profile data if projectId is provided
|
|
155
|
+
let profile = null;
|
|
156
|
+
if (projectId) {
|
|
157
|
+
profile = await getDb().collection("profiles").findOne({
|
|
158
|
+
userId: user._id,
|
|
159
|
+
projectId: projectId
|
|
160
|
+
});
|
|
161
|
+
if (profile) {
|
|
162
|
+
// Update lastActiveAt
|
|
163
|
+
await getDb().collection("profiles").updateOne({
|
|
164
|
+
_id: profile._id
|
|
165
|
+
}, {
|
|
166
|
+
$set: {
|
|
167
|
+
lastActiveAt: new Date()
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
} else {
|
|
171
|
+
// Create profile if it doesn't exist - only essential fields
|
|
172
|
+
const newProfile = {
|
|
173
|
+
userId: user._id,
|
|
174
|
+
projectId: projectId,
|
|
175
|
+
createdAt: new Date(),
|
|
176
|
+
updatedAt: new Date()
|
|
177
|
+
};
|
|
178
|
+
const profileResult = await getDb().collection("profiles").insertOne(newProfile);
|
|
179
|
+
profile = {
|
|
180
|
+
_id: profileResult.insertedId,
|
|
181
|
+
...newProfile
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const token = generateJWTToken(user, profile);
|
|
135
186
|
const {
|
|
136
187
|
password: _,
|
|
137
188
|
...userWithoutPassword
|
|
@@ -140,6 +191,7 @@ router.post("/login", async (req, res) => {
|
|
|
140
191
|
success: true,
|
|
141
192
|
message: "Login successful",
|
|
142
193
|
user: userWithoutPassword,
|
|
194
|
+
profile: profile,
|
|
143
195
|
token: token
|
|
144
196
|
});
|
|
145
197
|
} catch (error) {
|
package/package.json
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "powr-sdk-api",
|
|
3
|
-
"version": "2.3.
|
|
4
|
-
"description": "Shared API core library for PowrStack projects",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"files": [
|
|
8
|
-
"dist",
|
|
9
|
-
"README.md"
|
|
10
|
-
],
|
|
11
|
-
"scripts": {
|
|
12
|
-
"test": "jest --passWithNoTests",
|
|
13
|
-
"lint": "eslint .",
|
|
14
|
-
"build": "babel src -d dist",
|
|
15
|
-
"prepare": "npm run build",
|
|
16
|
-
"prepublishOnly": "npm run test"
|
|
17
|
-
},
|
|
18
|
-
"keywords": [
|
|
19
|
-
"api",
|
|
20
|
-
"express",
|
|
21
|
-
"middleware",
|
|
22
|
-
"error-handling",
|
|
23
|
-
"response-formatting",
|
|
24
|
-
"logging",
|
|
25
|
-
"swagger",
|
|
26
|
-
"documentation"
|
|
27
|
-
],
|
|
28
|
-
"author": "PowrStack",
|
|
29
|
-
"license": "ISC",
|
|
30
|
-
"repository": {
|
|
31
|
-
"type": "git",
|
|
32
|
-
"url": "git+https://github.com/powrstack/powr-sdk-api.git"
|
|
33
|
-
},
|
|
34
|
-
"bugs": {
|
|
35
|
-
"url": "https://github.com/powrstack/powr-sdk-api/issues"
|
|
36
|
-
},
|
|
37
|
-
"homepage": "https://github.com/powrstack/powr-sdk-api#readme",
|
|
38
|
-
"dependencies": {
|
|
39
|
-
"@aws-sdk/client-s3": "^3.787.0",
|
|
40
|
-
"@google-cloud/storage": "^7.16.0",
|
|
41
|
-
"express": "^4.18.2",
|
|
42
|
-
"jsonwebtoken": "^9.0.2",
|
|
43
|
-
"swagger-jsdoc": "^6.2.8",
|
|
44
|
-
"swagger-ui-express": "^5.0.0",
|
|
45
|
-
"winston": "^3.17.0",
|
|
46
|
-
"mongodb": "^6.3.0",
|
|
47
|
-
"multer": "^1.4.5-lts.1",
|
|
48
|
-
"bcrypt": "^5.1.1"
|
|
49
|
-
},
|
|
50
|
-
"devDependencies": {
|
|
51
|
-
"@babel/cli": "^7.23.9",
|
|
52
|
-
"@babel/core": "^7.24.0",
|
|
53
|
-
"@babel/preset-env": "^7.24.0",
|
|
54
|
-
"@types/express": "^4.17.21",
|
|
55
|
-
"@types/swagger-jsdoc": "^6.0.4",
|
|
56
|
-
"@types/swagger-ui-express": "^4.1.6",
|
|
57
|
-
"eslint": "^8.57.0",
|
|
58
|
-
"jest": "^29.7.0",
|
|
59
|
-
"typescript": "^5.3.3"
|
|
60
|
-
},
|
|
61
|
-
"peerDependencies": {
|
|
62
|
-
"express": "^4.18.2"
|
|
63
|
-
},
|
|
64
|
-
"engines": {
|
|
65
|
-
"node": ">=14.0.0"
|
|
66
|
-
}
|
|
67
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "powr-sdk-api",
|
|
3
|
+
"version": "2.3.7",
|
|
4
|
+
"description": "Shared API core library for PowrStack projects",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "jest --passWithNoTests",
|
|
13
|
+
"lint": "eslint .",
|
|
14
|
+
"build": "babel src -d dist",
|
|
15
|
+
"prepare": "npm run build",
|
|
16
|
+
"prepublishOnly": "npm run test"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"api",
|
|
20
|
+
"express",
|
|
21
|
+
"middleware",
|
|
22
|
+
"error-handling",
|
|
23
|
+
"response-formatting",
|
|
24
|
+
"logging",
|
|
25
|
+
"swagger",
|
|
26
|
+
"documentation"
|
|
27
|
+
],
|
|
28
|
+
"author": "PowrStack",
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/powrstack/powr-sdk-api.git"
|
|
33
|
+
},
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/powrstack/powr-sdk-api/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/powrstack/powr-sdk-api#readme",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@aws-sdk/client-s3": "^3.787.0",
|
|
40
|
+
"@google-cloud/storage": "^7.16.0",
|
|
41
|
+
"express": "^4.18.2",
|
|
42
|
+
"jsonwebtoken": "^9.0.2",
|
|
43
|
+
"swagger-jsdoc": "^6.2.8",
|
|
44
|
+
"swagger-ui-express": "^5.0.0",
|
|
45
|
+
"winston": "^3.17.0",
|
|
46
|
+
"mongodb": "^6.3.0",
|
|
47
|
+
"multer": "^1.4.5-lts.1",
|
|
48
|
+
"bcrypt": "^5.1.1"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@babel/cli": "^7.23.9",
|
|
52
|
+
"@babel/core": "^7.24.0",
|
|
53
|
+
"@babel/preset-env": "^7.24.0",
|
|
54
|
+
"@types/express": "^4.17.21",
|
|
55
|
+
"@types/swagger-jsdoc": "^6.0.4",
|
|
56
|
+
"@types/swagger-ui-express": "^4.1.6",
|
|
57
|
+
"eslint": "^8.57.0",
|
|
58
|
+
"jest": "^29.7.0",
|
|
59
|
+
"typescript": "^5.3.3"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"express": "^4.18.2"
|
|
63
|
+
},
|
|
64
|
+
"engines": {
|
|
65
|
+
"node": ">=14.0.0"
|
|
66
|
+
}
|
|
67
|
+
}
|