Created
August 23, 2019 16:53
-
-
Save phawk/b46f3e603a91be7e66d4653b655b9700 to your computer and use it in GitHub Desktop.
Lambda HoC for authenticating requests with dynamoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Account = require("../models/account") | |
module.exports = (handler) => async (event, context) => { | |
let auth = event.headers.authorization | |
auth = auth.replace(/^Basic\s/, "") | |
auth = Buffer.from(auth, 'base64').toString() | |
const [id, token] = auth.split(":") | |
const account = await Account.get({ id }) | |
if (!account || account.token !== token) { | |
return { | |
statusCode: 401, | |
headers: { "Content-Type": "application/json" }, | |
body: JSON.stringify({ | |
error: "Access token invalid" | |
}) | |
} | |
} | |
return await handler(event, { ...context, account }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment