From 5df57517365623ffde5acb3f6d06dffe07960704 Mon Sep 17 00:00:00 2001 From: Pitu Date: Fri, 29 Mar 2019 00:36:28 +0900 Subject: Removed apikey from user object and added route for requesting a new one --- src/api/routes/user/apiKey.js | 23 ++++++++++++++++------- src/api/routes/user/userGET.js | 3 +-- src/api/routes/verifyGET.js | 1 - 3 files changed, 17 insertions(+), 10 deletions(-) (limited to 'src/api/routes') diff --git a/src/api/routes/user/apiKey.js b/src/api/routes/user/apiKey.js index 820e28c..f80d563 100644 --- a/src/api/routes/user/apiKey.js +++ b/src/api/routes/user/apiKey.js @@ -1,21 +1,30 @@ const Route = require('../../structures/Route'); const randomstring = require('randomstring'); const moment = require('moment'); +const bcrypt = require('bcrypt'); +const { dump } = require('dumper.js'); class apiKeyPOST extends Route { constructor() { - super('/user/apikey/change', 'post'); + super('/user/apikey/change', 'post', { noApiKey: true }); } 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 - }); + + try { + const hash = await bcrypt.hash(apiKey, 10); + await db.table('users') + .where({ id: user.id }) + .update({ + apiKey: hash, + 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', diff --git a/src/api/routes/user/userGET.js b/src/api/routes/user/userGET.js index 7929aac..fe46fd4 100644 --- a/src/api/routes/user/userGET.js +++ b/src/api/routes/user/userGET.js @@ -11,8 +11,7 @@ class usersGET extends Route { user: { id: user.id, username: user.username, - isAdmin: user.isAdmin, - apiKey: user.apiKey + isAdmin: user.isAdmin } }); } diff --git a/src/api/routes/verifyGET.js b/src/api/routes/verifyGET.js index e588c22..5875dbb 100644 --- a/src/api/routes/verifyGET.js +++ b/src/api/routes/verifyGET.js @@ -9,7 +9,6 @@ class verifyGET extends Route { const returnUser = { id: user.id, username: user.username, - apiKey: user.apiKey, isAdmin: user.isAdmin }; -- cgit v1.2.3