1
|
|
|
import jwt from "jsonwebtoken"; |
2
|
|
|
import { db } from "./db.js" |
3
|
|
|
|
4
|
|
|
const user = { |
5
|
|
|
extractEmail: async function(githubToken) { |
|
|
|
|
6
|
|
|
// add logic here for extracting user email from Github |
7
|
|
|
// await .... |
8
|
|
|
}, |
9
|
|
|
insertIntoDB: async function(email, cardnr, cardtype) { |
10
|
|
|
|
11
|
|
|
const result = await db.queryWithArgs(`CALL new_user(?, ?, ?);`, [email, cardnr, cardtype]); |
12
|
|
|
|
13
|
|
|
return result[0][0]; |
14
|
|
|
|
15
|
|
|
}, |
16
|
|
|
/** |
17
|
|
|
* body should contain Github Token, |
18
|
|
|
* Card nr as string and card type as int |
19
|
|
|
* @param {*} req |
20
|
|
|
* @param {*} res |
21
|
|
|
* @param {*} next |
22
|
|
|
*/ |
23
|
|
|
register: async function(req, res, next) { |
|
|
|
|
24
|
|
|
const email = this.extractEmail(req.body.token) |
25
|
|
|
const payload = await this.newUser(email, req.body.cardnr, req.body.cardtype); |
26
|
|
|
const jwtToken = jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: "24h" }); |
27
|
|
|
return res.json({ |
28
|
|
|
data: { |
29
|
|
|
type: "success", |
30
|
|
|
message: "User logged in", |
31
|
|
|
user: payload, |
32
|
|
|
token: jwtToken |
33
|
|
|
} |
34
|
|
|
}); |
35
|
|
|
}, |
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @param {String | Int} what id or email to search for, for wilcard search add % before, after or both |
39
|
|
|
* @return {Array} if the search string is not a wildcard the array will only contain one object |
40
|
|
|
*/ |
41
|
|
|
userSearch: async function(what) { |
42
|
|
|
const result = await db.queryWithArgs(`CALL user_search(?);`, [what]); |
43
|
|
|
return result[0].map((user) => { |
44
|
|
|
return this.adjTypes(user); |
45
|
|
|
}); |
46
|
|
|
}, |
47
|
|
|
adjTypes(userObj) { |
48
|
|
|
userObj.balance = parseFloat(userObj.balance); |
49
|
|
|
userObj.active = userObj.active === 1; |
50
|
|
|
return userObj; |
51
|
|
|
}, |
52
|
|
|
}; |
53
|
|
|
|
54
|
|
|
export default user; |
55
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.