-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthServer.ts
48 lines (40 loc) · 1.37 KB
/
authServer.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
44
45
46
47
48
import express from 'express';
import 'dotenv/config';
import jwt from 'jsonwebtoken';
const app = express();
app.use(express.json());
let refreshTokens: string[] = [];
function generateAccessToken(user: any) {
return jwt.sign(user, process.env.ACCESS_TOKEN_SECRET!, { expiresIn: '15m' });
}
app.post('/token', (req: any, res: any) => {
const refreshToken = req.body.refreshToken;
console.log(refreshTokens, 'refreshTokens');
if (refreshToken == null) return res.sendStatus(401);
if (!refreshTokens.includes(refreshToken)) return res.sendStatus(403);
jwt.verify(
refreshToken,
process.env.REFRESH_TOKEN_SECRET!,
(err: any, user: any) => {
if (err) return res.sendStatus(403);
const accessToken = generateAccessToken({ name: user.name });
res.json({ accessToken: accessToken });
}
);
});
app.delete('/logout', (req: any, res: any) => {
refreshTokens = refreshTokens.filter(
(token) => token !== req.body.refreshToken
);
res.sendStatus(204);
});
app.post('/login', (req: any, res: any) => {
// Authenticate User
const username = req.body.username;
const user = { name: username };
const accessToken = generateAccessToken(user);
const refreshToken = jwt.sign(user, process.env.REFRESH_TOKEN_SECRET!);
refreshTokens.push(refreshToken);
res.json({ accessToken: accessToken, refreshToken: refreshToken });
});
app.listen(4000);