|
1
|
|
|
var express = require('express'); |
|
2
|
|
|
var router = express.Router(); |
|
3
|
|
|
// const config = require('../config'); |
|
4
|
|
|
const sqlite3 = require('sqlite3').verbose(); |
|
|
|
|
|
|
5
|
|
|
// const db = new sqlite3.Database('./db/texts.sqlite'); |
|
6
|
|
|
const db = require("../db/database.js"); |
|
7
|
|
|
const bcrypt = require('bcryptjs'); |
|
8
|
|
|
const jwt = require('jsonwebtoken'); |
|
9
|
|
|
|
|
10
|
|
|
let config; |
|
11
|
|
|
|
|
12
|
|
|
try { |
|
13
|
|
|
config = require('../config.js'); |
|
14
|
|
|
} catch (error) { |
|
15
|
|
|
console.error(error); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
const jwtSecret = process.env.JWT_SECRET || config.secret; |
|
19
|
|
|
|
|
20
|
|
|
router.get('/', function(req, res, next) { |
|
|
|
|
|
|
21
|
|
|
const data = { |
|
22
|
|
|
data: { |
|
23
|
|
|
msg: "Login a user" |
|
24
|
|
|
} |
|
25
|
|
|
}; |
|
26
|
|
|
|
|
27
|
|
|
res.json(data); |
|
28
|
|
|
}); |
|
29
|
|
|
|
|
30
|
|
|
router.post("/", (req, res) => { |
|
31
|
|
|
login(res, req.body); |
|
32
|
|
|
|
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
|
|
function login(res, body) { |
|
36
|
|
|
const email = body.email; |
|
37
|
|
|
const password = body.password; |
|
38
|
|
|
|
|
39
|
|
|
if (!email || !password) { |
|
40
|
|
|
return res.status(401).json({ |
|
41
|
|
|
errors: { |
|
42
|
|
|
status: 401, |
|
43
|
|
|
source: "/login", |
|
44
|
|
|
title: "Email or password missing", |
|
45
|
|
|
detail: "Email or password missing in request" |
|
46
|
|
|
} |
|
47
|
|
|
}); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
db.get("SELECT * FROM users WHERE email = ?", |
|
51
|
|
|
email, |
|
52
|
|
|
(err, rows) => { |
|
53
|
|
|
if (rows === undefined) { |
|
54
|
|
|
return res.status(401).json({ |
|
55
|
|
|
errors: { |
|
56
|
|
|
status: 401, |
|
57
|
|
|
source: "/login", |
|
58
|
|
|
title: "User not found", |
|
59
|
|
|
detail: "User with provided email not found." |
|
60
|
|
|
} |
|
61
|
|
|
}); |
|
62
|
|
|
} |
|
63
|
|
|
const user = rows; |
|
64
|
|
|
|
|
65
|
|
|
bcrypt.compare(password, user.password, (err, result) => { |
|
66
|
|
|
if (err) { |
|
67
|
|
|
return res.status(500).json({ |
|
68
|
|
|
errors: { |
|
69
|
|
|
status: 500, |
|
70
|
|
|
source: "/login", |
|
71
|
|
|
title: "bcrypt error", |
|
72
|
|
|
detail: "bcrypt error" |
|
73
|
|
|
} |
|
74
|
|
|
}); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (result) { |
|
78
|
|
|
let payload = { email: user.email }; |
|
79
|
|
|
let jwtToken = jwt.sign(payload, jwtSecret, { expiresIn: '24h' }); |
|
80
|
|
|
|
|
81
|
|
|
return res.json({ |
|
82
|
|
|
data: { |
|
83
|
|
|
type: "success", |
|
84
|
|
|
message: "User logged in", |
|
85
|
|
|
user: payload, |
|
86
|
|
|
token: jwtToken |
|
87
|
|
|
} |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return res.status(401).json({ |
|
92
|
|
|
errors: { |
|
93
|
|
|
status: 401, |
|
94
|
|
|
source: "/login", |
|
95
|
|
|
title: "Wrong password", |
|
96
|
|
|
detail: "Password is incorrect." |
|
97
|
|
|
} |
|
98
|
|
|
}); |
|
99
|
|
|
}); |
|
|
|
|
|
|
100
|
|
|
}); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
module.exports = router; |
|
104
|
|
|
|