/
server.js
152 lines (131 loc) · 3.75 KB
/
server.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
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const path = require("path");
//new here
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const Todo = require("./models/todoModel");
const User = require("./models/userModel");
const app = express();
app.use(cookieParser()); //added
app.use(bodyParser.json({extended:true})); //added
//app.use(cors());
app.use(express.json());
const SECRET = process.env.SECRET;
// const secret = 'secret123';
// Database connect
// Connect to mongodb
const URI = process.env.MONGODB_URL; //pass to .env file
mongoose.connect(URI, {
useCreateIndex: true, //comment out due to error
useFindAndModify: false, //comment out due to error
useNewUrlParser: true,
useUnifiedTopology: true
}, err => {
if(err) throw err;
console.log("Connected to mongodb"); //connected to mongo cloudb
})
app.use(cors({
credentials:true,
origin: 'http://localhost:3000',
}));
app.get('/', (req, res) => {
res.send('server is ok');
});
app.get('/user', (req, res) => {
if (!req.cookies.token) {
return res.json({});
}
const payload = jwt.verify(req.cookies.token, SECRET);
User.findById(payload.id)
.then(userInfo => {
if (!userInfo) {
return res.json({});
}
res.json({id:userInfo._id,email:userInfo.email});
});
});
app.post('/register', (req, res) => {
const {email,password} = req.body;
const hashedPassword = bcrypt.hashSync(password, 10);
const user = new User({password:hashedPassword,email});
user.save().then(userInfo => {
jwt.sign({id:userInfo._id,email:userInfo.email}, SECRET, (err,token) => {
if (err) {
console.log(err);
res.sendStatus(500);
} else {
res.cookie('token', token).json({id:userInfo._id,email:userInfo.email});
}
});
});
});
app.post('/login', (req, res) => {
const {email,password} = req.body;
User.findOne({email})
.then(userInfo => {
if (!userInfo) {
return res.sendStatus(401);
}
const passOk = bcrypt.compareSync(password, userInfo.password);
if (passOk) {
jwt.sign({id:userInfo._id,email},SECRET, (err,token) => {
if (err) {
console.log(err);
res.sendStatus(500);
} else {
res.cookie('token', token).json({id:userInfo._id,email:userInfo.email});
}
});
} else {
res.sendStatus(401);
}
})
});
app.post('/logout', (req, res) => {
res.cookie('token', '').send();
});
//Read Todos
app.get('/todos', (req,res) => {
const payload = jwt.verify(req.cookies.token, SECRET);
Todo.where({user:new mongoose.Types.ObjectId(payload.id)})
.find((err,todos) => {
res.json(todos);
})
});
//update
app.put('/todos', (req, res) => {
const payload = jwt.verify(req.cookies.token, SECRET);
const todo = new Todo({
text:req.body.text,
done:false,
user:new mongoose.Types.ObjectId(payload.id),
});
todo.save().then(todo => {
res.json(todo);
})
});
app.post('/todos', (req,res) => {
const payload = jwt.verify(req.cookies.token, SECRET);
Todo.updateOne({
_id:new mongoose.Types.ObjectId(req.body.id),
user:new mongoose.Types.ObjectId(payload.id)
}, {
done:req.body.done,
}).then(() => {
res.sendStatus(200);
});
});
// Routes
const notesRouter = require("./routes/notes");
app.use("/notes", notesRouter);
//const todos = require("./routes/todos");
//app.use("/todos", todos);
const PORT = process.env.PORT || 5000
app.listen(PORT, () => { //create NodeJS Server
console.log('Server is running on port', PORT) //listen port 5000 in the backend
})