fix: ensure profile picture on admin user creation (#38)

This commit is contained in:
Mario 2022-10-06 12:45:32 -05:00 committed by GitHub
parent 6900c401de
commit d3f85afa1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 12 deletions

View file

@ -48,7 +48,7 @@ module.exports = ({ db, config }) => {
router.use('/api', require('./api')({ db })) router.use('/api', require('./api')({ db }))
router.use('/setup', require('./setup')(db)) router.use('/setup', require('./setup')({ db, ensurePfp }))
router.use('/login', require('./login')({ ensurePfp })) router.use('/login', require('./login')({ ensurePfp }))
router.use('/logout', require('./logout')()) router.use('/logout', require('./logout')())

View file

@ -1,7 +1,7 @@
const bcrypt = require('bcrypt-nodejs') const bcrypt = require('bcrypt-nodejs')
const express = require('express') const express = require('express')
module.exports = (db) => { module.exports = ({ db, ensurePfp }) => {
const router = express.Router() const router = express.Router()
router.get('/', router.get('/',
@ -19,19 +19,23 @@ module.exports = (db) => {
async (req, res) => { async (req, res) => {
const dbInfo = await db.info() const dbInfo = await db.info()
if (dbInfo.doc_count === 0) { if (dbInfo.doc_count === 0) {
bcrypt.hash(req.body.adminPassword, null, null, (err, adminPasswordHash) => { const username = req.body.adminUsername.trim()
if (err) throw err await new Promise((resolve, reject) => {
db.put({ bcrypt.hash(req.body.adminPassword, null, null, (err, adminPasswordHash) => {
_id: req.body.adminUsername.trim(), if (err) throw err
password: adminPasswordHash, db.put({
admin: true, _id: username,
wishlist: [] password: adminPasswordHash,
admin: true,
wishlist: []
})
resolve()
}) })
res.redirect('/')
}) })
} else { await ensurePfp(username)
res.redirect('/')
} }
res.redirect('/')
} }
) )