-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
138 lines (119 loc) · 3.67 KB
/
Copy pathapp.js
File metadata and controls
138 lines (119 loc) · 3.67 KB
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
const express = require('express')
const app = express()
const PORT = 5000
const cors = require('cors')
const mongoose = require('mongoose')
const User = require('./models/Users')
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const {JWT_SECRET, MONGOURL} = require('./config/keys')
const Todo = require('./models/Todos')
mongoose.connect(MONGOURL,
{
useNewUrlParser: true,
useUnifiedTopology: true
})
mongoose.connection.on('connected', () => { console.log("Connected to mongo yeahhhh!") })
mongoose.connection.on('error', (err) => { console.log("error", err) })
app.get('/', (req, res) => {
res.json({ message: 'Hello world' })
})
app.use(express.json())
app.use(cors())
const requireLogin = (req, res, next) => {
const {authorization} = req.headers
if(!authorization){
return res.status(401).json({error: "You must be logged in"})
}
try{
const {userId} = jwt.verify(authorization, JWT_SECRET)
req.user = userId
next()
}
catch(err){
return res.status(401).json({error: "You must be logged in"})
}
}
app.get('/test', requireLogin, (req, res) => {
res.json({message: req.user})
})
//post request to server
app.post('/signup', async (req, res) => {
const { email, password } = req.body
console.log(req.body)
try {
if (!email || !password) {
return res.status(422).json({ error: 'Please add all fields' })
}
// if user already exist
const user = await User.findOne({ email })
if (user) {
return res.status(422).json({ error: "User already exist with that email" })
}
//encript password
const hashpassword = await bcrypt.hash(password, 12)
await new User({
email,
password: hashpassword
}).save()
res.status(200).json({ message: "SignUp successful you can now log in" })
}
catch (err) {
console.log(err)
}
})
//sign in
app.post('/signin', async (req, res) => {
const { email, password } = req.body
console.log(req.body)
try {
if (!email || !password) {
return res.status(422).json({ error: 'Please add all fields' })
}
// if user already exist
const user = await User.findOne({ email })
if (!user) {
return res.status(404).json({ error: "No user found with that email or password" })
}
//encript password
const doMatch = await bcrypt.compare(password, user.password)
if(doMatch){
const token = jwt.sign({userId: user._id}, JWT_SECRET)
res.status(201).json({token})
}
else{
return res.status(401).json({error: "email or password is invalid"})
}
}
catch (err) {
console.log(err)
}
})
//Todo
app.post('/createtodo', requireLogin,async (req, res) => {
const data = await new Todo({
todo: req.body.todo,
todoBy: req.user
}).save()
res.status(201).json({message: data})
})
app.get('/gettodos', requireLogin,async (req, res) => {
const data = await Todo.find({
todoBy:req.user
})
res.status(200).json({message: data})
})
app.delete('/remove/:Id',requireLogin ,async(req, res)=>{
const removedTodo = await Todo.findOneAndRemove({_id: req.params.Id})
res.status(200).json({message: removedTodo})
})
if(process.env.NODE_ENV == 'production'){
const path = require('path')
app.get('/', (req, res) => {
app.use(express.static(path.resolve(__dirname, 'client', 'build')))
res.sendFile(path.resolve(__dirname, 'client', 'build','index.html'))
})
}
app.listen(PORT, () => {
console.log("Server Running on", PORT)
})