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) => {
|
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) {
|
|
|
|
bcrypt.hash(req.body.adminPassword, null, null, (err, adminPasswordHash) => {
|
2020-11-08 13:54:08 -08:00
|
|
|
if (err) throw err
|
2018-11-20 11:19:58 -08:00
|
|
|
db.put({
|
2018-12-03 14:03:43 -08:00
|
|
|
_id: req.body.adminUsername.trim(),
|
2018-11-20 11:19:58 -08:00
|
|
|
password: adminPasswordHash,
|
|
|
|
admin: true,
|
|
|
|
wishlist: []
|
|
|
|
})
|
2020-11-08 13:54:08 -08:00
|
|
|
res.redirect('/')
|
|
|
|
})
|
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
|
|
|
|
2020-11-08 13:54:08 -08:00
|
|
|
return router
|
|
|
|
}
|