2020-11-08 13:54:08 -08:00
|
|
|
const verifyAuth = require('../../middlewares/verifyAuth')
|
|
|
|
const bcrypt = require('bcrypt-nodejs')
|
|
|
|
const express = require('express')
|
2018-11-20 11:19:58 -08:00
|
|
|
|
2021-10-11 06:50:09 -07:00
|
|
|
module.exports = ({ db, config, ensurePfp }) => {
|
2020-11-08 13:54:08 -08:00
|
|
|
const router = express.Router()
|
2018-11-20 11:19:58 -08:00
|
|
|
|
2021-09-14 23:41:44 -07:00
|
|
|
router.get('/', verifyAuth(), async (req, res) => {
|
|
|
|
await ensurePfp(req.user._id)
|
2021-12-03 08:13:43 -08:00
|
|
|
res.render('profile', { title: _CC.lang('PROFILE_TITLE', req.user._id) })
|
2021-09-14 23:41:44 -07:00
|
|
|
})
|
2021-09-14 23:55:39 -07:00
|
|
|
|
2021-09-14 23:41:44 -07:00
|
|
|
router.post('/pfp', verifyAuth(), async (req, res) => {
|
2021-10-11 06:50:09 -07:00
|
|
|
if (config.pfp) {
|
|
|
|
req.user.pfp = req.body.image
|
|
|
|
await db.put(req.user)
|
|
|
|
if (!req.user.pfp) await ensurePfp(req.user._id)
|
2021-12-03 08:13:43 -08:00
|
|
|
req.flash('success', _CC.lang('PROFILE_SAVE_PFP_SUCCESS'))
|
2021-10-11 06:50:09 -07:00
|
|
|
} else {
|
2021-12-03 08:13:43 -08:00
|
|
|
req.flash('error', _CC.lang('PROFILE_SAVE_PFP_DISABLED'))
|
2021-10-11 06:50:09 -07:00
|
|
|
}
|
2021-09-14 23:41:44 -07:00
|
|
|
res.redirect(`${_CC.config.base}profile`)
|
|
|
|
})
|
2021-09-14 23:55:39 -07:00
|
|
|
|
|
|
|
router.get('/password', verifyAuth(), async (req, res) => {
|
|
|
|
await ensurePfp(req.user._id)
|
2021-12-03 08:13:43 -08:00
|
|
|
res.render('profile-password', { title: _CC.lang('PROFILE_PASSWORD_TITLE', req.user._id) })
|
2021-09-14 23:55:39 -07:00
|
|
|
})
|
2021-09-14 23:51:58 -07:00
|
|
|
router.post('/password', verifyAuth(), (req, res) => {
|
|
|
|
if (!req.body.oldPassword) {
|
2021-12-03 08:13:43 -08:00
|
|
|
req.flash('error', _CC.lang('PROFILE_PASSWORD_REQUIRED_OLD'))
|
2021-09-14 23:51:58 -07:00
|
|
|
return res.redirect('/profile/password')
|
2018-11-20 11:19:58 -08:00
|
|
|
}
|
2021-09-14 23:51:58 -07:00
|
|
|
if (!req.body.newPassword) {
|
2021-12-03 08:13:43 -08:00
|
|
|
req.flash('error', _CC.lang('PROFILE_PASSWORD_REQUIRED_NEW'))
|
2021-09-14 23:51:58 -07:00
|
|
|
return res.redirect('/profile/password')
|
|
|
|
}
|
|
|
|
bcrypt.compare(req.body.oldPassword, req.user.password, (err, correct) => {
|
|
|
|
if (err) throw err
|
|
|
|
if (correct) {
|
|
|
|
bcrypt.hash(req.body.newPassword, null, null, (err, hash) => {
|
|
|
|
if (err) throw err
|
|
|
|
db.get(req.user._id)
|
|
|
|
.then(doc => {
|
|
|
|
doc.password = hash
|
|
|
|
db.put(doc)
|
|
|
|
.then(() => {
|
2021-12-03 08:13:43 -08:00
|
|
|
req.flash('success', _CC.lang('PROFILE_PASSWORD_SUCCESS'))
|
2021-09-14 23:51:58 -07:00
|
|
|
res.redirect('/profile/password')
|
|
|
|
})
|
|
|
|
.catch(err => { throw err })
|
|
|
|
})
|
|
|
|
.catch(err => { throw err })
|
|
|
|
})
|
|
|
|
} else {
|
2021-12-03 08:13:43 -08:00
|
|
|
req.flash('error', _CC.lang('PROFILE_PASSWORD_OLD_MISMATCH'))
|
2021-09-14 23:51:58 -07:00
|
|
|
res.redirect('/profile/password')
|
|
|
|
}
|
|
|
|
})
|
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
|
|
|
|
}
|