-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
72 lines (67 loc) · 2.08 KB
/
database.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const Sequelize = require('sequelize');
const scrypt = require('scryptsy')
var exports = module.exports = {};
var db = {};
exports.connect = () => {
try {
db = new Sequelize('animawka', 'animawka', 'debug', {
port: 3306,
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min:0,
acquire:30000,
idle:10000
}
});
db.authenticate().then(() => {
console.log('Connected to database "animawka"');
});
} catch (error) {
console.log("Connection Error");
console.error(error);
process.exit(1);
}
}
exports.getAnimeList = async () => {
var result = db.query("SELECT * FROM anime");
return result;
}
exports.userLogin = async(email, password) => {
var u = await db.query("SELECT * FROM users WHERE email = "+db.escape(email));
if (u[0] && u[0][0]) {
const hash = scrypt(password.normalize('NFKC'), "animawka", 16384, 8, 1, 64).toString('hex');
if(hash === u[0][0].password){
const user = {
id:u[0][0].ID,
username:u[0][0].login,
email:u[0][0].email,
token:u[0][0].token
};
return user;
} else {
return false;
}
} else {
return false;
}
}
exports.userRegister = async(username, email, password) => {
var u = await db.query("SELECT * FROM users WHERE email = "+db.escape(email)+" AND login = "+db.escape(username));
if (u[0] && u[0][0]) {
return false;
} else {
const hash = scrypt(password.normalize('NFKC'), "animawka", 16384, 8, 1, 64).toString('hex');
var u = await db.query("INSERT INTO users (login, email, password, rank, avatar, token) VALUES"
+ "( "+db.escape(username)
+ ", "+db.escape(email)
+ ", "+db.escape(hash)
+ ", "+db.escape("1")
+ ", "+db.escape("https://vignette2.wikia.nocookie.net/konosuba/images/6/69/6ItDIsj.png/revision/latest?cb=20160210105329")
+ ", "+db.escape("tokenXD")
+ ")"
);
return true;
}
}