aboutsummaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorPitu <[email protected]>2019-02-28 23:26:28 +0900
committerPitu <[email protected]>2019-02-28 23:26:28 +0900
commitf37d20694386e59622fdfab586a9b580011efce6 (patch)
treea59d69e97ccabba0d8e3ff1ba0bfc8f4341b9acd /src/api
parentWhen a call regarding the token fails, logout (diff)
downloadhost.fuwn.me-f37d20694386e59622fdfab586a9b580011efce6.tar.xz
host.fuwn.me-f37d20694386e59622fdfab586a9b580011efce6.zip
Change password and api keys
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/auth/apiKey.js23
-rw-r--r--src/api/routes/user/apiKey.js27
-rw-r--r--src/api/routes/user/changePasswordPOST.js40
-rw-r--r--src/api/routes/user/userGET.js21
4 files changed, 88 insertions, 23 deletions
diff --git a/src/api/routes/auth/apiKey.js b/src/api/routes/auth/apiKey.js
deleted file mode 100644
index 84df2e3..0000000
--- a/src/api/routes/auth/apiKey.js
+++ /dev/null
@@ -1,23 +0,0 @@
-const Route = require('../../structures/Route');
-
-class apiKeyGET extends Route {
- constructor() {
- super('/auth/apiKey', 'get');
- }
-
- run(req, res, user) {
- return res.json({ message: 'Hai hai api works.' });
- }
-}
-
-class apiKeyPOST extends Route {
- constructor() {
- super('/auth/apiKey', 'post');
- }
-
- run(req, res, user) {
- return res.json({ message: 'Hai hai api works.' });
- }
-}
-
-module.exports = [apiKeyGET, apiKeyPOST];
diff --git a/src/api/routes/user/apiKey.js b/src/api/routes/user/apiKey.js
new file mode 100644
index 0000000..820e28c
--- /dev/null
+++ b/src/api/routes/user/apiKey.js
@@ -0,0 +1,27 @@
+const Route = require('../../structures/Route');
+const randomstring = require('randomstring');
+const moment = require('moment');
+
+class apiKeyPOST extends Route {
+ constructor() {
+ super('/user/apikey/change', 'post');
+ }
+
+ async run(req, res, db, user) {
+ const now = moment.utc().toDate();
+ const apiKey = randomstring.generate(64);
+ await db.table('users')
+ .where({ id: user.id })
+ .update({
+ apiKey,
+ apiKeyEditedAt: now
+ });
+
+ return res.json({
+ message: 'Successfully created new api key',
+ apiKey
+ });
+ }
+}
+
+module.exports = apiKeyPOST;
diff --git a/src/api/routes/user/changePasswordPOST.js b/src/api/routes/user/changePasswordPOST.js
new file mode 100644
index 0000000..d73cff3
--- /dev/null
+++ b/src/api/routes/user/changePasswordPOST.js
@@ -0,0 +1,40 @@
+const Route = require('../../structures/Route');
+const log = require('../../utils/Log');
+const bcrypt = require('bcrypt');
+const moment = require('moment');
+
+class changePasswordPOST extends Route {
+ constructor() {
+ super('/user/password/change', 'post');
+ }
+
+ async run(req, res, db, 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 (password === newPassword) return res.status(400).json({ message: 'Passwords have to be different' });
+
+ 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;
diff --git a/src/api/routes/user/userGET.js b/src/api/routes/user/userGET.js
new file mode 100644
index 0000000..7929aac
--- /dev/null
+++ b/src/api/routes/user/userGET.js
@@ -0,0 +1,21 @@
+const Route = require('../../structures/Route');
+
+class usersGET extends Route {
+ constructor() {
+ super('/users/me', 'get');
+ }
+
+ run(req, res, db, user) {
+ return res.json({
+ message: 'Successfully retrieved user',
+ user: {
+ id: user.id,
+ username: user.username,
+ isAdmin: user.isAdmin,
+ apiKey: user.apiKey
+ }
+ });
+ }
+}
+
+module.exports = usersGET;