WellNuo/backend/scripts/create-test-user.js
Sergei 1dd7eb8289 Remove hardcoded credentials and use environment variables
- Remove hardcoded database credentials from all scripts
- Remove hardcoded Legacy API tokens from backend scripts
- Remove hardcoded MQTT credentials from mqtt-test.js
- Update backend/.env.example with DB_HOST, DB_USER, DB_PASSWORD, DB_NAME
- Update backend/.env.example with LEGACY_API_TOKEN and MQTT credentials
- Add dotenv config to all scripts requiring credentials
- Create comprehensive documentation:
  - scripts/README.md - Root scripts usage
  - backend/scripts/README.md - Backend scripts documentation
  - MQTT_TESTING.md - MQTT testing guide
  - SECURITY_CREDENTIALS_CLEANUP.md - Security changes summary

All scripts now read credentials from backend/.env instead of hardcoded values.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-29 12:13:32 -08:00

65 lines
2.0 KiB
JavaScript

const { Client } = require('pg');
require('dotenv').config();
const client = new Client({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: parseInt(process.env.DB_PORT || '5432'),
ssl: {
rejectUnauthorized: false
}
});
const TEST_EMAIL = 'test@test.com';
const TEST_CODE = '123456';
async function run() {
try {
await client.connect();
console.log('Connected to database');
// 1. Check if user exists
const userRes = await client.query('SELECT * FROM public.users WHERE email = $1', [TEST_EMAIL]);
let userId;
if (userRes.rows.length > 0) {
console.log('User already exists:', userRes.rows[0]);
userId = userRes.rows[0].id;
} else {
// 2. Create user (assuming id is serial)
console.log('Creating new user...');
const insertRes = await client.query(`
INSERT INTO public.users (email, name, role, is_active, created_at, updated_at)
VALUES ($1, $2, $3, $4, NOW(), NOW())
RETURNING id;
`, [TEST_EMAIL, 'Test User', 'user', true]);
userId = insertRes.rows[0].id;
console.log('User created with ID:', userId);
}
// 3. Create OTP code
// Delete old codes first
await client.query('DELETE FROM public.otp_codes WHERE email = $1', [TEST_EMAIL]);
const expiresAt = new Date();
expiresAt.setHours(expiresAt.getHours() + 24); // Valid for 24 hours
await client.query(`
INSERT INTO public.otp_codes (email, code, expires_at, created_at)
VALUES ($1, $2, $3, NOW())
`, [TEST_EMAIL, TEST_CODE, expiresAt]);
console.log(`OTP code ${TEST_CODE} created for ${TEST_EMAIL}`);
console.log('You can now log in with this email and OTP.');
} catch (err) {
console.error('Database error:', err);
} finally {
await client.end();
}
}
run();