christmas/routes/setup/index.js

44 lines
1 KiB
JavaScript
Raw Normal View History

2018-11-20 11:19:58 -08:00
const bcrypt = require('bcrypt-nodejs')
2020-11-08 13:54:08 -08:00
const express = require('express')
2018-11-20 11:19:58 -08:00
module.exports = ({ db, ensurePfp }) => {
2020-11-08 13:54:08 -08:00
const router = express.Router()
2018-11-20 11:19:58 -08:00
router.get('/',
async (req, res) => {
2020-11-08 13:54:08 -08:00
const dbInfo = await db.info()
2018-11-20 11:19:58 -08:00
if (dbInfo.doc_count === 0) {
2021-12-03 08:13:43 -08:00
res.render('setup', { title: _CC.lang('SETUP_HEADER') })
2018-11-20 11:19:58 -08:00
} else {
2020-11-08 13:54:08 -08:00
res.redirect('/')
2018-11-20 11:19:58 -08:00
}
}
2020-11-08 13:54:08 -08:00
)
2018-11-20 11:19:58 -08:00
router.post('/',
async (req, res) => {
2020-11-08 13:54:08 -08:00
const dbInfo = await db.info()
2018-11-20 11:19:58 -08:00
if (dbInfo.doc_count === 0) {
const username = req.body.adminUsername.trim()
await new Promise((resolve, reject) => {
bcrypt.hash(req.body.adminPassword, null, null, (err, adminPasswordHash) => {
if (err) throw err
db.put({
_id: username,
password: adminPasswordHash,
admin: true,
wishlist: []
})
resolve()
2018-11-20 11:19:58 -08:00
})
2020-11-08 13:54:08 -08:00
})
await ensurePfp(username)
2018-11-20 11:19:58 -08:00
}
res.redirect('/')
2018-11-20 11:19:58 -08:00
}
2020-11-08 13:54:08 -08:00
)
2018-11-20 11:19:58 -08:00
2020-11-08 13:54:08 -08:00
return router
}