fix: resolve production SMTP environment mapping and super_admin provisioning recovery
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m24s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m24s
This commit is contained in:
@@ -609,15 +609,23 @@ if (process.env.NODE_ENV === 'production') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Auto-provision Super Admin
|
// Auto-provision Super Admin
|
||||||
const provisionSuperAdmin = async () => {
|
const provisionSuperAdmin = async (retries = 5, delay = 5000) => {
|
||||||
const email = 'suporte@blyzer.com.br';
|
const email = 'suporte@blyzer.com.br';
|
||||||
|
|
||||||
|
for (let i = 0; i < retries; i++) {
|
||||||
try {
|
try {
|
||||||
|
// Test connection first
|
||||||
|
const connection = await pool.getConnection();
|
||||||
|
connection.release();
|
||||||
|
|
||||||
// Ensure system tenant exists
|
// Ensure system tenant exists
|
||||||
await pool.query('INSERT IGNORE INTO tenants (id, name, slug, admin_email, status) VALUES (?, ?, ?, ?, ?)', ['system', 'System Admin', 'system', email, 'active']);
|
await pool.query('INSERT IGNORE INTO tenants (id, name, slug, admin_email, status) VALUES (?, ?, ?, ?, ?)', ['system', 'System Admin', 'system', email, 'active']);
|
||||||
|
|
||||||
const [existing] = await pool.query('SELECT id FROM users WHERE email = ?', [email]);
|
const [existing] = await pool.query('SELECT id, password_hash FROM users WHERE email = ?', [email]);
|
||||||
|
if (existing.length === 0 || existing[0].password_hash === 'pending_setup') {
|
||||||
|
console.log('Provisioning default super_admin or resending email...');
|
||||||
|
|
||||||
if (existing.length === 0) {
|
if (existing.length === 0) {
|
||||||
console.log('Provisioning default super_admin...');
|
|
||||||
const uid = `u_${crypto.randomUUID().split('-')[0]}`;
|
const uid = `u_${crypto.randomUUID().split('-')[0]}`;
|
||||||
const placeholderHash = 'pending_setup';
|
const placeholderHash = 'pending_setup';
|
||||||
|
|
||||||
@@ -625,6 +633,7 @@ const provisionSuperAdmin = async () => {
|
|||||||
'INSERT INTO users (id, tenant_id, name, email, password_hash, role, status) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
'INSERT INTO users (id, tenant_id, name, email, password_hash, role, status) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||||
[uid, 'system', 'Blyzer Suporte', email, placeholderHash, 'super_admin', 'active']
|
[uid, 'system', 'Blyzer Suporte', email, placeholderHash, 'super_admin', 'active']
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const token = crypto.randomBytes(32).toString('hex');
|
const token = crypto.randomBytes(32).toString('hex');
|
||||||
await pool.query(
|
await pool.query(
|
||||||
@@ -651,8 +660,13 @@ const provisionSuperAdmin = async () => {
|
|||||||
`
|
`
|
||||||
}).catch(err => console.error("Failed to send super_admin email:", err));
|
}).catch(err => console.error("Failed to send super_admin email:", err));
|
||||||
}
|
}
|
||||||
|
return; // Success, exit the retry loop
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to provision super_admin:', error);
|
console.error(`Failed to provision super_admin (Attempt ${i + 1}/${retries}):`, error.message);
|
||||||
|
if (i < retries - 1) {
|
||||||
|
await new Promise(res => setTimeout(res, delay));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,13 @@ services:
|
|||||||
- DB_USER=${DB_USER:-root}
|
- DB_USER=${DB_USER:-root}
|
||||||
- DB_PASSWORD=${DB_PASSWORD:-root_password}
|
- DB_PASSWORD=${DB_PASSWORD:-root_password}
|
||||||
- DB_NAME=${DB_NAME:-agenciac_comia}
|
- DB_NAME=${DB_NAME:-agenciac_comia}
|
||||||
|
- SMTP_HOST=${SMTP_HOST}
|
||||||
|
- SMTP_PORT=${SMTP_PORT}
|
||||||
|
- SMTP_USER=${SMTP_USER}
|
||||||
|
- SMTP_PASS=${SMTP_PASS}
|
||||||
|
- MAIL_FROM=${MAIL_FROM}
|
||||||
|
- APP_URL=${APP_URL}
|
||||||
|
- JWT_SECRET=${JWT_SECRET}
|
||||||
ports:
|
ports:
|
||||||
- "3001:3001"
|
- "3001:3001"
|
||||||
deploy:
|
deploy:
|
||||||
|
|||||||
Reference in New Issue
Block a user