feat: роли customer/admin, админ-панель, admin@site.com
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
const bcrypt = require('bcryptjs');
|
||||
const { query } = require('./db');
|
||||
const { ROLES } = require('./constants/roles');
|
||||
|
||||
async function seedAdmin() {
|
||||
const email = (process.env.ADMIN_EMAIL || 'admin@site.com').trim().toLowerCase();
|
||||
const password = process.env.ADMIN_PASSWORD || 'admin';
|
||||
const name = process.env.ADMIN_NAME || 'Администратор';
|
||||
|
||||
const { rows } = await query('SELECT id, role FROM users WHERE email = $1', [email]);
|
||||
|
||||
if (!rows[0]) {
|
||||
const hash = bcrypt.hashSync(password, 10);
|
||||
await query(
|
||||
`INSERT INTO users (email, password_hash, name, role)
|
||||
VALUES ($1, $2, $3, $4)`,
|
||||
[email, hash, name, ROLES.ADMIN]
|
||||
);
|
||||
console.log('Создан администратор:', email);
|
||||
return;
|
||||
}
|
||||
|
||||
if (rows[0].role !== ROLES.ADMIN) {
|
||||
await query('UPDATE users SET role = $1 WHERE id = $2', [ROLES.ADMIN, rows[0].id]);
|
||||
console.log('Пользователю назначена роль admin:', email);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { seedAdmin };
|
||||
Reference in New Issue
Block a user