-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (53 loc) · 1.37 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
const express=require('express')
const path=require('path')
const User=require('./database/user-model.js')
const auth=require('./auth.js')
var cors = require('cors')
var app = express()
app.use(cors())
require('./database/connect.js')
const port=process.env.PORT||4000
app.use(express.static(__dirname))
app.post('/users/login',async (req,res)=>{
try{
const user=await User.findByCred(req.body.email,req.body.password)
const token=await user.genAuthToken()
return res.send({user,token})
}
catch(e){
console.log(e)
res.send(e)
}
})
app.post('/users/register',async (req,res)=>{
const user=new User(req.body)
console.log(req.body)
// console.log(user)
try{
await user.save()
console.log(`User Created ${user}`)
return res.status(201).send(user)
}
catch(e){
res.send(e.errmsg)
}
})
app.post('/users/logOut',async (req,res)=>{
try{
req.user.tokens=req.user.tokens.filter((obj)=>{
return obj.token !==req.token
})
await req.user.save()
console.log('TILL HERE')
res.send('Logged Out Successfully')
}
catch(e){
return res.status(500).send(e)
}
})
app.get('*',(req,res)=>{
res.sendFile(__dirname+'/error.html')
})
app.listen(port,()=>{
console.log(`Listening on port ${port}`)
})