-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.js
35 lines (31 loc) · 902 Bytes
/
tokenizer.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
const { AuthBase, NotAuthorized, UserNotFound } = require('./authbase');
const REFRESH_CACHE = new Set();
class Tokenizer extends AuthBase {
saveRefreshToken(token) {
REFRESH_CACHE.add(token);
}
async findAndRemoveRefreshToken(refreshToken) {
const exists = REFRESH_CACHE.has(refreshToken);
if (!exists) {
return null;
}
REFRESH_CACHE.delete(refreshToken);
return true;
}
async authErrorHandler(err, req, res, next) {
if (err instanceof UserNotFound) {
return res
.status(404)
.json(new Error('Username/Password not found.'));
}
return res
.status(400)
.json(new Error('Username/Password fields missing.'));
}
async guardErrorHandler(err, req, res, next) {
if (err instanceof NotAuthorized) {
return res.status(401).json(new Error('Not Authorized.'));
}
}
}
module.exports = new Tokenizer();