Public Access
feat: enhance setup process with SMTP configuration and token generation
fix: require authentication for certain routes and improve exam data handling chore: update .gitignore to include additional database files
This commit is contained in:
1 parent
ca9c646b65
commit
83bb412b35
8 files changed
+115
-28
No files matched your search
+46
-10
@@ -6,7 +6,7 @@ import { AppError } from '../middleware/error.js'
|
||||
import * as userRepo from '../repositories/userRepo.js'
|
||||
import * as settingsRepo from '../repositories/settingsRepo.js'
|
||||
import { setupSchema } from '../validators/setupSchemas.js'
|
||||
import { sendTestEmail } from '../utils/email.js'
|
||||
import { signToken } from '../utils/tokens.js'
|
||||
|
||||
const router = Router()
|
||||
|
||||
@@ -41,11 +41,24 @@ router.post(
|
||||
throw new AppError(400, 'Users already exist')
|
||||
}
|
||||
|
||||
const { email, password, displayName, siteTitle, language } = req.body
|
||||
const {
|
||||
email,
|
||||
password,
|
||||
displayName,
|
||||
siteTitle,
|
||||
language,
|
||||
smtpHost,
|
||||
smtpPort,
|
||||
smtpUser,
|
||||
smtpPass,
|
||||
smtpFrom,
|
||||
smtpSecure,
|
||||
appUrl,
|
||||
} = req.body
|
||||
|
||||
const passwordHash = await bcrypt.hash(password, SALT_ROUNDS)
|
||||
|
||||
userRepo.createUser({
|
||||
const { id } = userRepo.createUser({
|
||||
email,
|
||||
passwordHash,
|
||||
displayName,
|
||||
@@ -53,16 +66,39 @@ router.post(
|
||||
emailVerified: 1,
|
||||
})
|
||||
|
||||
settingsRepo.setSettingsBatch([
|
||||
['site_title', siteTitle.trim()],
|
||||
['site_language', language],
|
||||
const settings: Array<[string, string]> = [
|
||||
['site_title', siteTitle?.trim() || 'BrainWave'],
|
||||
['site_language', language || 'en'],
|
||||
['maintenance_mode', 'false'],
|
||||
])
|
||||
]
|
||||
|
||||
// Send a test email to the new admin
|
||||
sendTestEmail(email).catch(() => {})
|
||||
if (smtpHost !== undefined) settings.push(['smtp_host', smtpHost])
|
||||
if (smtpPort !== undefined) settings.push(['smtp_port', smtpPort])
|
||||
if (smtpUser !== undefined) settings.push(['smtp_user', smtpUser])
|
||||
if (smtpPass !== undefined) settings.push(['smtp_pass', smtpPass])
|
||||
if (smtpFrom !== undefined) settings.push(['smtp_from', smtpFrom])
|
||||
if (smtpSecure !== undefined) settings.push(['smtp_secure', smtpSecure])
|
||||
if (appUrl !== undefined) settings.push(['app_url', appUrl])
|
||||
|
||||
res.status(201).json({ success: true })
|
||||
settingsRepo.setSettingsBatch(settings)
|
||||
|
||||
const token = signToken({
|
||||
userId: id,
|
||||
email,
|
||||
displayName,
|
||||
role: 'admin',
|
||||
})
|
||||
|
||||
res.status(201).json({
|
||||
message: 'Setup completed successfully',
|
||||
token,
|
||||
user: {
|
||||
id,
|
||||
email,
|
||||
displayName,
|
||||
role: 'admin',
|
||||
},
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user