-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
43 lines (35 loc) · 930 Bytes
/
server.ts
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
import express from 'express';
import 'dotenv/config';
import jwt from 'jsonwebtoken';
const app = express();
app.use(express.json());
const posts = [
{
username: 'Fayed',
title: 'Post 1',
},
{
username: 'Fayed2',
title: 'Post 12',
},
{
username: 'Dev',
title: 'Post 2',
},
];
app.get('/posts', authenticateToken, (req: any, res: any) => {
res.json(posts.filter((post) => post.username === req.user.name));
});
function authenticateToken(req: any, res: any, next: any) {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (token == null) return res.sendStatus(401);
console.log(process.env.ACCESS_TOKEN_SECRET, '11');
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET!, (err: any, user: any) => {
console.log(err);
if (err) return res.sendStatus(403);
req.user = user;
next();
});
}
app.listen(3000);