diff options
author | 2022-05-29 17:45:54 +0100 | |
---|---|---|
committer | 2022-05-29 17:45:54 +0100 | |
commit | 308d07785f811ff470d0e90b11680926a823027b (patch) | |
tree | 7afe7859fc29f6a5c34be8b7b486cc317cc82e21 /pages/api/password.js | |
parent | 50d781d0bdcac217f2bc037abe087a00019edba5 (diff) | |
download | my_apps-308d07785f811ff470d0e90b11680926a823027b.tar.gz my_apps-308d07785f811ff470d0e90b11680926a823027b.tar.bz2 my_apps-308d07785f811ff470d0e90b11680926a823027b.zip |
add change password option
Diffstat (limited to 'pages/api/password.js')
-rw-r--r-- | pages/api/password.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/pages/api/password.js b/pages/api/password.js new file mode 100644 index 0000000..871c9e6 --- /dev/null +++ b/pages/api/password.js @@ -0,0 +1,35 @@ +import dbConnect from 'configs/dbConnect' +import withSession from 'hocs/withSession' +import sendMail from 'configs/sendMail' +import mailData from 'helpers/email' +import User from 'models/User' + +export default withSession(async (req, res) => { + await dbConnect() + + switch (req.method) { + case 'POST': + try { + const data = JSON.parse(req.body) + if (data._id && data.currentPassword && data.newPassword) { + const user = await User.savePassword(data) + req.session.set('user', user) + await req.session.save() + + sendMail({to: user.email, ...mailData('p', user.language)}) + + res.status(200).json(user) + } + } catch (error) { + if (error?.message === 'Wrong password') { + res.status(401).send({ error: 'Wrong password' }) + } else { + res.status(400).send({ error }) + } + } + break + default: + res.status(400).send({ error }) + break + } +}) |