aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/user/apiKey.js
diff options
context:
space:
mode:
authorPitu <[email protected]>2021-01-04 01:04:20 +0900
committerPitu <[email protected]>2021-01-04 01:04:20 +0900
commitfcd39dc550dec8dbcb8325e07e938c5024cbc33d (patch)
treef41acb4e0d5fd3c3b1236fe4324b3fef9ec6eafe /src/api/routes/user/apiKey.js
parentCreate FUNDING.yml (diff)
parentchore: update todo (diff)
downloadhost.fuwn.me-fcd39dc550dec8dbcb8325e07e938c5024cbc33d.tar.xz
host.fuwn.me-fcd39dc550dec8dbcb8325e07e938c5024cbc33d.zip
Merge branch 'dev'
Diffstat (limited to 'src/api/routes/user/apiKey.js')
-rw-r--r--src/api/routes/user/apiKey.js34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/api/routes/user/apiKey.js b/src/api/routes/user/apiKey.js
new file mode 100644
index 0000000..653c56a
--- /dev/null
+++ b/src/api/routes/user/apiKey.js
@@ -0,0 +1,34 @@
+const randomstring = require('randomstring');
+const moment = require('moment');
+const { dump } = require('dumper.js');
+const Route = require('../../structures/Route');
+
+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);
+
+ try {
+ await db.table('users')
+ .where({ id: user.id })
+ .update({
+ apiKey,
+ apiKeyEditedAt: now
+ });
+ } catch (error) {
+ dump(error);
+ return res.status(401).json({ message: 'There was a problem processing your account' });
+ }
+
+ return res.json({
+ message: 'Successfully created new api key',
+ apiKey
+ });
+ }
+}
+
+module.exports = apiKeyPOST;