-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.js
38 lines (32 loc) · 1.01 KB
/
auth.js
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
37
38
const express = require('express');
const connection = require('../helpers/connection');
const query = require('../helpers/query');
const router = express.Router();
const dbConfig = require('../dbConfig');
const create = require('../crud/create');
router.post('/register', async (req, res) => {
const { username, password } = req.body;
const conn = await connection(dbConfig).catch(e => {});
const result = await create(
conn,
'USERS',
['username', 'password'],
[username, { toString: () => `MD5('${password}')`}]
);
const [user = {}] = result;
res.send({
id: user.id || null,
username: user.username || null,
});
});
router.post('/login', async (req, res) => {
const { username, password } = req.body;
const conn = await connection(dbConfig).catch(e => {});
const user = await query(
conn,
`SELECT id, username FROM USERS WHERE username=? AND password=MD5(?)`,
[username, password]
);
res.send(user[0] || { id: null, username: null });
});
module.exports = router;