Skip to content

Commit 0666fc2

Browse files
Finish Video
1 parent 5b200ee commit 0666fc2

File tree

6 files changed

+74
-23
lines changed

6 files changed

+74
-23
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
node_modules
2-
.env
1+
.env
2+
node_modules

authServer.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require('dotenv').config()
2+
3+
const express = require('express')
4+
const app = express()
5+
const jwt = require('jsonwebtoken')
6+
7+
app.use(express.json())
8+
9+
let refreshTokens = []
10+
11+
app.post('/token', (req, res) => {
12+
const refreshToken = req.body.token
13+
if (refreshToken == null) return res.sendStatus(401)
14+
if (!refreshTokens.includes(refreshToken)) return res.sendStatus(403)
15+
jwt.verify(refreshToken, process.env.REFRESH_TOKEN_SECRET, (err, user) => {
16+
if (err) return res.sendStatus(403)
17+
const accessToken = generateAccessToken({ name: user.name })
18+
res.json({ accessToken: accessToken })
19+
})
20+
})
21+
22+
app.delete('/logout', (req, res) => {
23+
refreshTokens = refreshTokens.filter(token => token !== req.body.token)
24+
res.sendStatus(204)
25+
})
26+
27+
app.post('/login', (req, res) => {
28+
// Authenticate User
29+
30+
const username = req.body.username
31+
const user = { name: username }
32+
33+
const accessToken = generateAccessToken(user)
34+
const refreshToken = jwt.sign(user, process.env.REFRESH_TOKEN_SECRET)
35+
refreshTokens.push(refreshToken)
36+
res.json({ accessToken: accessToken, refreshToken: refreshToken })
37+
})
38+
39+
function generateAccessToken(user) {
40+
return jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '15s' })
41+
}
42+
43+
app.listen(4000)

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"name": "jwt-api",
2+
"name": "current-project",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
77
"devStart": "nodemon server.js",
8-
"devStart2": "set PORT=4000 && nodemon server.js",
8+
"devStartAuth": "nodemon authServer.js",
99
"test": "echo \"Error: no test specified\" && exit 1"
1010
},
1111
"keywords": [],

requests.rest

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
GET http://localhost:4000/posts
2-
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSmltIiwiaWF0IjoxNTY4NDczMTczLCJleHAiOjE1Njg0NzM3NzN9.LTCI3p1FacQzzceA4lhXM57sRl3WUgkr5nfYSQfCMi0
1+
GET http://localhost:3000/posts
2+
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSmltIiwiaWF0IjoxNTY4NzU5ODEyLCJleHAiOjE1Njg3NTk4Mjd9.0i6Im2gKz7jj8wM7aZZzOPaBS_xHoZWAqBwnldn-lQQ
33

44
###
55

6-
POST http://localhost:5000/login
6+
DELETE http://localhost:4000/logout
7+
Content-Type: application/json
8+
9+
{
10+
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSmltIiwiaWF0IjoxNTY4NzU5OTIyfQ.RT6wszuCeFLwC_6ksmNMIELxiC5s-uRivfRxyZof5ag"
11+
}
12+
13+
###
14+
15+
POST http://localhost:4000/token
16+
Content-Type: application/json
17+
18+
{
19+
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSmltIiwiaWF0IjoxNTY4NzU5OTIyfQ.RT6wszuCeFLwC_6ksmNMIELxiC5s-uRivfRxyZof5ag"
20+
}
21+
22+
###
23+
24+
POST http://localhost:4000/login
725
Content-Type: application/json
826

927
{

server.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,20 @@ const posts = [
1818
]
1919

2020
app.get('/posts', authenticateToken, (req, res) => {
21-
res.json(posts.filter(p => p.username === req.user.name))
22-
})
23-
24-
app.post('/login', (req, res) => {
25-
// Authenticate User
26-
const user = { name: req.body.username }
27-
const token = jwt.sign(
28-
user,
29-
process.env.TOKEN_SECRET,
30-
{ expiresIn: '10m' }
31-
)
32-
res.json({ token: token })
21+
res.json(posts.filter(post => post.username === req.user.name))
3322
})
3423

3524
function authenticateToken(req, res, next) {
3625
const authHeader = req.headers['authorization']
37-
const token = authHeader && authHeader.split(" ")[1]
26+
const token = authHeader && authHeader.split(' ')[1]
3827
if (token == null) return res.sendStatus(401)
3928

40-
jwt.verify(token, process.env.TOKEN_SECRET, (err, user) => {
29+
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, user) => {
30+
console.log(err)
4131
if (err) return res.sendStatus(403)
4232
req.user = user
4333
next()
4434
})
4535
}
4636

47-
app.listen(process.env.PORT || 3000)
37+
app.listen(3000)

0 commit comments

Comments
 (0)