-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (54 loc) · 1.74 KB
/
app.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
const express = require('express');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
const app = express();
app.use(express.json());
mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true, useUnifiedTopology: true});
const userSchema = new mongoose.Schema({
email: String,
password: String,
});
const blogSchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
title: String,
description: String
});
const User = mongoose.model('User', userSchema);
const Blog = mongoose.model('Blog', blogSchema);
app.post('/register', async (req, res) => {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const user = new User({ email: req.body.email, password: hashedPassword });
await user.save();
res.status(201).send();
});
app.post('/login', async (req, res) => {
const user = await User.findOne({ email: req.body.email });
if (user && await bcrypt.compare(req.body.password, user.password)) {
const token = jwt.sign({ id: user._id }, 'secret_key');
res.json({ token });
} else {
res.status(401).send();
}
});
app.use((req, res, next) => {
const token = req.headers['authorization'];
try {
req.user = jwt.verify(token, 'secret_key');
next();
} catch {
res.status(401).send();
}
});
app.post('/blogs', async (req, res) => {
const blog = new Blog({ ...req.body, userId: req.user.id });
await blog.save();
res.status(201).send();
});
app.get('/blogs', async (req, res) => {
const blogs = await Blog.find({ userId: req.user.id });
res.json(blogs);
});
app.listen(3000, () => {
console.log("running in 3000");
});