-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (110 loc) · 3.56 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
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
import express from "express";
import bodyParser from "body-parser";
import admin from "firebase-admin";
import jwt from "jsonwebtoken";
import dotenv from "dotenv";
import serviceAccount from "./blog-post-d2781-firebase-adminsdk-nuoh5-3f12f39d53.json" assert { type: 'json' };
dotenv.config();
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: process.env.FIREBASE_DATABASE_URL
});
const jwtSecret = process.env.JWT_SECRET;
const db = admin.firestore();
const app = express();
app.use(bodyParser.json());
const PORT = process.env.PORT;
const authenticateToken = (req, res, next) => {
const token = req.headers['authorization'];
if (!token) return res.sendStatus(401);
jwt.verify(token, jwtSecret, (err, user) => {
if (err) return res.sendStatus(403);
req.user = user;
next();
});
};
app.get('/', (req, res) => {
res.send('Welcome to the Blog Post API!');
});
app.post('/posts', authenticateToken, async (req, res) => {
const { title, content } = req.body;
if (!title || !content) {
return res.status(400).json({ error: 'Title and content are required' });
}
const newPost = {
title,
content,
createdAt: new Date().toISOString()
};
try {
const docRef = await db.collection('posts').add(newPost);
res.status(201).json({ id: docRef.id, ...newPost });
} catch (error) {
res.status(500).json({ error: 'Failed to create post' });
}
});
app.get('/posts',authenticateToken, async (req, res) => {
try {
const snapshot = await db.collection('posts').get();
const posts = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
res.status(200).json(posts);
} catch (error) {
res.status(500).json({ error: 'Failed to retrieve posts' });
}
});
app.get('/posts/:id',authenticateToken, async (req, res) => {
const { id } = req.params;
try {
const doc = await db.collection('posts').doc(id).get();
if (!doc.exists) {
return res.status(404).json({ error: 'Post not found' });
}
res.status(200).json({ id: doc.id, ...doc.data() });
} catch (error) {
res.status(500).json({ error: 'Failed to retrieve post' });
}
});
app.put('/posts/:id', authenticateToken, async (req, res) => {
const { id } = req.params;
const { title, content } = req.body;
if (!title || !content) {
return res.status(400).json({ error: 'Title and content are required' });
}
try {
const postRef = db.collection('posts').doc(id);
const doc = await postRef.get();
if (!doc.exists) {
return res.status(404).json({ error: 'Post not found' });
}
await postRef.update({ title, content });
res.status(200).json({ id, title, content });
} catch (error) {
res.status(500).json({ error: 'Failed to update post' });
}
});
app.delete('/posts/:id', authenticateToken, async (req, res) => {
const { id } = req.params;
try {
const postRef = db.collection('posts').doc(id);
const doc = await postRef.get();
if (!doc.exists) {
return res.status(404).json({ error: 'Post not found' });
}
await postRef.delete();
res.status(200).json({ message: 'Post deleted' });
} catch (error) {
res.status(500).json({ error: 'Failed to delete post' });
}
});
app.post('/token', (req, res) => {
const { username } = req.body;
if (!username) {
return res.status(400).json({ error: 'Username is required' });
}
const user = { username };
const token = jwt.sign(user, jwtSecret, { expiresIn: '1h' });
res.json({ token });
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});