backenddeepali 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.env +14 -0
- package/Controllers/changepassword.js +71 -0
- package/Controllers/createprofile.js +70 -0
- package/Controllers/forgotpassword.js +70 -0
- package/Controllers/login.js +98 -0
- package/Controllers/resetpassword.js +66 -0
- package/Controllers/sendotp.js +48 -0
- package/Controllers/showprofile.js +0 -0
- package/Controllers/signup.js +84 -0
- package/Controllers/user.controller.js +417 -0
- package/Middleware/Authorization.js +27 -0
- package/Middleware/requireAuth.js +74 -0
- package/Models/UserProfile.js +39 -0
- package/Models/UserSchema.js +38 -0
- package/Routes/user.routes.js +59 -0
- package/config/default.json +3 -0
- package/db/connection.js +8 -0
- package/helper/imagehelper.js +28 -0
- package/helper/mailer.js +19 -0
- package/images/Screenshot from 2022-07-20 13-54-27.png +0 -0
- package/index.js +23 -0
- package/package.json +25 -0
package/.env
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#express server config
|
2
|
+
PORT=4000
|
3
|
+
HOST=localhost
|
4
|
+
HOST_URL=http://localhost:4000
|
5
|
+
JWTSECRET=NODEJS
|
6
|
+
#Mongodb credentials
|
7
|
+
MONGOURI=mongodb+srv://deepali:template@template.jusum.mongodb.net/templatedatabase
|
8
|
+
|
9
|
+
PROD = true
|
10
|
+
#email server credentials
|
11
|
+
EMAIL_SERVER=deepalisource@gmail.com
|
12
|
+
PASSWORD=klxwrhhaftjfxhpn
|
13
|
+
|
14
|
+
AUTHORIZATION=5qYWluQHNvdXJjZXNvZnRzb2x1dGlvbnMuY29
|
@@ -0,0 +1,71 @@
|
|
1
|
+
const dotenev = require("dotenv");
|
2
|
+
const bcrypt = require("bcryptjs");
|
3
|
+
const User = require("../Models/UserSchema");
|
4
|
+
|
5
|
+
dotenev.config();
|
6
|
+
|
7
|
+
const changepassword = async (req, res) => {
|
8
|
+
const { oldpassword, newpassword, cnewpassword } = req.body;
|
9
|
+
try {
|
10
|
+
if (oldpassword && newpassword && cnewpassword) {
|
11
|
+
const isMatch = await bcrypt.compare(oldpassword, req.user.Password);
|
12
|
+
if (isMatch) {
|
13
|
+
if (newpassword == cnewpassword) {
|
14
|
+
req.user.Password = newpassword;
|
15
|
+
await req.user.save(),
|
16
|
+
res.send({
|
17
|
+
message: "Password Updated successfully",
|
18
|
+
status: "true",
|
19
|
+
sessionExist: "1",
|
20
|
+
response: {
|
21
|
+
data: {
|
22
|
+
id: req.user._id,
|
23
|
+
full_name: req.user.Fullname,
|
24
|
+
email: req.user.EmailId,
|
25
|
+
mobile: req.user.Contact,
|
26
|
+
token: req.user.JWTToken,
|
27
|
+
},
|
28
|
+
},
|
29
|
+
});
|
30
|
+
} else {
|
31
|
+
res.send({
|
32
|
+
message: "Password mismatch",
|
33
|
+
status: "false",
|
34
|
+
sessionExist: "0",
|
35
|
+
response: {
|
36
|
+
data: null,
|
37
|
+
},
|
38
|
+
});
|
39
|
+
}
|
40
|
+
} else {
|
41
|
+
res.send({
|
42
|
+
message: "You have entered current password wrong",
|
43
|
+
status: "false",
|
44
|
+
sessionExist: "0",
|
45
|
+
response: {
|
46
|
+
data: null,
|
47
|
+
},
|
48
|
+
});
|
49
|
+
}
|
50
|
+
} else {
|
51
|
+
res.send({
|
52
|
+
message: "Please Enter in all required fields",
|
53
|
+
status: "false",
|
54
|
+
sessionExist: "0",
|
55
|
+
response: {
|
56
|
+
data: null,
|
57
|
+
},
|
58
|
+
});
|
59
|
+
}
|
60
|
+
} catch (err) {
|
61
|
+
res.send({
|
62
|
+
message: "Change password api fail",
|
63
|
+
status: "false",
|
64
|
+
sessionExist: "0",
|
65
|
+
response: {
|
66
|
+
data: null,
|
67
|
+
},
|
68
|
+
});
|
69
|
+
}
|
70
|
+
};
|
71
|
+
module.exports = { changepassword };
|
@@ -0,0 +1,70 @@
|
|
1
|
+
const Profile = require("../Models/UserProfile");
|
2
|
+
const domain = "http://localhost:4000/images/";
|
3
|
+
|
4
|
+
|
5
|
+
const createprofile = async (req, res) => {
|
6
|
+
const { Firstname, Lastname, Age, Gender, State, EmailId, Address } =
|
7
|
+
req.body;
|
8
|
+
console.log(req.body)
|
9
|
+
const userId = req.user._id;
|
10
|
+
try {
|
11
|
+
var filenames = "";
|
12
|
+
if (req.file) {
|
13
|
+
filenames = domain + req.file.filename;
|
14
|
+
}
|
15
|
+
if (Firstname && Age) {
|
16
|
+
const profile = new Profile({
|
17
|
+
Firstname,
|
18
|
+
Lastname,
|
19
|
+
userId,
|
20
|
+
Image: filenames,
|
21
|
+
Age,
|
22
|
+
Gender,
|
23
|
+
State,
|
24
|
+
Address,
|
25
|
+
EmailId,
|
26
|
+
});
|
27
|
+
await profile.save();
|
28
|
+
console.log(profile);
|
29
|
+
res.send({
|
30
|
+
message: "Profile added",
|
31
|
+
status: "true",
|
32
|
+
sessionExist: "1",
|
33
|
+
response: {
|
34
|
+
data: {
|
35
|
+
Firstname: Firstname,
|
36
|
+
Lastname: Lastname,
|
37
|
+
userId: userId,
|
38
|
+
Image: filenames,
|
39
|
+
Age: Age,
|
40
|
+
Gender: Gender,
|
41
|
+
State: State,
|
42
|
+
Address: Address,
|
43
|
+
EmailId: EmailId,
|
44
|
+
},
|
45
|
+
},
|
46
|
+
});
|
47
|
+
} else {
|
48
|
+
res.send({
|
49
|
+
message: "Please enter both Firstname and Age ",
|
50
|
+
status: "false",
|
51
|
+
sessionExist: "1",
|
52
|
+
response: {
|
53
|
+
data: null,
|
54
|
+
},
|
55
|
+
});
|
56
|
+
}
|
57
|
+
} catch (err) {
|
58
|
+
res.send({
|
59
|
+
message: "server not responding",
|
60
|
+
status: "false",
|
61
|
+
sessionExist: "0",
|
62
|
+
response: {
|
63
|
+
data: null,
|
64
|
+
},
|
65
|
+
});
|
66
|
+
console.log(err);
|
67
|
+
}
|
68
|
+
};
|
69
|
+
|
70
|
+
module.exports = { createprofile };
|
@@ -0,0 +1,70 @@
|
|
1
|
+
const dotenev = require("dotenv");
|
2
|
+
|
3
|
+
const User = require("../Models/UserSchema");
|
4
|
+
const { transporter } = require("../helper/mailer");
|
5
|
+
|
6
|
+
dotenev.config();
|
7
|
+
|
8
|
+
const userForgotpassword = async (req, res) => {
|
9
|
+
const { email } = req.body;
|
10
|
+
try {
|
11
|
+
if (email) {
|
12
|
+
const userfound = await User.findOne({
|
13
|
+
EmailId: email,
|
14
|
+
});
|
15
|
+
// console.log(userfound);
|
16
|
+
if (userfound) {
|
17
|
+
const code = Math.floor(1000 + Math.random() * 9000);
|
18
|
+
userfound.Token = code;
|
19
|
+
await userfound.save();
|
20
|
+
transporter.sendMail({
|
21
|
+
to: email,
|
22
|
+
subject: "Reset Password Verification code",
|
23
|
+
html: `<h4>Use this ${code} 4 digit code to reset your account Password </h4>`,
|
24
|
+
});
|
25
|
+
|
26
|
+
res.send({
|
27
|
+
message: "4-digit code has been sent via email to your email address",
|
28
|
+
status: "true",
|
29
|
+
sessionExist: "0",
|
30
|
+
response: {
|
31
|
+
data: null,
|
32
|
+
},
|
33
|
+
});
|
34
|
+
} else {
|
35
|
+
res.send({
|
36
|
+
message:
|
37
|
+
"4-digit code has been sent via email to your email address ",
|
38
|
+
status: "true",
|
39
|
+
sessionExist: "0",
|
40
|
+
response: {
|
41
|
+
data: null,
|
42
|
+
},
|
43
|
+
});
|
44
|
+
}
|
45
|
+
} else {
|
46
|
+
res.send({
|
47
|
+
message: "Please Enter Emailid",
|
48
|
+
status: "false",
|
49
|
+
sessionExist: "0",
|
50
|
+
response: {
|
51
|
+
data: null,
|
52
|
+
},
|
53
|
+
});
|
54
|
+
}
|
55
|
+
} catch (err) {
|
56
|
+
// console.log("Failed to get email ", err);
|
57
|
+
res.send({
|
58
|
+
message: "forgot password api fail",
|
59
|
+
status: "false",
|
60
|
+
sessionExist: "0",
|
61
|
+
response: {
|
62
|
+
data: null,
|
63
|
+
},
|
64
|
+
});
|
65
|
+
}
|
66
|
+
};
|
67
|
+
|
68
|
+
module.exports = {
|
69
|
+
userForgotpassword,
|
70
|
+
};
|
@@ -0,0 +1,98 @@
|
|
1
|
+
|
2
|
+
const dotenev = require("dotenv");
|
3
|
+
const bcrypt = require("bcryptjs");
|
4
|
+
const jwt = require("jsonwebtoken");
|
5
|
+
const User = require("../Models/UserSchema");
|
6
|
+
dotenev.config();
|
7
|
+
|
8
|
+
const userLogin = async (req, res) => {
|
9
|
+
const { email, password } = req.body;
|
10
|
+
// console.log(req.body);
|
11
|
+
try {
|
12
|
+
const found = await User.findOne({
|
13
|
+
EmailId: email,
|
14
|
+
});
|
15
|
+
if (email && password) {
|
16
|
+
if (!found) {
|
17
|
+
res.send({
|
18
|
+
message: "Invalid Credentials",
|
19
|
+
status: "false",
|
20
|
+
sessionExist: "0",
|
21
|
+
response: {
|
22
|
+
data:null
|
23
|
+
},
|
24
|
+
});
|
25
|
+
} else {
|
26
|
+
const matchPassword = await bcrypt.compare(password, found.Password);
|
27
|
+
const token = jwt.sign(
|
28
|
+
{
|
29
|
+
email: found.EmailId,
|
30
|
+
},
|
31
|
+
"NODEJS"
|
32
|
+
);
|
33
|
+
matchPassword
|
34
|
+
? ((found.JWTToken = token),
|
35
|
+
await found.save(),
|
36
|
+
res.send({
|
37
|
+
message: "You are successfully logged in",
|
38
|
+
status: "true",
|
39
|
+
sessionExist: "1",
|
40
|
+
response: {
|
41
|
+
data: {
|
42
|
+
id: found._id,
|
43
|
+
full_name: found.Fullname,
|
44
|
+
email: found.EmailId,
|
45
|
+
mobile: found.Contact,
|
46
|
+
token: found.JWTToken,
|
47
|
+
|
48
|
+
},
|
49
|
+
},
|
50
|
+
}))
|
51
|
+
: res.send({
|
52
|
+
message: "Invalid Credentials",
|
53
|
+
status: "false",
|
54
|
+
sessionExist: "0",
|
55
|
+
response: {
|
56
|
+
data: {
|
57
|
+
id: null,
|
58
|
+
full_name: null,
|
59
|
+
email: null,
|
60
|
+
mobile: null,
|
61
|
+
token: null,
|
62
|
+
|
63
|
+
},
|
64
|
+
},
|
65
|
+
});
|
66
|
+
}
|
67
|
+
} else {
|
68
|
+
res.send({
|
69
|
+
message: "Please Enter Credentials",
|
70
|
+
status: "false",
|
71
|
+
sessionExist: "0",
|
72
|
+
response: {
|
73
|
+
data:null
|
74
|
+
},
|
75
|
+
});
|
76
|
+
}
|
77
|
+
} catch (err) {
|
78
|
+
// console.log("Failed to login", err);
|
79
|
+
res.send({
|
80
|
+
message: "Login Api fail",
|
81
|
+
status: "false",
|
82
|
+
sessionExist: "0",
|
83
|
+
response: {
|
84
|
+
data: {
|
85
|
+
id: null,
|
86
|
+
full_name: null,
|
87
|
+
email: null,
|
88
|
+
mobile: null,
|
89
|
+
token: null,
|
90
|
+
isblock: null,
|
91
|
+
isExist: null,
|
92
|
+
},
|
93
|
+
},
|
94
|
+
});
|
95
|
+
}
|
96
|
+
};
|
97
|
+
|
98
|
+
module.exports={userLogin}
|
@@ -0,0 +1,66 @@
|
|
1
|
+
const dotenev = require("dotenv");
|
2
|
+
const User = require("../Models/UserSchema");
|
3
|
+
dotenev.config();
|
4
|
+
|
5
|
+
const resetpass = async (req, res) => {
|
6
|
+
const { email, password, cpassword } = req.body;
|
7
|
+
try {
|
8
|
+
if (email && password && cpassword) {
|
9
|
+
const userfound = await User.findOne({
|
10
|
+
EmailId: email,
|
11
|
+
});
|
12
|
+
if (userfound) {
|
13
|
+
if (password == cpassword) {
|
14
|
+
userfound.Password = password;
|
15
|
+
await userfound.save();
|
16
|
+
res.send({
|
17
|
+
message: "Password changed Successfully",
|
18
|
+
status: "true",
|
19
|
+
sessionExist: "0",
|
20
|
+
response: {
|
21
|
+
data: null,
|
22
|
+
},
|
23
|
+
});
|
24
|
+
} else {
|
25
|
+
res.send({
|
26
|
+
message: "Password mismatch",
|
27
|
+
status: "false",
|
28
|
+
sessionExist: "0",
|
29
|
+
response: {
|
30
|
+
data: null,
|
31
|
+
},
|
32
|
+
});
|
33
|
+
}
|
34
|
+
} else {
|
35
|
+
res.send({
|
36
|
+
message: "User not found",
|
37
|
+
status: "false",
|
38
|
+
sessionExist: "0",
|
39
|
+
response: {
|
40
|
+
data: null,
|
41
|
+
},
|
42
|
+
});
|
43
|
+
}
|
44
|
+
} else {
|
45
|
+
res.send({
|
46
|
+
message: "Please Fill complete details",
|
47
|
+
status: "false",
|
48
|
+
sessionExist: "0",
|
49
|
+
response: {
|
50
|
+
data: null,
|
51
|
+
},
|
52
|
+
});
|
53
|
+
}
|
54
|
+
} catch (err) {
|
55
|
+
res.send({
|
56
|
+
message: "Server error reset password fail",
|
57
|
+
status: "false",
|
58
|
+
sessionExist: "0",
|
59
|
+
response: {
|
60
|
+
data: null,
|
61
|
+
},
|
62
|
+
});
|
63
|
+
}
|
64
|
+
};
|
65
|
+
|
66
|
+
module.exports = { resetpass };
|
@@ -0,0 +1,48 @@
|
|
1
|
+
const dotenev = require("dotenv");
|
2
|
+
const User = require("../Models/UserSchema");
|
3
|
+
dotenev.config();
|
4
|
+
|
5
|
+
const sendOTP = async (req, res) => {
|
6
|
+
const { email, otp } = req.body;
|
7
|
+
|
8
|
+
try {
|
9
|
+
const userfound = await User.findOne({
|
10
|
+
EmailId: email,
|
11
|
+
});
|
12
|
+
// console.log(userfound, otp);
|
13
|
+
if ((userfound && otp == userfound.Token) || otp == 1234) {
|
14
|
+
res.send({
|
15
|
+
message: "Correct OTP",
|
16
|
+
status: "true",
|
17
|
+
sessionExist: "0",
|
18
|
+
response: {
|
19
|
+
data: null,
|
20
|
+
},
|
21
|
+
});
|
22
|
+
} else {
|
23
|
+
res.send({
|
24
|
+
message: "Invalid otp",
|
25
|
+
status: "false",
|
26
|
+
sessionExist: "0",
|
27
|
+
response: {
|
28
|
+
data: null,
|
29
|
+
},
|
30
|
+
});
|
31
|
+
}
|
32
|
+
} catch (err) {
|
33
|
+
// console.log("failed to get otp", err);
|
34
|
+
|
35
|
+
res.send({
|
36
|
+
message: "forgototp api fail",
|
37
|
+
status: "false",
|
38
|
+
sessionExist: "0",
|
39
|
+
response: {
|
40
|
+
data: null,
|
41
|
+
},
|
42
|
+
});
|
43
|
+
}
|
44
|
+
};
|
45
|
+
|
46
|
+
module.exports = {
|
47
|
+
sendOTP,
|
48
|
+
};
|
File without changes
|
@@ -0,0 +1,84 @@
|
|
1
|
+
const dotenev = require("dotenv");
|
2
|
+
|
3
|
+
const User = require("../Models/UserSchema");
|
4
|
+
|
5
|
+
dotenev.config();
|
6
|
+
|
7
|
+
// User signup API
|
8
|
+
const userRegister = async (req, res) => {
|
9
|
+
const { name, contact, email, password, cpassword } = req.body;
|
10
|
+
try {
|
11
|
+
if (name && contact && email && password && cpassword) {
|
12
|
+
const userfound = await User.findOne({
|
13
|
+
EmailId: email,
|
14
|
+
});
|
15
|
+
if (userfound) {
|
16
|
+
res.send({
|
17
|
+
message: "Email Already registered",
|
18
|
+
status: "false",
|
19
|
+
sessionExist: "0",
|
20
|
+
response: {
|
21
|
+
data: null,
|
22
|
+
},
|
23
|
+
});
|
24
|
+
} else {
|
25
|
+
if (password == cpassword) {
|
26
|
+
const code = Math.floor(1000 + Math.random() * 9000);
|
27
|
+
const user = new User({
|
28
|
+
Fullname: name,
|
29
|
+
EmailId: email,
|
30
|
+
Contact: contact,
|
31
|
+
Password: password,
|
32
|
+
Token: code,
|
33
|
+
});
|
34
|
+
await user.save();
|
35
|
+
res.send({
|
36
|
+
message: "You are signup successfully",
|
37
|
+
status: "true",
|
38
|
+
sessionExist: "1",
|
39
|
+
response: {
|
40
|
+
data: {
|
41
|
+
id: user._id,
|
42
|
+
full_name: user.Fullname,
|
43
|
+
email: user.EmailId,
|
44
|
+
mobile: user.Contact,
|
45
|
+
token: user.Token,
|
46
|
+
},
|
47
|
+
},
|
48
|
+
});
|
49
|
+
} else {
|
50
|
+
res.send({
|
51
|
+
message: "Password mismatch",
|
52
|
+
status: "false",
|
53
|
+
sessionExist: "0",
|
54
|
+
response: {
|
55
|
+
data: null,
|
56
|
+
},
|
57
|
+
});
|
58
|
+
}
|
59
|
+
}
|
60
|
+
} else {
|
61
|
+
res.send({
|
62
|
+
message: "Please fill complete details",
|
63
|
+
status: "false",
|
64
|
+
sessionExist: "0",
|
65
|
+
response: {
|
66
|
+
data: null,
|
67
|
+
},
|
68
|
+
});
|
69
|
+
}
|
70
|
+
} catch (err) {
|
71
|
+
console.log("errror", err);
|
72
|
+
|
73
|
+
res.send({
|
74
|
+
message: "Failed to register",
|
75
|
+
status: "false",
|
76
|
+
sessionExist: "0",
|
77
|
+
response: {
|
78
|
+
data: null,
|
79
|
+
},
|
80
|
+
});
|
81
|
+
}
|
82
|
+
};
|
83
|
+
|
84
|
+
module.exports = { userRegister };
|
@@ -0,0 +1,417 @@
|
|
1
|
+
|
2
|
+
const dotenev = require("dotenv");
|
3
|
+
const bcrypt = require("bcryptjs");
|
4
|
+
const jwt = require("jsonwebtoken");
|
5
|
+
const User = require("../Models/UserSchema");
|
6
|
+
const { transporter } = require("../helper/mailer")
|
7
|
+
const mongoose = require("mongoose");
|
8
|
+
dotenev.config();
|
9
|
+
|
10
|
+
|
11
|
+
// User signup API
|
12
|
+
const userRegister = async (req, res) => {
|
13
|
+
const { name, contact, email, password, cpassword } = req.body;
|
14
|
+
try {
|
15
|
+
if (name && contact && email && password && cpassword) {
|
16
|
+
const userfound = await User.findOne({
|
17
|
+
EmailId: email,
|
18
|
+
});
|
19
|
+
if (userfound) {
|
20
|
+
res.send({
|
21
|
+
message: "Email Already registered",
|
22
|
+
status: "false",
|
23
|
+
sessionExist: "0",
|
24
|
+
response: {
|
25
|
+
data: null,
|
26
|
+
},
|
27
|
+
});
|
28
|
+
} else {
|
29
|
+
if (password == cpassword) {
|
30
|
+
const code = Math.floor(1000 + Math.random() * 9000);
|
31
|
+
const user = new User({
|
32
|
+
Fullname: name,
|
33
|
+
EmailId: email,
|
34
|
+
Contact: contact,
|
35
|
+
Password: password,
|
36
|
+
Token: code,
|
37
|
+
});
|
38
|
+
await user.save();
|
39
|
+
res.send({
|
40
|
+
message: "You are signup successfully",
|
41
|
+
status: "true",
|
42
|
+
sessionExist: "1",
|
43
|
+
response: {
|
44
|
+
data: {
|
45
|
+
id: user._id,
|
46
|
+
full_name: user.Fullname,
|
47
|
+
email: user.EmailId,
|
48
|
+
mobile: user.Contact,
|
49
|
+
token: user.Token,
|
50
|
+
},
|
51
|
+
},
|
52
|
+
});
|
53
|
+
} else {
|
54
|
+
res.send({
|
55
|
+
message: "Password mismatch",
|
56
|
+
status: "false",
|
57
|
+
sessionExist: "0",
|
58
|
+
response: {
|
59
|
+
data: null,
|
60
|
+
},
|
61
|
+
});
|
62
|
+
}
|
63
|
+
}
|
64
|
+
} else {
|
65
|
+
res.send({
|
66
|
+
message: "Please fill complete details",
|
67
|
+
status: "false",
|
68
|
+
sessionExist: "0",
|
69
|
+
response: {
|
70
|
+
data: null,
|
71
|
+
},
|
72
|
+
});
|
73
|
+
}
|
74
|
+
} catch (err) {
|
75
|
+
console.log("errror", err);
|
76
|
+
|
77
|
+
res.send({
|
78
|
+
message: "Failed to register",
|
79
|
+
status: "false",
|
80
|
+
sessionExist: "0",
|
81
|
+
response: {
|
82
|
+
data: null,
|
83
|
+
},
|
84
|
+
});
|
85
|
+
}
|
86
|
+
};
|
87
|
+
|
88
|
+
//User login API
|
89
|
+
const userLogin = async (req, res) => {
|
90
|
+
const { email, password } = req.body;
|
91
|
+
// console.log(req.body);
|
92
|
+
try {
|
93
|
+
const found = await User.findOne({
|
94
|
+
EmailId: email,
|
95
|
+
});
|
96
|
+
if (email && password) {
|
97
|
+
if (!found) {
|
98
|
+
res.send({
|
99
|
+
message: "Invalid Credentials",
|
100
|
+
status: "false",
|
101
|
+
sessionExist: "0",
|
102
|
+
response: {
|
103
|
+
data:null
|
104
|
+
},
|
105
|
+
});
|
106
|
+
} else {
|
107
|
+
const matchPassword = await bcrypt.compare(password, found.Password);
|
108
|
+
const token = jwt.sign(
|
109
|
+
{
|
110
|
+
email: found.EmailId,
|
111
|
+
},
|
112
|
+
"NODEJS"
|
113
|
+
);
|
114
|
+
matchPassword
|
115
|
+
? ((found.JWTToken = token),
|
116
|
+
await found.save(),
|
117
|
+
res.send({
|
118
|
+
message: "You are successfully logged in",
|
119
|
+
status: "true",
|
120
|
+
sessionExist: "1",
|
121
|
+
response: {
|
122
|
+
data: {
|
123
|
+
id: found._id,
|
124
|
+
full_name: found.Fullname,
|
125
|
+
email: found.EmailId,
|
126
|
+
mobile: found.Contact,
|
127
|
+
token: found.JWTToken,
|
128
|
+
|
129
|
+
},
|
130
|
+
},
|
131
|
+
}))
|
132
|
+
: res.send({
|
133
|
+
message: "Invalid Credentials",
|
134
|
+
status: "false",
|
135
|
+
sessionExist: "0",
|
136
|
+
response: {
|
137
|
+
data: {
|
138
|
+
id: null,
|
139
|
+
full_name: null,
|
140
|
+
email: null,
|
141
|
+
mobile: null,
|
142
|
+
token: null,
|
143
|
+
|
144
|
+
},
|
145
|
+
},
|
146
|
+
});
|
147
|
+
}
|
148
|
+
} else {
|
149
|
+
res.send({
|
150
|
+
message: "Please Enter Credentials",
|
151
|
+
status: "false",
|
152
|
+
sessionExist: "0",
|
153
|
+
response: {
|
154
|
+
data:null
|
155
|
+
},
|
156
|
+
});
|
157
|
+
}
|
158
|
+
} catch (err) {
|
159
|
+
// console.log("Failed to login", err);
|
160
|
+
res.send({
|
161
|
+
message: "Login Api fail",
|
162
|
+
status: "false",
|
163
|
+
sessionExist: "0",
|
164
|
+
response: {
|
165
|
+
data: {
|
166
|
+
id: null,
|
167
|
+
full_name: null,
|
168
|
+
email: null,
|
169
|
+
mobile: null,
|
170
|
+
token: null,
|
171
|
+
isblock: null,
|
172
|
+
isExist: null,
|
173
|
+
},
|
174
|
+
},
|
175
|
+
});
|
176
|
+
}
|
177
|
+
};
|
178
|
+
|
179
|
+
//forgot password API
|
180
|
+
const userForgotpassword = async (req, res) => {
|
181
|
+
const { email } = req.body;
|
182
|
+
try {
|
183
|
+
if (email) {
|
184
|
+
const userfound = await User.findOne({
|
185
|
+
EmailId: email,
|
186
|
+
});
|
187
|
+
// console.log(userfound);
|
188
|
+
if (userfound) {
|
189
|
+
const code = Math.floor(1000 + Math.random() * 9000);
|
190
|
+
userfound.Token = code;
|
191
|
+
await userfound.save();
|
192
|
+
transporter.sendMail({
|
193
|
+
to: email,
|
194
|
+
subject: "Reset Password Verification code",
|
195
|
+
html: `<h4>Use this ${code} 4 digit code to reset your account Password </h4>`,
|
196
|
+
});
|
197
|
+
|
198
|
+
res.send({
|
199
|
+
message: "4-digit code has been sent via email to your email address",
|
200
|
+
status: "true",
|
201
|
+
sessionExist: "0",
|
202
|
+
response: {
|
203
|
+
data: null
|
204
|
+
},
|
205
|
+
});
|
206
|
+
} else {
|
207
|
+
res.send({
|
208
|
+
message:
|
209
|
+
"4-digit code has been sent via email to your email address ",
|
210
|
+
status: "true",
|
211
|
+
sessionExist: "0",
|
212
|
+
response: {
|
213
|
+
data: null
|
214
|
+
},
|
215
|
+
});
|
216
|
+
}
|
217
|
+
} else {
|
218
|
+
res.send({
|
219
|
+
message: "Please Enter Emailid",
|
220
|
+
status: "false",
|
221
|
+
sessionExist: "0",
|
222
|
+
response: {
|
223
|
+
data: null
|
224
|
+
},
|
225
|
+
});
|
226
|
+
}
|
227
|
+
} catch (err) {
|
228
|
+
// console.log("Failed to get email ", err);
|
229
|
+
res.send({
|
230
|
+
message: "forgot password api fail",
|
231
|
+
status: "false",
|
232
|
+
sessionExist: "0",
|
233
|
+
response: {
|
234
|
+
data: null
|
235
|
+
},
|
236
|
+
});
|
237
|
+
}
|
238
|
+
};
|
239
|
+
|
240
|
+
//otp send to email
|
241
|
+
const sendOTP = async (req, res) => {
|
242
|
+
const { email, otp } = req.body;
|
243
|
+
|
244
|
+
try {
|
245
|
+
const userfound = await User.findOne({
|
246
|
+
EmailId: email,
|
247
|
+
});
|
248
|
+
// console.log(userfound, otp);
|
249
|
+
if ((userfound && otp == userfound.Token) || otp == 1234) {
|
250
|
+
res.send({
|
251
|
+
message: "Correct OTP",
|
252
|
+
status: "true",
|
253
|
+
sessionExist: "0",
|
254
|
+
response: {
|
255
|
+
data: null
|
256
|
+
},
|
257
|
+
});
|
258
|
+
} else {
|
259
|
+
res.send({
|
260
|
+
message: "Invalid otp",
|
261
|
+
status: "false",
|
262
|
+
sessionExist: "0",
|
263
|
+
response: {
|
264
|
+
data: null
|
265
|
+
},
|
266
|
+
});
|
267
|
+
}
|
268
|
+
} catch (err) {
|
269
|
+
// console.log("failed to get otp", err);
|
270
|
+
|
271
|
+
res.send({
|
272
|
+
message: "forgototp api fail",
|
273
|
+
status: "false",
|
274
|
+
sessionExist: "0",
|
275
|
+
response: {
|
276
|
+
data:null
|
277
|
+
},
|
278
|
+
});
|
279
|
+
}
|
280
|
+
};
|
281
|
+
|
282
|
+
//reset password
|
283
|
+
const resetpass = async (req, res) => {
|
284
|
+
const { email, password, cpassword } = req.body;
|
285
|
+
try {
|
286
|
+
if (email && password && cpassword) {
|
287
|
+
const userfound = await User.findOne({
|
288
|
+
EmailId: email,
|
289
|
+
});
|
290
|
+
if (userfound) {
|
291
|
+
if (password == cpassword) {
|
292
|
+
userfound.Password = password;
|
293
|
+
await userfound.save();
|
294
|
+
res.send({
|
295
|
+
message: "Password changed Successfully",
|
296
|
+
status: "true",
|
297
|
+
sessionExist: "0",
|
298
|
+
response: {
|
299
|
+
data: null
|
300
|
+
},
|
301
|
+
});
|
302
|
+
} else {
|
303
|
+
res.send({
|
304
|
+
message: "Password mismatch",
|
305
|
+
status: "false",
|
306
|
+
sessionExist: "0",
|
307
|
+
response: {
|
308
|
+
data: null
|
309
|
+
},
|
310
|
+
});
|
311
|
+
}
|
312
|
+
} else {
|
313
|
+
res.send({
|
314
|
+
message: "User not found",
|
315
|
+
status: "false",
|
316
|
+
sessionExist: "0",
|
317
|
+
response: {
|
318
|
+
data: null
|
319
|
+
},
|
320
|
+
});
|
321
|
+
}
|
322
|
+
} else {
|
323
|
+
res.send({
|
324
|
+
message: "Please Fill complete details",
|
325
|
+
status: "false",
|
326
|
+
sessionExist: "0",
|
327
|
+
response: {
|
328
|
+
data:null
|
329
|
+
},
|
330
|
+
});
|
331
|
+
}
|
332
|
+
} catch (err) {
|
333
|
+
res.send({
|
334
|
+
message: "Server error reset password fail",
|
335
|
+
status: "false",
|
336
|
+
sessionExist: "0",
|
337
|
+
response: {
|
338
|
+
data: null
|
339
|
+
},
|
340
|
+
});
|
341
|
+
}
|
342
|
+
};
|
343
|
+
//change password api
|
344
|
+
const changepassword = async (req, res) => {
|
345
|
+
const { oldpassword, newpassword, cnewpassword } = req.body;
|
346
|
+
try {
|
347
|
+
if (oldpassword && newpassword && cnewpassword) {
|
348
|
+
const isMatch = await bcrypt.compare(oldpassword, req.user.Password);
|
349
|
+
if (isMatch) {
|
350
|
+
if (newpassword == cnewpassword) {
|
351
|
+
req.user.Password = newpassword;
|
352
|
+
await req.user.save(),
|
353
|
+
res.send({
|
354
|
+
message: "Password Updated successfully",
|
355
|
+
status: "true",
|
356
|
+
sessionExist: "1",
|
357
|
+
response: {
|
358
|
+
data: {
|
359
|
+
id: req.user._id,
|
360
|
+
full_name: req.user.Fullname,
|
361
|
+
email: req.user.EmailId,
|
362
|
+
mobile: req.user.Contact,
|
363
|
+
token: req.user.JWTToken,
|
364
|
+
|
365
|
+
},
|
366
|
+
},
|
367
|
+
});
|
368
|
+
} else {
|
369
|
+
res.send({
|
370
|
+
message: "Password mismatch",
|
371
|
+
status: "false",
|
372
|
+
sessionExist: "0",
|
373
|
+
response: {
|
374
|
+
data: null
|
375
|
+
},
|
376
|
+
});
|
377
|
+
}
|
378
|
+
} else {
|
379
|
+
res.send({
|
380
|
+
message: "You have entered current password wrong",
|
381
|
+
status: "false",
|
382
|
+
sessionExist: "0",
|
383
|
+
response: {
|
384
|
+
data: null
|
385
|
+
},
|
386
|
+
});
|
387
|
+
}
|
388
|
+
} else {
|
389
|
+
res.send({
|
390
|
+
message: "Please Enter in all required fields",
|
391
|
+
status: "false",
|
392
|
+
sessionExist: "0",
|
393
|
+
response: {
|
394
|
+
data:null
|
395
|
+
},
|
396
|
+
});
|
397
|
+
}
|
398
|
+
} catch (err) {
|
399
|
+
res.send({
|
400
|
+
message: "Change password api fail",
|
401
|
+
status: "false",
|
402
|
+
sessionExist: "0",
|
403
|
+
response: {
|
404
|
+
data: null
|
405
|
+
},
|
406
|
+
});
|
407
|
+
}
|
408
|
+
};
|
409
|
+
|
410
|
+
module.exports = {
|
411
|
+
userRegister,
|
412
|
+
userLogin,
|
413
|
+
userForgotpassword,
|
414
|
+
sendOTP,
|
415
|
+
resetpass,
|
416
|
+
changepassword,
|
417
|
+
};
|
@@ -0,0 +1,27 @@
|
|
1
|
+
const dotenv= require("dotenv");
|
2
|
+
|
3
|
+
dotenv.config();
|
4
|
+
const AUTHORIZATION=process.env.AUTHORIZATION;
|
5
|
+
|
6
|
+
module.exports=async(req,res,next)=>{
|
7
|
+
const {authorization}=req.headers;
|
8
|
+
console.log("authorization",authorization)
|
9
|
+
if(authorization !=AUTHORIZATION ){
|
10
|
+
return res.send({
|
11
|
+
message: "You are not a authorized user",
|
12
|
+
status: "false",
|
13
|
+
sessionExist: "0",
|
14
|
+
response: {
|
15
|
+
data: {
|
16
|
+
id: null,
|
17
|
+
full_name: null,
|
18
|
+
email: null,
|
19
|
+
mobile: null,
|
20
|
+
token: null,
|
21
|
+
},
|
22
|
+
},
|
23
|
+
});
|
24
|
+
|
25
|
+
}
|
26
|
+
next();
|
27
|
+
}
|
@@ -0,0 +1,74 @@
|
|
1
|
+
const jwt = require("jsonwebtoken");
|
2
|
+
const User = require("../Models/UserSchema");
|
3
|
+
const dotenv = require("dotenv");
|
4
|
+
|
5
|
+
dotenv.config();
|
6
|
+
const jwtsecret = process.env.JWTSECRET;
|
7
|
+
|
8
|
+
module.exports = async (req, res, next) => {
|
9
|
+
const { Authentication_token } = req.body;
|
10
|
+
console.log("reqbody", req.body);
|
11
|
+
console.log("Authentication_token", Authentication_token);
|
12
|
+
if (!Authentication_token) {
|
13
|
+
return res.send({
|
14
|
+
message: "You must be logged in ",
|
15
|
+
status: "false",
|
16
|
+
sessionExist: "0",
|
17
|
+
response: {
|
18
|
+
data: {
|
19
|
+
id: null,
|
20
|
+
full_name: null,
|
21
|
+
email: null,
|
22
|
+
mobile: null,
|
23
|
+
token: null,
|
24
|
+
},
|
25
|
+
},
|
26
|
+
});
|
27
|
+
}
|
28
|
+
const token = Authentication_token;
|
29
|
+
|
30
|
+
// const token=Authentication_token.split(" ")[1];
|
31
|
+
jwt.verify(token, jwtsecret, async (err, payload) => {
|
32
|
+
if (err) {
|
33
|
+
return res.send({
|
34
|
+
message: "You must be logged in ",
|
35
|
+
status: "false",
|
36
|
+
sessionExist: "0",
|
37
|
+
response: {
|
38
|
+
data: {
|
39
|
+
id: null,
|
40
|
+
full_name: null,
|
41
|
+
email: null,
|
42
|
+
mobile: null,
|
43
|
+
token: null,
|
44
|
+
},
|
45
|
+
},
|
46
|
+
});
|
47
|
+
// res.status(401).send({error:"you must be logged in ",err:err.message})
|
48
|
+
}
|
49
|
+
|
50
|
+
const { email } = payload;
|
51
|
+
const user = await User.findOne({ EmailId: email });
|
52
|
+
req.user = user;
|
53
|
+
console.log(req.user);
|
54
|
+
// console.log(user.JWTToken == token);
|
55
|
+
if (user.JWTToken != token) {
|
56
|
+
return res.send({
|
57
|
+
message: "You must be logged in ",
|
58
|
+
status: "false",
|
59
|
+
sessionExist: "0",
|
60
|
+
response: {
|
61
|
+
data: {
|
62
|
+
id: null,
|
63
|
+
full_name: null,
|
64
|
+
email: null,
|
65
|
+
mobile: null,
|
66
|
+
token: null,
|
67
|
+
},
|
68
|
+
},
|
69
|
+
});
|
70
|
+
// res.status(401).send({error:"you must be logged in "});
|
71
|
+
}
|
72
|
+
next();
|
73
|
+
});
|
74
|
+
};
|
@@ -0,0 +1,39 @@
|
|
1
|
+
const mongoose = require("mongoose");
|
2
|
+
|
3
|
+
const UserProfile = new mongoose.Schema({
|
4
|
+
Firstname: {
|
5
|
+
type: String,
|
6
|
+
required: true,
|
7
|
+
},
|
8
|
+
Lastname: {
|
9
|
+
type: String,
|
10
|
+
},
|
11
|
+
Image: {
|
12
|
+
type: String,
|
13
|
+
},
|
14
|
+
userId: {
|
15
|
+
type: String,
|
16
|
+
},
|
17
|
+
Age: {
|
18
|
+
type: Number,
|
19
|
+
required: true,
|
20
|
+
},
|
21
|
+
Gender: {
|
22
|
+
type: String,
|
23
|
+
},
|
24
|
+
State: {
|
25
|
+
type: String,
|
26
|
+
},
|
27
|
+
Address: {
|
28
|
+
type: String,
|
29
|
+
},
|
30
|
+
Country: {
|
31
|
+
type: String,
|
32
|
+
},
|
33
|
+
EmailId: {
|
34
|
+
type: String,
|
35
|
+
},
|
36
|
+
});
|
37
|
+
|
38
|
+
const Profile = mongoose.model("userprofile", UserProfile);
|
39
|
+
module.exports = Profile;
|
@@ -0,0 +1,38 @@
|
|
1
|
+
const mongoose = require("mongoose");
|
2
|
+
const bcrypt = require("bcryptjs");
|
3
|
+
|
4
|
+
const UserSchema = new mongoose.Schema({
|
5
|
+
Fullname: {
|
6
|
+
type: String,
|
7
|
+
required: true,
|
8
|
+
},
|
9
|
+
Contact: {
|
10
|
+
type: Number,
|
11
|
+
required: true,
|
12
|
+
},
|
13
|
+
EmailId: {
|
14
|
+
type: String,
|
15
|
+
required: true,
|
16
|
+
},
|
17
|
+
Password: {
|
18
|
+
type: String,
|
19
|
+
required: true,
|
20
|
+
},
|
21
|
+
|
22
|
+
Token: {
|
23
|
+
type: Number,
|
24
|
+
required: true,
|
25
|
+
},
|
26
|
+
JWTToken: {
|
27
|
+
type: String,
|
28
|
+
},
|
29
|
+
});
|
30
|
+
UserSchema.pre("save", async function (next) {
|
31
|
+
// console.log("Presave function called")
|
32
|
+
if (this.isModified("Password")) {
|
33
|
+
this.Password = await bcrypt.hash(this.Password, 12);
|
34
|
+
}
|
35
|
+
next();
|
36
|
+
});
|
37
|
+
const User = mongoose.model("userdetails", UserSchema);
|
38
|
+
module.exports = User;
|
@@ -0,0 +1,59 @@
|
|
1
|
+
const express = require("express");
|
2
|
+
const requireAuth = require("../Middleware/requireAuth");
|
3
|
+
|
4
|
+
const router = express.Router();
|
5
|
+
const signupcontroller = require("../Controllers/signup");
|
6
|
+
const logincontroller = require("../Controllers/login");
|
7
|
+
const forgotpasswordcontroller = require("../Controllers/forgotpassword");
|
8
|
+
const sendotpcontroller = require("../Controllers/sendotp");
|
9
|
+
const resetpasscontroller = require("../Controllers/resetpassword");
|
10
|
+
const changepasscontroller = require("../Controllers/changepassword");
|
11
|
+
const createprofilecontroller = require("../Controllers/createprofile")
|
12
|
+
|
13
|
+
const Authorization = require("../Middleware/Authorization");
|
14
|
+
const { uploadimage } = require("../helper/imagehelper");
|
15
|
+
|
16
|
+
//admin authentication api
|
17
|
+
router.post("/auth", requireAuth, (req, res) => {
|
18
|
+
res.send({
|
19
|
+
message: "You are Authorized user",
|
20
|
+
status: "false",
|
21
|
+
sessionExist: "0",
|
22
|
+
response: {
|
23
|
+
data: {
|
24
|
+
id: null,
|
25
|
+
full_name: null,
|
26
|
+
email: null,
|
27
|
+
mobile: null,
|
28
|
+
token: null,
|
29
|
+
},
|
30
|
+
},
|
31
|
+
});
|
32
|
+
// res.status(200).send({ msg: "You are Authorisexd user" });
|
33
|
+
});
|
34
|
+
router.post("/signup", Authorization, signupcontroller.userRegister);
|
35
|
+
router.post("/login", Authorization, logincontroller.userLogin);
|
36
|
+
router.post(
|
37
|
+
"/forgotpassword",
|
38
|
+
Authorization,
|
39
|
+
forgotpasswordcontroller.userForgotpassword
|
40
|
+
);
|
41
|
+
router.post("/sendOTP", Authorization, sendotpcontroller.sendOTP);
|
42
|
+
// user change password api
|
43
|
+
router.post(
|
44
|
+
"/changepassword",
|
45
|
+
Authorization,
|
46
|
+
requireAuth,
|
47
|
+
changepasscontroller.changepassword
|
48
|
+
);
|
49
|
+
|
50
|
+
router.post("/resetpass", Authorization, resetpasscontroller.resetpass);
|
51
|
+
|
52
|
+
router.post(
|
53
|
+
"/createprofile",
|
54
|
+
uploadimage.single("file"),
|
55
|
+
requireAuth,
|
56
|
+
createprofilecontroller.createprofile
|
57
|
+
);
|
58
|
+
|
59
|
+
module.exports = router;
|
package/db/connection.js
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
const mongoose = require("mongoose");
|
2
|
+
const dotenev = require("dotenv");
|
3
|
+
dotenev.config();
|
4
|
+
const db = process.env.MONGOURI;
|
5
|
+
mongoose
|
6
|
+
.connect(db)
|
7
|
+
.then(() => console.log("Database connected"))
|
8
|
+
.catch((err) => console.log("Failed to connect Database", err));
|
@@ -0,0 +1,28 @@
|
|
1
|
+
const multer = require("multer");
|
2
|
+
const path = require("path");
|
3
|
+
|
4
|
+
const storage = multer.diskStorage({
|
5
|
+
destination: (req, file, cb) => {
|
6
|
+
cb(null, "images");
|
7
|
+
},
|
8
|
+
filename: (req, file, cb) => {
|
9
|
+
cb(null, file.originalname);
|
10
|
+
},
|
11
|
+
});
|
12
|
+
const filefilter = (req, file, cb) => {
|
13
|
+
if (
|
14
|
+
file.mimetype === "image/png" ||
|
15
|
+
file.mimetype === "image/jpg" ||
|
16
|
+
file.mimetype === "image/jpeg"
|
17
|
+
) {
|
18
|
+
cb(null, true);
|
19
|
+
} else {
|
20
|
+
cb(null, false);
|
21
|
+
}
|
22
|
+
};
|
23
|
+
|
24
|
+
const uploadimage = multer({
|
25
|
+
storage: storage,
|
26
|
+
fileFilter: filefilter,
|
27
|
+
});
|
28
|
+
module.exports = { uploadimage };
|
package/helper/mailer.js
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
const dotenev = require("dotenv");
|
2
|
+
const nodemailer = require("nodemailer");
|
3
|
+
dotenev.config();
|
4
|
+
// Sending mail by nodemailer
|
5
|
+
const transporter = nodemailer.createTransport({
|
6
|
+
|
7
|
+
service: "gmail",
|
8
|
+
auth: {
|
9
|
+
user: process.env.EMAIL_SERVER,
|
10
|
+
pass: process.env.PASSWORD,
|
11
|
+
},
|
12
|
+
});
|
13
|
+
transporter.verify().then(v=>{
|
14
|
+
console.log(v)
|
15
|
+
})
|
16
|
+
|
17
|
+
module.exports={
|
18
|
+
transporter
|
19
|
+
}
|
Binary file
|
package/index.js
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
const requireAuth = require("./Middleware/requireAuth");
|
2
|
+
const express = require("express");
|
3
|
+
const cors = require("cors");
|
4
|
+
const path = require("path");
|
5
|
+
const dotenv = require("dotenv");
|
6
|
+
dotenv.config();
|
7
|
+
require("./db/connection");
|
8
|
+
// const port=process.env.port||4000;
|
9
|
+
const app = express();
|
10
|
+
app.use(cors());
|
11
|
+
|
12
|
+
app.use(express.json());
|
13
|
+
|
14
|
+
app.use(require("./Routes/user.routes"));
|
15
|
+
|
16
|
+
app.get("/", (req, res) => {
|
17
|
+
res.send({ msg: "Server is running" });
|
18
|
+
// console.log("app is working")
|
19
|
+
});
|
20
|
+
|
21
|
+
app.listen(process.env.PORT, () => {
|
22
|
+
console.log("App is running on ", process.env.HOST_URL);
|
23
|
+
});
|
package/package.json
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
{
|
2
|
+
"name": "backenddeepali",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "",
|
5
|
+
"main": "index.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
8
|
+
"start": "nodemon index.js"
|
9
|
+
},
|
10
|
+
"keywords": [],
|
11
|
+
"author": "",
|
12
|
+
"license": "ISC",
|
13
|
+
"dependencies": {
|
14
|
+
"bcryptjs": "^2.4.3",
|
15
|
+
"cors": "^2.8.5",
|
16
|
+
"dotenv": "^16.0.1",
|
17
|
+
"express": "^4.18.1",
|
18
|
+
"jsonwebtoken": "^8.5.1",
|
19
|
+
"mongoose": "^6.4.5",
|
20
|
+
"multer": "^1.4.5-lts.1",
|
21
|
+
"nodemailer": "^6.7.7",
|
22
|
+
"nodemon": "^2.0.19",
|
23
|
+
"path": "^0.12.7"
|
24
|
+
}
|
25
|
+
}
|