smart_ims/server/init_users.js
2026-01-22 23:42:55 +09:00

53 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const db = require('./db');
const crypto = require('crypto');
async function init() {
try {
console.log('Initializing Users Table...');
const createTableSQL = `
CREATE TABLE IF NOT EXISTS users (
id VARCHAR(50) PRIMARY KEY COMMENT '아이디',
password VARCHAR(255) NOT NULL COMMENT '비밀번호 (Hash)',
name VARCHAR(100) NOT NULL COMMENT '이름',
department VARCHAR(100) COMMENT '부서',
position VARCHAR(100) COMMENT '직위',
phone VARCHAR(255) COMMENT '핸드폰 (Encrypted)',
role ENUM('admin', 'user') DEFAULT 'user' COMMENT '권한',
last_login TIMESTAMP NULL COMMENT '마지막 로그인',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='사용자 관리';
`;
await db.query(createTableSQL);
console.log('✅ Users Table Created/Verified.');
const [rows] = await db.query('SELECT * FROM users WHERE id = ?', ['admin']);
if (rows.length === 0) {
// Create default admin
const password = 'admin123';
// Simple hash for MVP: SHA-256. In production, use bcrypt or scrypt with salt.
const hashedPassword = crypto.createHash('sha256').update(password).digest('hex');
// Placeholder for encrypted phone
const encryptedPhone = 'pending_encryption';
await db.query(
'INSERT INTO users (id, password, name, role, department, position, phone) VALUES (?, ?, ?, ?, ?, ?, ?)',
['admin', hashedPassword, '시스템 관리자', 'admin', 'IT팀', '관리자', encryptedPhone]
);
console.log('✅ Default Admin Account Created (admin / admin123)');
} else {
console.log(' Admin account already exists.');
}
process.exit(0);
} catch (err) {
console.error('❌ Initialization Failed:', err);
process.exit(1);
}
}
init();