-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (138 loc) · 4.86 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
const express = require("express");
const mongoose = require('mongoose');
const cors = require("cors");
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const cookieParser = require('cookie-parser');
const userModel = require('./models/user');
const app = express()
app.use(express.json()) //Parsing incoming JSON payloads.
app.use(cors({ //configure frontend running on a different ports
origin: ["http://localhost:5174"],
method: ["GET", "POST"],
credentials:true,
allowedHeaders: ["Content-Type", "Authorization"]
}));
app.use(cookieParser())
mongoose.connect('mongodb://localhost:27017/employee');
const varifyUser = (req, res, next)=>{
const token = req.cookies.token;
if(!token){
return res.json("Token is missing")
}else{
jwt.verify(token, "jwt-secret-key", (err, decoded) => {
if(err){
return res.json("Error with token")
}else{
if(decoded.role === "admin"){
next()
}else{
return res.json("not admin")
}
}
});
}
}
app.get('/dashboard',varifyUser ,(req, res) =>{
res.json("Success")
})
app.post('/register', (req, res) => {
const {name,email,password} = req.body;
bcrypt.hash(password, 10)
.then(hash => {
userModel.create({name, email, password: hash})
.then(user => res.json({status: "OK"}))
.catch(err => res.json(err))
}) .catch(err => res.json(err))
})
app.post('/updateUser', (req, res) => {
const {name,email,password} = req.body;
bcrypt.hash(password, 10)
.then(hash => {
userModel.updateOne({name, email, password: hash})
.then(user => res.json({status: "OK"}))
.catch(err => res.json(err))
}) .catch(err => res.json(err))
})
app.post('/login', (req, res) => {
console.log("login called");
const { email, password } = req.body;
userModel.findOne({ email: email })
.then(user => {
if (user) {
console.log("user");
console.log(user);
bcrypt.compare(password, user.password, (err, response) => {
if (response) {
console.log("response");
console.log(response);
const token = jwt.sign(
{ email: user.email, role: user.role },
"jwt-secret-key",
{ expiresIn: '1d' }
);
// Set the token in a cookie
res.cookie('token', token, { httpOnly: true });
return res.json({ message: "Login successful" });
} else {
return res.json({ message: "The password is incorrect" });
}
});
} else {
return res.json({ message: "User not found" });
}
})
.catch(err => res.status(500).json({ message: "Server error" }));
});
app.listen(3001, () => {
console.log("Server is Running")
})
const nodemailer = require('nodemailer');
app.post('/forgot-password', (req, res) => {
const {email} = req.body;
userModel.findOne({email: email})
.then(user => {
if(!user) {
return res.send({Status: "User not exist"})
}
const token = jwt.sign({id: user._id}, "jwt_secret_key", {expiresIn: "1d"})
//node.js email
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'yourpassword'
}
});
var mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Reset your password',
text: `http://localhost:5173/reset-password/${user._id}/${token}`
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
return res.send({status: "Success"})
}
});
})
});
app.post('/reset-password/:id/:token', (req, res) => {
const {id, token} = req.params
const {password} = req.body
jwt.verify(token, "jwt_secret_key", (err, decoded) => {
if(err) {
return res.json({Status: "Error with token"})
} else {
bcrypt.hash (password, 10)
.then(hash =>{
userModel.findByIdAndUpdate({_id:id}, {password: hash})
.then(u => res.send({status: err}))
})
.catch(err => res.send({status: err}))
}
})
})