-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.js
155 lines (144 loc) · 4.5 KB
/
blog.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
var express = require('express')
var router = express.Router()
var bodyParser = require('body-parser')
var mongoose = require('mongoose')
var jwt = require('jsonwebtoken')
var passport = require('passport')
var bcrypt = require('bcryptjs')
var Strategy = require('passport-http-bearer').Strategy
var Post = require('./models/Post')
var User = require('./models/User')
require("dotenv").config({silent: true});
var DATABASE_URI = process.env.DATABASE_URI
var TOKENSECRET = process.env.SECRET
var jsonParser = bodyParser.json()
// passport.serializeUser(function(user, done) {
// console.log("USER: ", user);
// done(null, user)
// })
// passport.deserializeUser(function(user, done) {
// done(null, user)
// })
passport.use(new Strategy(
function(token, done) {
if(token) {
jwt.verify(token, TOKENSECRET, function(err, decoded) {
if(err) {
return done(err)
}
return done(null, decoded, {scope: 'all'})
})
} else {
return done(null, false)
}
}
))
router.use(passport.initialize())
// User.find({}).remove().exec()
// Post.find({}).remove().exec()
router.post('/users', jsonParser, function(req,res) {
console.log(req.body)
var name = req.body.name
var email = req.body.email
var password = req.body.password
bcrypt.genSalt(10, function(err, salt) {
if (err) {
console.log(err)
return res.status(500).json({
message: 'Internal server error'
});
}
bcrypt.hash(password, salt, function(err, hash) {
if (err) {
console.log("!!!", err)
return res.status(500).json({
message: 'Internal server error'
});
}
var user = new User({
name: name,
email: email,
password: hash
});
user.save(function(err) {
if (err) {
console.log("Mongoose: ", err)
return res.status(500).json({
message: 'Internal server error'
});
}
var token = jwt.sign(user, TOKENSECRET, {
expiresIn: "24h"
})
return res.status(201).json({
sucess: true,
message: 'Token created',
token: token
});
});
});
});
})
router.get('/users', passport.authenticate('bearer', {session: false}), function(req, res) {
return res.status(200).json({message: "Token validated"})
})
router.post('/login', jsonParser, function(req, res) {
console.log('Login endpoint accessed here')
var password = req.body.password
User.findOne({email: req.body.email}, function(err, user) {
console.log('return from mongo', user, !user)
if(err) return res.status(500).json({message: 'Internal server error'})
if(!user) {
console.log("this ran")
return res.status(400);
}
user.validatePassword(password, function(err, isValid) {
if(err) {
console.log("bcrypt error: ", err)
return res.status(400).json({message: "Internal server error"});
}
if(!isValid) {
console.log('password incorrect')
return res.status(400).json({message: 'Incorrect password'})
}
var token = jwt.sign(user, TOKENSECRET, {
expiresIn: "24h"
})
console.log('validated response sent')
return res.status(200).json({
sucess: true,
message: 'Token created',
token: token
});
})
})
})
router.get('/posts', function(req, res) {
Post.find({}, function(err, posts) {
if(err) {
console.log("Mongo ERROR: ", err)
return res.status(500).json('Internal Server Error')
}
return res.status(200).json(posts)
})
})
router.post('/posts', passport.authenticate('bearer', {session:false}), jsonParser, function(req, res) {
Post.create({
title: req.body.title,
description: req.body.description,
body: req.body.body
}, function(err, post) {
if(err) {
console.log("Mongo ERROR: ", err)
res.status(500).json('Internal Server Error')
}
return res.status(200).json(post)
})
})
router.delete('/posts', passport.authenticate('bearer', {session:false}), jsonParser, function(req, res) {
Post.remove({_id: req.body._id}, function(err, post) {
if(err) res.status(501).json({"message": "internal server error"});
res.status(200).json("post deleted");
})
})
module.exports = router