Skip to content

Commit

Permalink
Convert to aws lambda func.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterp committed Jul 21, 2019
1 parent d1b92f2 commit 2231e27
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions api/src/functions/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { user } from "src/services";
dotenv.config({ path: "../../.env" });

// TODO: Move these into specialized auth0 package.
const tokenFromRequest = req => req.get("authorization").split(" ")[1];
const tokenFromEvent = event => {
if (!event.headers.authorization && !event.headers.authorization.length) {
return undefined;
}
return event.headers.authorization.split(" ")[1];
};

const userProfileForToken = async token => {
const response = await fetch(`https://${process.env.AUTH0_DOMAIN}/userinfo`, {
Expand Down Expand Up @@ -67,24 +72,14 @@ const decodeVerifiedToken = token => {
});
};

export const SERVERLESS_FUNCTION_TYPE = "express";

export const handler = async (req, res, next) => {
try {
const token = tokenFromRequest(req);

const userProfile = await userProfileForToken(token);

// here we can get the email, and generate an account,
// or retrieve an account.

const { email } = userProfile;

await user.findOrCreate({ email });

res.send(JSON.stringify(userProfile));
} catch (e) {
console.log(e, "-------");
res.send(e);
}
export const handler = async event => {
const token = tokenFromEvent(event);
const userProfile = await userProfileForToken(token);
// here we can get the email, and generate an account,
// or retrieve an account.
const { email } = userProfile;
await user.findOrCreate({ email });
return {
body: userProfile
};
};

0 comments on commit 2231e27

Please sign in to comment.