Skip to content

Commit c4bae4c

Browse files
Reame file ande some admin valiadation
1 parent 653e797 commit c4bae4c

File tree

2 files changed

+78
-19
lines changed

2 files changed

+78
-19
lines changed

README.md

+56-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,62 @@
1717

1818
* This API is return all customer's list if the following conditions are matched
1919
* Method : GET
20+
* If user role is admin
2021
* If user is loggedIn then pass JWT token into header
21-
Like: https://drive.google.com/file/d/1Zu2J8KOgQIaAvjcqwvPZMq_Ol7gRemHz/view
22+
Likes: https://drive.google.com/file/d/1Zu2J8KOgQIaAvjcqwvPZMq_Ol7gRemHz/view
2223
* If user is not loggedIn then this will return following response
23-
Link: https://drive.google.com/file/d/1hTq_3DeTljQP0KGzhCU0fYS9NtBNWlTu/view?usp=drivesdk
24+
Links: https://drive.google.com/file/d/1hTq_3DeTljQP0KGzhCU0fYS9NtBNWlTu/view?usp=drivesdk
2425

25-
*
26-
26+
* http://localhost:3000/auth/register
27+
* This API is used for register customer using following condition's
28+
* Method : PUT (reason for put methods here because we want to insert new resource )
29+
* In this API just pass header Content-Type: application/json or whatever you want in to response
30+
* The purpose to use this API is to register user with the specific role like : user,admin
31+
* Please check Restlet Client schreenshot for more clarification
32+
Links: https://drive.google.com/file/d/1urhWZ7WmhViCsKUzNB6xd9ghC7Sg93l1/view?usp=drivesdk
33+
34+
* http://localhost:3000/auth/login
35+
* This API is used for login of the customer using the following condition
36+
* Pass email/password of the customer header Content-Type: application/json or whatever you want in to response.
37+
* If the user is registered user then this will return JWT token with message : "Success"
38+
* If the user is not found then simply return message": "User not found."
39+
40+
* http://localhost:3000/getUserDetails
41+
* Method : GET
42+
* This API return user details when user pass JWT token and email id of the customer based on the email id the data is return
43+
* This API is return customer details only when the role of the user is admin
44+
* Pass emailId from query parameters
45+
Links: https://drive.google.com/file/d/1Fw03ZzG60J8hkWfzV7hv4q0wsOLjQIwm/view?usp=drivesdk
46+
47+
* http://localhost:3000/updateUser
48+
* Method : POST
49+
* Same condition's like getUserDetails API but where user and customer both can update data
50+
Links: https://drive.google.com/file/d/1CInwrTVvdcPLCXvmuaFO1V_SMUDwVxNq/view?usp=drivesdk
51+
52+
* http://localhost:3000/deleteUser
53+
* Method : POST
54+
* This API is use to delete all customer from database
55+
* The role of the user should be admin
56+
* This just return { message: 'Customer record successfully deleted' } if user is admin otherwise
57+
{ message: "Unauthorised access" }
58+
59+
60+
Code Explanation :
61+
62+
* Middleware
63+
* The purpose to add middleware here is to check if the user is passing JWT token or not
64+
Before calling API
65+
* If user passing JWT token then we assign the user is defined
66+
* If user does not passing the JWT token the we assign the user is undefined
67+
Links: https://drive.google.com/file/d/1nGQlXKOLxDuZSyUbsd34AsuWfHmgOnpU/view?usp=drivesdk
68+
69+
* Routes:
70+
* Check this links https://drive.google.com/file/d/1Oq-mtwhjhXxbd0PIFcRtO_klAYhsBE7A/view?usp=drivesdk
71+
for more information
72+
* As you can see there is one function is called on most of API routes which is userHandlers.loginRequired
73+
the purpose to user this function is that we want to disallow customer to access these API
74+
directly these API is only and only access by the user if the user is login
75+
76+
Thanks for reading this document hope you enjoy this documents
77+
feel free to ask if you have any doubts
78+

api/controllers/userController.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,13 @@ exports.login = function (req, res) {
2424
User.findOne({
2525
email: req.body.email
2626
}, function (err, user) {
27-
// console.log("User details");
28-
// console.log(user);
2927
if (err) throw err;
3028
if (!user) {
3129
res.status(401).json({ message: 'User not found.' });
3230
} else if (user) {
3331
if (!user.comparePassword(req.body.password)) {
3432
res.status(401).json({ message: 'Please enter valid password' });
3533
} else {
36-
console.log("Login User details");
37-
console.log(user);
3834
return res.json({ token: jwt.sign({ _id: user._id, role: user.role, userName: user.userName }, 'schoolcom'), message: "success" });
3935
}
4036
}
@@ -50,21 +46,32 @@ exports.loginRequired = function (req, res, next) {
5046
};
5147

5248
exports.createUser = function (req, res) {
53-
var newUser = new User(req.body);
54-
newUser.save(function (err, user) {
55-
if (err)
56-
res.send(err);
57-
res.json(user);
58-
});
49+
console.log(req.user.role);
50+
if(req.user.role == 'admin'){
51+
var newUser = new User(req.body);
52+
newUser.save(function (err, user) {
53+
if (err)
54+
res.send(err);
55+
res.json(user);
56+
});
57+
}else{
58+
res.json({message: "Unauthorised access"});
59+
}
60+
5961
};
6062

6163
exports.getUserDetails = function (req, res) {
6264
console.log(req.query.email);
63-
User.findOne({ email: req.query.email }, function (err, user) {
64-
if (err)
65-
res.send(err);
66-
res.json(user);
67-
});
65+
if(req.user.role == 'admin'){
66+
User.findOne({ email: req.query.email }, function (err, user) {
67+
if (err)
68+
res.send(err);
69+
res.json(user);
70+
});
71+
}else{
72+
res.json({message: "Unauthorised access"});
73+
}
74+
6875
};
6976

7077
exports.updateUser = function (req, res) {

0 commit comments

Comments
 (0)