You can extend the existing custom authentication challenge Lambda triggers (Magic Links, FIDO2, or SMS OTP) by using the configure() method provided by the library. This allows you to inject custom logic—such as custom email templates or different email providers—without rewriting the entire authentication flow.
To use this approach:
- Import the specific auth module (e.g.,
magicLink) from amazon-cognito-passwordless-auth/custom-auth. - Export the library's
createAuthChallengeHandler as your Lambda handler. - Call
configure() to override specific properties like contentCreator or emailSender. - The library will merge your custom logic with its internal
createAuthChallenge function.
import { magicLink } from "amazon-cognito-passwordless-auth/custom-auth";
// Export the solution's handler to be the handler of YOUR Lambda function too:
export { createAuthChallengeHandler as handler } from "amazon-cognito-passwordless-auth/custom-auth";
// Calling configure() without arguments retrieves the current configuration:
const defaultConfig = magicLink.configure();
// Add your own logic:
magicLink.configure({
async contentCreator({ secretLoginLink }) {
return {
html: {
data: `<html><body><p>Your secret sign-in link: <a href="${secretLoginLink}">sign in</a></p>This link is valid for ${Math.floor(
defaultConfig.secondsUntilExpiry / 60
)} minutes<p></p></body></html>`,
charSet: "UTF-8",
},
text: {
data: `Your secret sign-in link: ${secretLoginLink}`,
charSet: "UTF-8",
},
subject: {
data: "Your secret sign-in link",
charSet: "UTF-8",
},
};
},
});