Skip to content

Commit 421d06d

Browse files
Merge pull request #13 from aayush-05/addBetterAuthentication
Add User and Organization API
2 parents 06486db + 88dcc64 commit 421d06d

File tree

17 files changed

+1024
-87
lines changed

17 files changed

+1024
-87
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
package-lock.json
3+
.env

example.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DATABASE_URL="mongodb://<username>:<password>@cluster0-shard-00-00.t4hho.mongodb.net:27017,cluster0-shard-00-01.t4hho.mongodb.net:27017,cluster0-shard-00-02.t4hho.mongodb.net:27017/<dbname>?ssl=true&replicaSet=atlas-a80apd-shard-0&authSource=admin&retryWrites=true&w=majority"
2+
JWT_SECRET = "somesupersecretkey"
3+
PORT=8000

graphql/resolvers/auth.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,6 @@ require("dotenv").config();
44
const User = require("../../models/user");
55

66
module.exports = {
7-
createUser: async (args) => {
8-
try {
9-
const existingUser = await User.findOne({ email: args.userInput.email });
10-
if (existingUser) {
11-
throw Error("User is already registered");
12-
}
13-
const user = new User({
14-
email: args.userInput.email,
15-
password: args.userInput.password,
16-
username: args.userInput.username,
17-
});
18-
const saveUser = await user.save();
19-
return { ...saveUser._doc };
20-
} catch (err) {
21-
console.log(err);
22-
throw err;
23-
}
24-
},
257
login: async (args) => {
268
try {
279
const user = await User.findOne({ email: args.email });
@@ -34,9 +16,7 @@ module.exports = {
3416
}
3517
const token = jwt.sign(
3618
{
37-
userId: user.id,
38-
email: user.email,
39-
username: user.username,
19+
id: user.id,
4020
},
4121
`${process.env.JWT_SECRET}`,
4222
{
@@ -45,8 +25,8 @@ module.exports = {
4525
}
4626
);
4727
return {
48-
userId: user.id,
49-
username: user.username,
28+
id: user.id,
29+
name: user.name,
5030
token: token,
5131
tokenexpiration: 1,
5232
};

graphql/resolvers/errorMessages.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//user
2+
module.exports.authenticationError = "Authentication Required";
3+
module.exports.userExistError = "User is already registered";
4+
module.exports.noUserError = "User not registered";
5+
module.exports.adminAccessError = "Admin authorization required";
6+
module.exports.noAdminError = "User is not an Admin"
7+
module.exports.noModeratorError = "User is not a Moderator";
8+
module.exports.firstAdminDemoteError = "First Admin can't be demoted";
9+
module.exports.firstAdminBlockError = "First Admin can't be blocked";
10+
module.exports.firstAdminRemoveError = "First Admin can't be removed";
11+
12+
//organization
13+
module.exports.noOrganizationError = "Organization to be created first";
14+
module.exports.organizationExistError = "Organization can be created only once";

graphql/resolvers/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
const topicResolver = require("./topic");
2-
const categoryResolver = require("./category");
31
const authResolver = require("./auth");
4-
const messageResolver = require("./message");
2+
const userResolver = require("./user");
3+
const organizationResolver = require("./organization");
54

65
const rootResolver = {
7-
...messageResolver,
8-
...topicResolver,
9-
...categoryResolver,
6+
...organizationResolver,
7+
...userResolver,
108
...authResolver,
119
};
1210

graphql/resolvers/organization.js

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
require("dotenv").config();
2+
const User = require("../../models/user");
3+
const Organization = require("../../models/organization");
4+
const {
5+
organizationCreatedResult,
6+
madeAdminResult,
7+
madeModeratorResult,
8+
removeAdminResult,
9+
removeModeratorResult } = require("./resultMessages");
10+
const { authenticationError,
11+
adminAccessError,
12+
noUserError,
13+
organizationExistError,
14+
firstAdminDemoteError,
15+
noAdminError,
16+
noModeratorError } = require("./errorMessages");
17+
18+
module.exports = {
19+
createOrganization: async (args) => {
20+
try {
21+
const organizations = await Organization.find({}).lean();
22+
if (organizations.length === 0) {
23+
const organization = new Organization({
24+
name: args.organizationInput.name,
25+
description: args.organizationInput.description,
26+
contactInfo: args.organizationInput.contactInfo,
27+
});
28+
await organization.save();
29+
return { result: organizationCreatedResult };
30+
} else {
31+
throw new Error(organizationExistError);
32+
}
33+
} catch (err) {
34+
console.log(err);
35+
throw err;
36+
}
37+
},
38+
39+
getOrganization: async () => {
40+
try {
41+
const organization = await Organization.findOne().lean();
42+
return organization;
43+
} catch (err) {
44+
console.log(err);
45+
throw err;
46+
}
47+
},
48+
49+
updateOrganization: async (req, args) => {
50+
if (!req.isAuth) {
51+
throw new Error(authenticationError);
52+
}
53+
try {
54+
const organizations = await Organization.find({}).lean();
55+
if (req.currentUser.isAdmin && organizations.length !== 0) {
56+
const organization = await Organization.updateOne(
57+
{},
58+
{
59+
$set: {
60+
name: args.organizationInput.name,
61+
description: args.organizationInput.description,
62+
contactInfo: args.organizationInput.contactInfo,
63+
},
64+
}
65+
);
66+
return { ...organization._doc };
67+
} else {
68+
throw new Error(adminAccessError);
69+
}
70+
} catch (err) {
71+
console.log(err);
72+
throw err;
73+
}
74+
},
75+
76+
makeAdmin: async (req, args) => {
77+
if (!req.isAuth) {
78+
throw new Error(authenticationError);
79+
}
80+
try {
81+
if (req.currentUser.isAdmin) {
82+
let user;
83+
if (args.userFindInput.email) {
84+
user = await User.findOne({ email: args.userFindInput.email });
85+
} else if (args.userFindInput._id) {
86+
user = await User.findById(args.userFindInput._id);
87+
}
88+
if (!user) {
89+
throw new Error(noUserError);
90+
}
91+
user.isAdmin = true;
92+
await user.save();
93+
const organization = await Organization.findOne({});
94+
organization.adminInfo.adminIds.push(user);
95+
await organization.save();
96+
return { result: madeAdminResult };
97+
} else {
98+
throw new Error(adminAccessError);
99+
}
100+
} catch (err) {
101+
console.log(err);
102+
throw err;
103+
}
104+
},
105+
106+
makeModerator: async (req, args) => {
107+
if (!req.isAuth) {
108+
throw new Error(authenticationError);
109+
}
110+
try {
111+
if (req.currentUser.isAdmin) {
112+
let user;
113+
if (args.userFindInput.email) {
114+
user = await User.findOne({ email: args.userFindInput.email });
115+
} else if (args.userFindInput._id) {
116+
user = await User.findById(args.userFindInput._id);
117+
}
118+
if (!user) {
119+
throw new Error(noUserError);
120+
}
121+
user.isModerator = true;
122+
await user.save();
123+
const organization = await Organization.findOne({});
124+
organization.moderatorInfo.moderatorIds.push(user);
125+
await organization.save();
126+
return {
127+
result: madeModeratorResult,
128+
};
129+
} else {
130+
throw new Error(adminAccessError);
131+
}
132+
} catch (err) {
133+
console.log(err);
134+
throw err;
135+
}
136+
},
137+
138+
removeAdmin: async (req, args) => {
139+
if (!req.isAuth) {
140+
throw new Error(authenticationError);
141+
}
142+
console.log(args.userFindInput);
143+
try {
144+
if (req.currentUser.isAdmin) {
145+
let user;
146+
if (args.userFindInput.email) {
147+
user = await User.findOne({ email: args.userFindInput.email });
148+
} else if (args.userFindInput._id) {
149+
user = await User.findById(args.userFindInput._id);
150+
}
151+
if (!user) {
152+
throw new Error(noUserError);
153+
}
154+
if (user.isFirstAdmin) {
155+
throw new Error(firstAdminDemoteError);
156+
}
157+
if(!user.isAdmin) {
158+
throw new Error(noAdminError);
159+
}
160+
user.isAdmin = false;
161+
user.isModerator = false;
162+
await user.save();
163+
const organization = await Organization.findOne({});
164+
console.log(organization.adminInfo.adminIds);
165+
organization.adminInfo.adminIds = organization.adminInfo.adminIds.filter(
166+
(adminId) => adminId.toString() !== user.id
167+
);
168+
await organization.save();
169+
return { result: removeAdminResult };
170+
} else {
171+
throw new Error(adminAccessError);
172+
}
173+
} catch (err) {
174+
console.log(err);
175+
throw err;
176+
}
177+
},
178+
179+
removeModerator: async (req, args) => {
180+
if (!req.isAuth) {
181+
throw new Error(authenticationError);
182+
}
183+
try {
184+
if (req.currentUser.isAdmin) {
185+
let user;
186+
if (args.email) {
187+
user = await User.findOne({ email: args.userFindInput.email });
188+
} else if (args.userFindInput._id) {
189+
user = await User.findById(args.userFindInput._id);
190+
}
191+
if (!user) {
192+
throw new Error(noUserError);
193+
}
194+
if (!user.isModerator) {
195+
throw new Error(noModeratorError);
196+
}
197+
user.isModerator = false;
198+
await user.save();
199+
const organization = await Organization.findOne({});
200+
organization.moderatorInfo.moderatorIds = organization.moderatorInfo.moderatorIds.filter(
201+
(moderatorId) => moderatorId.toString() !== user.id
202+
);
203+
await organization.save();
204+
return {
205+
result: removeModeratorResult,
206+
};
207+
} else {
208+
throw new Error(adminAccessError);
209+
}
210+
} catch (err) {
211+
console.log(err);
212+
throw err;
213+
}
214+
},
215+
216+
getAdminModerators: async (req) => {
217+
if (!req.isAuth) {
218+
throw new Error(authenticationError);
219+
}
220+
try {
221+
if (req.currentUser.isAdmin) {
222+
const organization = await Organization.findOne({}).populate();
223+
return {
224+
admins: organization.adminInfo.adminIds,
225+
moderators: organization.moderatorInfo.moderatorIds,
226+
};
227+
} else {
228+
throw new Error(adminAccessError);
229+
}
230+
} catch (err) {
231+
console.log(err);
232+
throw err;
233+
}
234+
},
235+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//user
2+
module.exports.userBlockResult = "User blocked successfully";
3+
module.exports.userRemoveResult = "User removed successfully";
4+
5+
//organization
6+
module.exports.organizationCreatedResult = "Organization created successfully";
7+
module.exports.madeAdminResult =
8+
"User promoted to Admin authorization successfully";
9+
module.exports.madeModeratorResult =
10+
"User promoted to Moderator authorization successfully";
11+
module.exports.removeAdminResult =
12+
"Admin demoted to User authorization successfully";
13+
module.exports.removeModeratorResult =
14+
"Moderator demoted to User authorization successfully";

0 commit comments

Comments
 (0)