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