blob: f80d56328a20ffad4125f7aecb6639f81fbfbeb4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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', { noApiKey: true });
}
async run(req, res, db, user) {
const now = moment.utc().toDate();
const apiKey = randomstring.generate(64);
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',
apiKey
});
}
}
module.exports = apiKeyPOST;
|