diff options
| author | Pitu <[email protected]> | 2019-03-19 07:58:36 +0000 |
|---|---|---|
| committer | Pitu <[email protected]> | 2019-03-19 07:58:36 +0000 |
| commit | 107d1f4750e8f82a628b528c4ec200e918be271d (patch) | |
| tree | 0225bca3fd2a27e81b2ff711e53278cee890fdab /src/api/routes/user/apiKey.js | |
| parent | add restart at the end of update command (diff) | |
| download | host.fuwn.me-107d1f4750e8f82a628b528c4ec200e918be271d.tar.xz host.fuwn.me-107d1f4750e8f82a628b528c4ec200e918be271d.zip | |
API key WIP
Diffstat (limited to 'src/api/routes/user/apiKey.js')
| -rw-r--r-- | src/api/routes/user/apiKey.js | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/src/api/routes/user/apiKey.js b/src/api/routes/user/apiKey.js index 820e28c..7de6cb8 100644 --- a/src/api/routes/user/apiKey.js +++ b/src/api/routes/user/apiKey.js @@ -1,6 +1,7 @@ const Route = require('../../structures/Route'); const randomstring = require('randomstring'); const moment = require('moment'); +const bcrypt = require('bcrypt'); class apiKeyPOST extends Route { constructor() { @@ -10,17 +11,27 @@ class apiKeyPOST extends Route { 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 + }); + + return res.json({ + message: 'Successfully created new api key', + apiKey }); - return res.json({ - message: 'Successfully created new api key', - apiKey - }); + } catch (error) { + return super.error(res, error); + } + + } } |