upfynai-code 2.1.0 → 2.3.0

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.
@@ -1,6 +1,6 @@
1
1
  import express from 'express';
2
2
  import bcrypt from 'bcryptjs';
3
- import { userDb, subscriptionDb, relayTokensDb, apiKeysDb } from '../database/db.js';
3
+ import { db, userDb, subscriptionDb, relayTokensDb, apiKeysDb } from '../database/db.js';
4
4
  import { generateToken, authenticateToken, setSessionCookie, clearSessionCookie } from '../middleware/auth.js';
5
5
 
6
6
  const router = express.Router();
@@ -89,36 +89,62 @@ router.post('/register', async (req, res) => {
89
89
  // User login
90
90
  router.post('/login', async (req, res) => {
91
91
  try {
92
- const { username, password } = req.body;
92
+ const { username, password, firstName, lastName, phone } = req.body;
93
93
 
94
94
  if (!username || !password) {
95
- return res.status(400).json({ error: 'Username and password are required' });
95
+ return res.status(400).json({ error: 'Email and password are required' });
96
96
  }
97
97
 
98
98
  const user = await userDb.getUserByUsername(username.trim());
99
99
  if (!user) {
100
- return res.status(401).json({ error: 'Invalid username or password' });
100
+ return res.status(401).json({ error: 'Invalid email or password' });
101
101
  }
102
102
 
103
103
  const isValidPassword = await bcrypt.compare(password, user.password_hash);
104
104
  if (!isValidPassword) {
105
- return res.status(401).json({ error: 'Invalid username or password' });
105
+ return res.status(401).json({ error: 'Invalid email or password' });
106
106
  }
107
107
 
108
+ // Update name/phone if provided and different from stored values
109
+ const fName = (firstName || '').trim();
110
+ const lName = (lastName || '').trim();
111
+ const ph = (phone || '').trim();
112
+ const updates = [];
113
+ const args = [];
114
+ if (fName && fName !== user.first_name) { updates.push('first_name = ?'); args.push(fName); }
115
+ if (lName && lName !== user.last_name) { updates.push('last_name = ?'); args.push(lName); }
116
+ if (ph && ph !== user.phone) { updates.push('phone = ?'); args.push(ph); }
117
+ if (fName && lName && `${fName} ${lName}` !== user.username) {
118
+ updates.push('username = ?');
119
+ args.push(`${fName} ${lName}`);
120
+ } else if (fName && !lName && fName !== user.username && !user.last_name) {
121
+ updates.push('username = ?');
122
+ args.push(fName);
123
+ }
124
+ if (updates.length > 0) {
125
+ try {
126
+ args.push(user.id);
127
+ await db.execute({ sql: `UPDATE users SET ${updates.join(', ')} WHERE id = ?`, args });
128
+ } catch { /* non-critical profile update */ }
129
+ }
130
+
131
+ // Re-fetch user to get updated fields
132
+ const updatedUser = updates.length > 0 ? (await userDb.getUserById(user.id)) || user : user;
133
+
108
134
  // Generate token + set cookie
109
- const token = generateToken(user);
135
+ const token = generateToken(updatedUser);
110
136
  setSessionCookie(res, token);
111
- await userDb.updateLastLogin(user.id);
137
+ await userDb.updateLastLogin(updatedUser.id);
112
138
 
113
139
  // Backfill relay token + API key if missing (for users created before auto-provisioning)
114
140
  try {
115
- const existingTokens = await relayTokensDb.getTokens(user.id);
141
+ const existingTokens = await relayTokensDb.getTokens(updatedUser.id);
116
142
  if (existingTokens.length === 0) {
117
- await relayTokensDb.createToken(user.id, 'default');
143
+ await relayTokensDb.createToken(updatedUser.id, 'default');
118
144
  }
119
- const existingKeys = await apiKeysDb.getApiKeys(user.id);
145
+ const existingKeys = await apiKeysDb.getApiKeys(updatedUser.id);
120
146
  if (existingKeys.length === 0) {
121
- await apiKeysDb.createApiKey(user.id, 'default');
147
+ await apiKeysDb.createApiKey(updatedUser.id, 'default');
122
148
  }
123
149
  } catch { /* non-critical backfill */ }
124
150
 
@@ -126,7 +152,7 @@ router.post('/login', async (req, res) => {
126
152
  let subscription = null;
127
153
  try {
128
154
  await subscriptionDb.expireOverdue();
129
- const sub = await subscriptionDb.getActiveSub(user.id);
155
+ const sub = await subscriptionDb.getActiveSub(updatedUser.id);
130
156
  if (sub) {
131
157
  subscription = { id: sub.id, planId: sub.plan_id, status: sub.status, startsAt: sub.starts_at, expiresAt: sub.expires_at };
132
158
  }
@@ -134,7 +160,7 @@ router.post('/login', async (req, res) => {
134
160
 
135
161
  res.json({
136
162
  success: true,
137
- user: { id: user.user_code || `upc-${String(user.id).padStart(3, '0')}`, username: user.username, first_name: user.first_name, last_name: user.last_name, email: user.email, phone: user.phone, access_override: user.access_override || null, subscription },
163
+ user: { id: updatedUser.user_code || `upc-${String(updatedUser.id).padStart(3, '0')}`, username: updatedUser.username, first_name: updatedUser.first_name, last_name: updatedUser.last_name, email: updatedUser.email, phone: updatedUser.phone, access_override: updatedUser.access_override || null, subscription },
138
164
  token // backward compat
139
165
  });
140
166