-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
308 lines (288 loc) · 9.55 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const dotenv = require("dotenv");
dotenv.config();
const admin = require("firebase-admin");
const serviceAccount = require("./serviceWorker.json");
const bcrypt = require("bcrypt");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
const {
ApolloServer,
ApolloError,
ValidationError,
ForbiddenError,
AuthenticationError,
gql,
} = require("apollo-server");
const depthLimit = require("graphql-depth-limit");
const typeDefs = require("./schema");
const rateLimit = require("./graphql-functions/universal-ratelimit");
const auth = require("./graphql-functions/auth-token");
const createToken = require("./graphql-functions/create-token");
const validate = require("./graphql-functions/validate");
const resolvers = {
User: {
async mainGroup(user) {
try {
const mainGroup = await admin
.firestore()
.collection("groups")
.doc(user.mainGroup.id)
.get();
return mainGroup.data();
} catch (e) {
throw new ApolloError(e);
}
},
async groups(user) {
try {
const userGroups = await admin
.firestore()
.collection("groups")
.where("memberIDs", "array-contains", user.id)
.get();
return userGroups.docs.map((val) => val.data());
} catch (e) {
throw new ApolloError(e);
}
},
},
Group: {
async owner(group) {
try {
const owner = await admin
.firestore()
.doc(`groups/${group.id}/members/${group.owner.id}`)
.get();
return owner.data();
} catch (e) {
throw new ApolloError(e);
}
},
async admins(group) {
try {
let admins = [];
for (let val of group.admins) {
const userDoc = await admin
.firestore()
.collection("groups")
.doc(group.id)
.collection("members")
.doc(val.id)
.get();
admins.push(userDoc.data());
}
return admins;
} catch (e) {
throw new ApolloError(e);
}
},
async members(group) {
try {
const groupMembers = await admin
.firestore()
.collection("groups")
.doc(group.id)
.collection("members")
.get();
return groupMembers.docs.map((val) => val.data());
} catch (e) {
throw new ApolloError(e);
}
},
async parties(group) {
try {
const groupParties = await admin
.firestore()
.collection("groups")
.doc(group.id)
.collection("parties")
.get();
return groupParties.docs.map((val) => val.data());
} catch (e) {
throw new ApolloError(e);
}
},
},
GroupMember: {
async user(groupMember) {
try {
const userDoc = await admin
.firestore()
.doc(`users/${groupMember.id}`)
.get();
const user = userDoc.data();
return user;
} catch (e) {
throw new ApolloError(e);
}
},
async group(groupMember) {
try {
const group = await admin
.firestore()
.collection("groups")
.doc(groupMember.group.id)
.get();
return group.data();
} catch (e) {
throw new ApolloError(e);
}
},
},
Party: {
async group(party) {
try {
const group = await admin
.firestore()
.collection("groups")
.doc(party.group.id)
.get();
return group.data();
} catch (e) {
throw new ApolloError(e);
}
},
async attendees(party) {
try {
let attendees = [];
const partyDoc = await admin
.firestore()
.collection("groups")
.doc(party.group.id)
.collection("parties")
.doc(party.id)
.get();
for (let val of party.attendees) {
const userDoc = await admin
.firestore()
.collection("users")
.doc(val.id)
.get();
attendees.push(userDoc.data());
}
return attendees;
} catch (e) {
throw new ApolloError(e);
}
},
},
Query: {
async user(_, args, context, info) {
try {
await auth(context.token, admin);
await rateLimit(_, args, context, info);
const userDoc = await admin
.firestore()
.doc(`users/${args.id}`)
.get();
const user = userDoc.data();
delete user.auth;
return user || new ValidationError("User ID not found");
} catch (e) {
throw new ApolloError(e);
}
},
async group(_, args, context, info) {
try {
await auth(context.token, admin);
await rateLimit(_, args, context, info);
const groupDoc = await admin
.firestore()
.doc(`groups/${args.id}`)
.get();
const group = groupDoc.data();
return group || new ValidationError("Group ID not found");
} catch (e) {
throw new ApolloError(e);
}
},
},
Mutation: {
async login(_, args, context, info) {
try {
await rateLimit(_, args, context, info);
let userDoc = await admin
.firestore()
.collection("users")
.where("email", "==", args.email)
.limit(1)
.get();
if (userDoc.empty)
throw new Error(`Could not find ${args.email}.`);
userDoc = userDoc.docs.map((val) => val.data());
if (!userDoc || userDoc.length != 1)
throw new Error(`Could not find ${args.email}.`);
userDoc = userDoc[0];
const salt = userDoc.auth.salt; // salt to add
const password = userDoc.auth.password; // should have salt already added
args.password = salt + args.password; // plaintext
const isEqual = await bcrypt.compare(args.password, password);
if (!isEqual)
throw new AuthenticationError("Incorrect Password");
const token = await createToken(
admin,
userDoc.email,
userDoc.id
);
return token;
} catch (e) {
throw new ApolloError(e);
}
},
async signUp(_, args, context, info) {
try {
const salt = [...Array(6)]
.map(() => Math.floor(Math.random() * 16).toString(16))
.join("");
const objToAdd = {
id: args.username,
username: args.username,
name: args.name,
email: args.email,
auth: {
password: args.password,
salt: salt,
},
mainGroup: null,
groups: [],
adminOf: [],
ownerOf: [],
};
await validate(objToAdd, admin);
objToAdd.auth.password = await bcrypt.hash(salt + objToAdd.auth.password, 10)
const set = await admin
.firestore()
.collection("users")
.doc(objToAdd.id)
.set(objToAdd, { merge: true });
const token = await createToken(
admin,
objToAdd.email,
objToAdd.id
);
return token;
} catch (e) {
throw new ApolloError(e);
}
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({ req }) => ({
token: req.get("Authorization"),
}),
validationRules: [depthLimit(10)],
introspection: process.env.NODE_ENV !== "production",
formatError: (err) => {
if (err.message.startsWith("Database Error: ")) {
return new Error("Internal server error");
}
return err;
},
});
server.listen().then(({ url }) => {
console.log(url);
});