aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/auth/changePasswordPOST.js
diff options
context:
space:
mode:
authorPitu <[email protected]>2018-09-16 00:56:13 -0300
committerPitu <[email protected]>2018-09-16 00:56:13 -0300
commite7767ac7095f93393a627fd5e867af4a1ca4b011 (patch)
treec571d5a13ac949bc8c826a27772b78992b320d58 /src/api/routes/auth/changePasswordPOST.js
parentUtils (diff)
downloadhost.fuwn.me-e7767ac7095f93393a627fd5e867af4a1ca4b011.tar.xz
host.fuwn.me-e7767ac7095f93393a627fd5e867af4a1ca4b011.zip
Routes
Diffstat (limited to 'src/api/routes/auth/changePasswordPOST.js')
-rw-r--r--src/api/routes/auth/changePasswordPOST.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/api/routes/auth/changePasswordPOST.js b/src/api/routes/auth/changePasswordPOST.js
new file mode 100644
index 0000000..bd64320
--- /dev/null
+++ b/src/api/routes/auth/changePasswordPOST.js
@@ -0,0 +1,41 @@
+const Route = require('../../structures/Route');
+const config = require('../../../../config');
+const log = require('../../utils/Log');
+const db = require('knex')(config.server.database);
+const bcrypt = require('bcrypt');
+const moment = require('moment');
+
+class changePasswordPOST extends Route {
+ constructor() {
+ super('/auth/password/change', 'post');
+ }
+
+ async run(req, res, user) {
+ if (!req.body) return res.status(400).json({ message: 'No body provided' });
+ const { password, newPassword } = req.body;
+ if (!password || !newPassword) return res.status(401).json({ message: 'Invalid body provided' });
+
+ if (newPassword.length < 6 || newPassword.length > 64) {
+ return res.status(400).json({ message: 'Password must have 6-64 characters' });
+ }
+
+ let hash;
+ try {
+ hash = await bcrypt.hash(newPassword, 10);
+ } catch (error) {
+ log.error('Error generating password hash');
+ log.error(error);
+ return res.status(401).json({ message: 'There was a problem processing your account' });
+ }
+
+ const now = moment.utc().toDate();
+ await db.table('users').where('id', user.id).update({
+ password: hash,
+ passwordEditedAt: now
+ });
+
+ return res.json({ message: 'The password was changed successfully' });
+ }
+}
+
+module.exports = changePasswordPOST;