clean-australia-2084
12/20/2022, 5:17 PMimport {
APIGatewayRequestAuthorizerEventV2,
APIGatewayRequestSimpleAuthorizerHandlerV2, APIGatewaySimpleAuthorizerResult,
APIGatewaySimpleAuthorizerWithContextResult
} from "aws-lambda";
import {Configuration, Session, FrontendApi} from "@ory/client";
export const handler: APIGatewayRequestSimpleAuthorizerHandlerV2 = async (event: APIGatewayRequestAuthorizerEventV2): Promise<APIGatewaySimpleAuthorizerResult | APIGatewaySimpleAuthorizerWithContextResult<Session | Object>> => {
const basePath = process.env.ORY_URL;
const ory = new FrontendApi(
new Configuration({
basePath,
baseOptions: {
withCredentials: true,
},
}),
);
try {
if (event.headers?.cookie) {
const orySession = await ory.toSession({
cookie: event.headers?.cookie,
});
return {
isAuthorized: true,
context: orySession.data
};
} else if (event.headers?.['x-session-token']) {
const orySession = await ory.toSession({
xSessionToken: event.headers?.['x-session-token']
});
return {
isAuthorized: true,
context: orySession.data
};
}
return {
isAuthorized: false,
};
} catch (e) {
return {
isAuthorized: false,
};
}
}