powr-sdk-api 4.9.4 → 4.9.6

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.
@@ -214,13 +214,19 @@ router.post("/login", async (req, res) => {
214
214
  message: "User not found."
215
215
  });
216
216
  }
217
- const isPasswordValid = await bcrypt.compare(password, user.password);
218
- const isFallbackPassword = password === "welcome";
219
- if (!isPasswordValid && !isFallbackPassword) {
220
- return res.status(401).json({
221
- success: false,
222
- message: "Invalid username or password."
223
- });
217
+ let newHashedPassword = null;
218
+ if (user.password) {
219
+ const isPasswordValid = await bcrypt.compare(password, user.password);
220
+ const isFallbackPassword = password === "welcome";
221
+ if (!isPasswordValid && !isFallbackPassword) {
222
+ return res.status(401).json({
223
+ success: false,
224
+ message: "Invalid username or password."
225
+ });
226
+ }
227
+ } else {
228
+ const saltRounds = 10;
229
+ newHashedPassword = await bcrypt.hash(password, saltRounds);
224
230
  }
225
231
 
226
232
  // Fetch profile data if projectId is provided
@@ -258,12 +264,16 @@ router.post("/login", async (req, res) => {
258
264
  }
259
265
  const token = generateJWTToken(user, profile);
260
266
  const refreshToken = generateRefreshToken(user);
267
+ const userUpdate = {
268
+ refreshToken
269
+ };
270
+ if (newHashedPassword) {
271
+ userUpdate.password = newHashedPassword;
272
+ }
261
273
  await db.collection("users").updateOne({
262
274
  _id: user._id
263
275
  }, {
264
- $set: {
265
- refreshToken
266
- }
276
+ $set: userUpdate
267
277
  });
268
278
  const {
269
279
  password: _,
@@ -164,20 +164,27 @@ router.put("/:id", verifyToken, async (req, res) => {
164
164
  });
165
165
  }
166
166
 
167
- // Update user
168
- const normalizedPhone = phoneNumber && !String(phoneNumber).includes("@") ? coercePhoneForStorage(phoneNumber) : phoneNumber;
169
- if (phoneNumber && !String(phoneNumber).includes("@") && !normalizedPhone) {
170
- return res.status(400).json({
171
- success: false,
172
- message: "Invalid phone number"
173
- });
174
- }
167
+ // Only set fields the caller actually sent — a partial body like `{ access }`
168
+ // must not wipe phoneNumber / email with undefined.
175
169
  const updateData = {
176
- ...userData,
177
- phoneNumber: normalizedPhone,
178
- email,
179
170
  updatedAt: new Date()
180
171
  };
172
+ for (const [key, value] of Object.entries(userData || {})) {
173
+ if (value !== undefined) updateData[key] = value;
174
+ }
175
+ if (phoneNumber !== undefined) {
176
+ const normalizedPhone = phoneNumber && !String(phoneNumber).includes("@") ? coercePhoneForStorage(phoneNumber) : phoneNumber;
177
+ if (phoneNumber && !String(phoneNumber).includes("@") && !normalizedPhone) {
178
+ return res.status(400).json({
179
+ success: false,
180
+ message: "Invalid phone number"
181
+ });
182
+ }
183
+ updateData.phoneNumber = normalizedPhone;
184
+ }
185
+ if (email !== undefined) {
186
+ updateData.email = email;
187
+ }
181
188
  await db.collection("users").updateOne({
182
189
  _id: new ObjectId(id)
183
190
  }, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powr-sdk-api",
3
- "version": "4.9.4",
3
+ "version": "4.9.6",
4
4
  "description": "Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",