Skip to content

Commit 3fac1f5

Browse files
author
Zoltán Takács
committed
Detect expired token
1 parent fea9686 commit 3fac1f5

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/login/renew.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ const jwt = require('jsonwebtoken');
33
module.exports = (req, res) => {
44
const { prevJwt } = req.body;
55

6-
const { userId } = jwt.verify(prevJwt, 'almafa');
6+
let userId = null;
7+
8+
try {
9+
userId = jwt.verify(token, 'almafa').userId;
10+
} catch {
11+
throw new Error('Unauthorized|401|UNAUTHORIZED');
12+
}
713

814
const me = req.context.memoryStorage.getUserById(userId);
915

src/middlewares/auth.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ const auth = (req, res, next) => {
77
throw new Error('Unauthorized|401|UNAUTHORIZED');
88
}
99

10-
const { userId } = jwt.verify(token, 'almafa');
10+
let userId = null;
1111

12-
if (!userId) {
12+
try {
13+
userId = jwt.verify(token, 'almafa').userId;
14+
} catch {
1315
throw new Error('Unauthorized|401|UNAUTHORIZED');
1416
}
1517

0 commit comments

Comments
 (0)