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,50 +609,64 @@ 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';
|
||||||
try {
|
|
||||||
// Ensure system tenant exists
|
|
||||||
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]);
|
for (let i = 0; i < retries; i++) {
|
||||||
if (existing.length === 0) {
|
try {
|
||||||
console.log('Provisioning default super_admin...');
|
// Test connection first
|
||||||
const uid = `u_${crypto.randomUUID().split('-')[0]}`;
|
const connection = await pool.getConnection();
|
||||||
const placeholderHash = 'pending_setup';
|
connection.release();
|
||||||
|
|
||||||
await pool.query(
|
// Ensure system tenant exists
|
||||||
'INSERT INTO users (id, tenant_id, name, email, password_hash, role, status) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
await pool.query('INSERT IGNORE INTO tenants (id, name, slug, admin_email, status) VALUES (?, ?, ?, ?, ?)', ['system', 'System Admin', 'system', email, 'active']);
|
||||||
[uid, 'system', 'Blyzer Suporte', email, placeholderHash, 'super_admin', 'active']
|
|
||||||
);
|
|
||||||
|
|
||||||
const token = crypto.randomBytes(32).toString('hex');
|
const [existing] = await pool.query('SELECT id, password_hash FROM users WHERE email = ?', [email]);
|
||||||
await pool.query(
|
if (existing.length === 0 || existing[0].password_hash === 'pending_setup') {
|
||||||
'INSERT INTO password_resets (email, token, expires_at) VALUES (?, ?, DATE_ADD(NOW(), INTERVAL 15 MINUTE))',
|
console.log('Provisioning default super_admin or resending email...');
|
||||||
[email, token]
|
|
||||||
);
|
|
||||||
|
|
||||||
const setupLink = `${process.env.APP_URL || 'http://localhost:3001'}/#/reset-password?token=${token}`;
|
if (existing.length === 0) {
|
||||||
console.log(`\n\n=== SUPER ADMIN SETUP LINK ===\n${setupLink}\n==============================\n\n`);
|
const uid = `u_${crypto.randomUUID().split('-')[0]}`;
|
||||||
|
const placeholderHash = 'pending_setup';
|
||||||
|
|
||||||
await transporter.sendMail({
|
await pool.query(
|
||||||
from: `"Fasto" <${process.env.MAIL_FROM || 'nao-responda@blyzer.com.br'}>`,
|
'INSERT INTO users (id, tenant_id, name, email, password_hash, role, status) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||||
to: email,
|
[uid, 'system', 'Blyzer Suporte', email, placeholderHash, 'super_admin', 'active']
|
||||||
subject: 'Conta Super Admin Criada - Fasto',
|
);
|
||||||
html: `
|
}
|
||||||
<div style="font-family: sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #e2e8f0; border-radius: 12px; background: #ffffff; color: #0f172a;">
|
|
||||||
<h2 style="color: #0f172a;">Conta Super Admin Gerada</h2>
|
const token = crypto.randomBytes(32).toString('hex');
|
||||||
<p style="color: #475569;">Sua conta de suporte (super_admin) foi criada no Fasto.</p>
|
await pool.query(
|
||||||
<div style="text-align: center; margin: 30px 0;">
|
'INSERT INTO password_resets (email, token, expires_at) VALUES (?, ?, DATE_ADD(NOW(), INTERVAL 15 MINUTE))',
|
||||||
<a href="${setupLink}" style="background-color: #0f172a; color: white; padding: 12px 24px; text-decoration: none; border-radius: 8px; font-weight: bold; display: inline-block;">Finalizar Cadastro</a>
|
[email, token]
|
||||||
|
);
|
||||||
|
|
||||||
|
const setupLink = `${process.env.APP_URL || 'http://localhost:3001'}/#/reset-password?token=${token}`;
|
||||||
|
console.log(`\n\n=== SUPER ADMIN SETUP LINK ===\n${setupLink}\n==============================\n\n`);
|
||||||
|
|
||||||
|
await transporter.sendMail({
|
||||||
|
from: `"Fasto" <${process.env.MAIL_FROM || 'nao-responda@blyzer.com.br'}>`,
|
||||||
|
to: email,
|
||||||
|
subject: 'Conta Super Admin Criada - Fasto',
|
||||||
|
html: `
|
||||||
|
<div style="font-family: sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #e2e8f0; border-radius: 12px; background: #ffffff; color: #0f172a;">
|
||||||
|
<h2 style="color: #0f172a;">Conta Super Admin Gerada</h2>
|
||||||
|
<p style="color: #475569;">Sua conta de suporte (super_admin) foi criada no Fasto.</p>
|
||||||
|
<div style="text-align: center; margin: 30px 0;">
|
||||||
|
<a href="${setupLink}" style="background-color: #0f172a; color: white; padding: 12px 24px; text-decoration: none; border-radius: 8px; font-weight: bold; display: inline-block;">Finalizar Cadastro</a>
|
||||||
|
</div>
|
||||||
|
<p style="font-size: 12px; color: #94a3b8;">Este link expira em 15 minutos.</p>
|
||||||
</div>
|
</div>
|
||||||
<p style="font-size: 12px; color: #94a3b8;">Este link expira em 15 minutos.</p>
|
`
|
||||||
</div>
|
}).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) {
|
||||||
|
console.error(`Failed to provision super_admin (Attempt ${i + 1}/${retries}):`, error.message);
|
||||||
|
if (i < retries - 1) {
|
||||||
|
await new Promise(res => setTimeout(res, delay));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to provision super_admin:', error);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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