-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lambda-Authorizer.js
35 lines (32 loc) · 905 Bytes
/
Lambda-Authorizer.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
"use strict";
const jwt = require("jsonwebtoken");
const JWT_SECRET = process.env.JWT_SECRET;
module.exports.handler = async (event, context, callback) => {
try {
const token = event.headers.authorization;
const authorizationToken = jwt.verify(token, JWT_SECRET);
const userId = authorizationToken.id;
console.log("Allowed");
return generateResponse(true, userId);
} catch (err) {
if (err.name === "TokenExpiredError") {
console.log("TokenExpiredError");
callback("Unauthorized");
} else if (err.name === "JWTError") {
console.log("JWTError");
return generateResponse(false);
} else {
console.log(err.name);
return generateResponse(false);
}
}
};
const generateResponse = (isAuthorized, userId) => {
const response = {
isAuthorized: isAuthorized,
context: {
userId: userId,
},
};
return response;
};