Skip to content

Instantly share code, notes, and snippets.

@phawk
Created August 23, 2019 16:53
Show Gist options
  • Save phawk/b46f3e603a91be7e66d4653b655b9700 to your computer and use it in GitHub Desktop.
Save phawk/b46f3e603a91be7e66d4653b655b9700 to your computer and use it in GitHub Desktop.
Lambda HoC for authenticating requests with dynamoDB
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