-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.js
More file actions
94 lines (70 loc) · 2.4 KB
/
resolver.js
File metadata and controls
94 lines (70 loc) · 2.4 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var mysql = require('mysql2');
var bcrypt = require('bcrypt')
export const resolvers = {
Query: {
messages: () => messages,
user(parent, args, context, info) {
if (!context.user) {
throw new Error("Please log in to view this information");
} else {
console.log(context.user)
return context.user.user
}
}
},
Mutation: {
addMessage(parent, args, context, info) {
console.log(args.text + " " + args.author)
var time = new Date().toISOString().slice(0, 19).replace('T', ' ');
var sql = `INSERT INTO user3 (name, message, sendDate) VALUES ('${args.author}', '${args.text}', '${time}')`;
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Inserted");
});
},
getMessage(parent, args, context, info) {
con.query(`SELECT * FROM ${args.table}`, function (err, result, fields) {
if (err) throw err;
console.log(result);
});
},
register: async (parent, args) => {
pass = await bcrypt.hash(args.password, 12);
var time = new Date().toISOString().slice(0, 19).replace('T', ' ');
var sql = `INSERT INTO users (username, email, password, creationDate) VALUES ('${args.username}', '${args.email}', '${pass}', '${time}')`;
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Inserted");
});
console.log("Success")
const user = {
username: args.username,
email: args.email,
password: pass
}
return user
},
login: async (parent, args, context) => {
var theResult = "";
async function get_info(email) {
const results = await con.promise().query(`SELECT * FROM users WHERE email = '${email}'`)
return results[0][0].password
}
theResult = await get_info(args.email)
const isValid = await bcrypt.compare(args.password, theResult);
if (!isValid) {
throw new Error("Incorrect password");
}
console.log(theResult)
const token = await jwt.sign(
{
user: args.email
},
SECRET,
{expiresIn: "1h" }
);
console.log(token)
return token;
}
}
};