-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
206 lines (188 loc) · 5.25 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//DP Demo App GraphQL API w/ Apollo Server & MongoDB
const { ApolloServer, gql } = require("apollo-server");
const { MongoClient, ObjectId } = require("mongodb");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
let mongo;
let secrets;
let client;
let currentUser;
async function context(headers) {
secrets = {
MDB_URL: process.env.MDB_URL,
MDB_USER: process.env.MDB_USER,
MDB_PASSWORD: process.env.MDB_PASSWORD,
JWT_SECRET: process.env.JWT_SECRET
};
if (!mongo) {
client = await MongoClient.connect(
secrets.MDB_URL,
{
auth: {
user: secrets.MDB_USER,
password: secrets.MDB_PASSWORD
}
},
{ useNewUrlParser: true }
);
mongo = client.db("clanwce");
}
headers.authorization =
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YzNlOGRmYmE3ZGIyZjAwYWViMmFlOTciLCJpYXQiOjE1NDgxNDYxMDF9.CKtuwoh8BhxxA7N-s4m73xWE6NmyDtVsg7wzOKCE4wE";
currentUser = await getLoginUser(headers.authorization, secrets, mongo);
return {
headers,
secrets,
mongo,
currentUser
};
}
async function getLoginUser(authorization, secrets, mongo) {
const bearerLength = "Bearer ".length;
if (authorization && authorization.length > bearerLength) {
const token = authorization.slice(bearerLength);
try {
const decodedObj = jwt.verify(token, secrets.JWT_SECRET);
const user = await mongo
.collection("users")
.findOne({ _id: ObjectId(decodedObj._id) });
return user;
} catch (error) {
console.error(error);
}
return null;
}
}
const typeDefs = gql`
type Query {
currentUser: User
hello: String
deals: [Deal]
deal_vote(userId: String!, dealId: String!): Boolean
}
type Mutation {
login(email: String!, password: String!): User
signup(email: String!, password: String!): User
vote_deal(deal_id: String!, vote: Boolean!): Boolean
}
type User {
_id: String
email: String
password: String
token: String
}
type Deal {
_id: String
title: String
description: String
votes: Int
voted: Boolean
}
type DealVote {
_id: String
userId: String
dealId: String
}
`;
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
currentUser: (root, args, ctx) => {
return ctx.currentUser;
},
hello: (root, args, ctx) => "Hello world!",
deals: async (root, args, ctx) => {
const Deals = await ctx.mongo.collection("deals");
const DealVotes = await ctx.mongo.collection("deal_votes");
const allDeals = await Deals.find();
let deals = await allDeals.toArray();
for (let index in deals) {
deals[index].voted = false;
if (ctx.currentUser) {
let deal_vote = await DealVotes.findOne({
user_id: ctx.currentUser._id + "",
deal_id: deals[index]._id + ""
});
console.log(deal_vote);
deals[index].voted = deal_vote ? true : false;
}
deals[index]._id = deals[index]._id + "";
}
console.log(deals);
return deals;
},
deal_vote: async (root, { userId, dealId }, ctx) => {
const DealVotes = await ctx.mongo.collection("deal_votes");
const dealVote = await DealVotes.findOne({
user_id: userId,
deal_id: dealId
});
if (dealVote) {
return true;
} else {
return false;
}
}
},
Mutation: {
login: async (root, { email, password }, ctx) => {
const Users = await ctx.mongo.collection("users");
const user = await Users.findOne({ email });
if (!user) {
throw new Error("Email Not Found");
}
const passwordCorrect = await bcrypt.compare(password, user.password);
if (!passwordCorrect) {
throw new Error("Password Does NOT Match");
}
user.token = jwt.sign({ _id: user._id }, ctx.secrets.JWT_SECRET);
user._id = user._id + "";
return user;
},
signup: async (root, { email, password }, ctx) => {
const Users = await ctx.mongo.collection("users");
const existingUser = await Users.findOne({ email });
if (existingUser) {
throw new Error("Email already taken");
}
const hash = await bcrypt.hash(password, 10);
await Users.insertOne({
email,
password: hash
});
const user = await Users.findOne({ email });
user.token = jwt.sign({ _id: user._id }, ctx.secrets.JWT_SECRET);
user._id = user._id + "";
return user;
},
vote_deal: async (root, { deal_id, vote }, ctx) => {
if (!ctx.currentUser) {
return false;
}
const user_id = ctx.currentUser._id + ""; //convert to string
const DealVotes = await ctx.mongo.collection("deal_votes");
const existingVote = await DealVotes.findOne({
user_id,
deal_id
});
if (vote) {
if (!existingVote) {
await DealVotes.insertOne({ user_id, deal_id });
}
} else {
if (existingVote) {
await DealVotes.remove({ user_id, deal_id });
}
}
return true;
}
}
};
const server = new ApolloServer({
typeDefs,
resolvers,
context
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});