Skip to content

Instantly share code, notes, and snippets.

@fl0wo
Last active December 12, 2022 21:14
Show Gist options
  • Save fl0wo/fd5e30a20d3bc85a5bd7c72b5930b785 to your computer and use it in GitHub Desktop.
Save fl0wo/fd5e30a20d3bc85a5bd7c72b5930b785 to your computer and use it in GitHub Desktop.
Integration of login stytch lambda
export const handler = async (event:any) => {
// Ge the token from Stytch
const token = event.queryStringParameters.token;
const client = loadStytch();
// Grant JWT
const response:AuthenticateResponse = await client.oauth.authenticate(token,{
session_duration_minutes: 60
});
const userId = response.user_id;
const sessionId = getSafeOrThrow(response.session?.session_id,'Session id cannot be null');
const tokenJwt = getSafeOrThrow(response.session_jwt,'Session session_jwt cannot be null');
// Save JWT to Secrets Manager using session-id as key
const jwtId = await putParameterValue(wrapCodeWithPrefix(sessionId),tokenJwt);
// Redirect to profile front-end page
return {
statusCode: 301,
headers: {
Location: 'http://localhost:4200/profile?stytch_session_id='+sessionId
}
};
};
export function wrapCodeWithPrefix(code: string) {
return String(process.env.PARAMETER_STORE_PREFIX) + code
}
@fl0wo
Copy link
Author

fl0wo commented Dec 12, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment