#!/usr/bin/env node import pg from 'pg'; import bcrypt from 'bcryptjs'; import { readFileSync } from 'fs'; const phone = process.argv[2] || '17883314362'; const newPassword = process.argv[3] || phone.replace(/\D/g, '').slice(-6); const env = readFileSync('/opt/ai-fresh-delivery/server/.env', 'utf8'); const url = env.match(/^DATABASE_URL=(.+)$/m)[1].trim().replace(/^["']|["']$/g, ''); const pool = new pg.Pool({ connectionString: url }); const phoneDigits = phone.replace(/\D/g, ''); try { const user = await pool.query( `SELECT p.id, p.username FROM profiles p WHERE regexp_replace(COALESCE(p.phone, ''), '[^0-9]', '', 'g') = $1 OR p.username = $2 LIMIT 1`, [phoneDigits, phone] ); if (!user.rowCount) { console.error('User not found'); process.exit(1); } const userId = user.rows[0].id; const hash = await bcrypt.hash(newPassword, 10); await pool.query(`UPDATE auth.users SET encrypted_password = $2, updated_at = NOW() WHERE id = $1`, [userId, hash]); await pool.query(`UPDATE profiles SET must_change_password = true, updated_at = NOW() WHERE id = $1`, [userId]); console.log('Reset OK for', user.rows[0].username, 'password length:', newPassword.length); const verify = await pool.query(`SELECT encrypted_password FROM auth.users WHERE id = $1`, [userId]); const ok = await bcrypt.compare(newPassword, verify.rows[0].encrypted_password); console.log('bcrypt verify:', ok); const resp = await fetch('https://www.xdjchuanhui.com/api/mobile/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: phone, password: newPassword }), }); const body = await resp.json(); console.log('mobile/login status:', resp.status); console.log('mobile/login body keys:', Object.keys(body)); if (body.error_description) console.log('error:', body.error_description); if (body.access_token) console.log('login OK role_type:', body.role_type); } finally { await pool.end(); }