Skip to content

How to streamline access token issuance after account confirmation in AWS Cognito for a passwordless flow (combined sign-up/sign-in)?

0

I've set up a passwordless login flow using AWS Cognito and want to improve the user experience based on patterns seen in popular apps:

Sign-up / sign-in Verify

In my existing implementation, AWS Cognito handles a passwordless login where users receive a confirmation code to verify their account. However, after confirming their account, users still need to log in separately to obtain the access token, which triggers another confirmation code via email.

This two-step process, where users must check their email twice—once for account confirmation and then again for logging in—creates friction, especially in mobile apps.

What I want to achieve is a more seamless flow: users interact with a combined sign-up/sign-in page where they enter their email address. This unified process applies whether they're registering for the first time or logging back in.

For example, the flow would be:

  • Enter email: (Sign-up / Sign-in)
  • Enter verification code: (One-time code)

After entering the code, they should be logged in, regardless of whether it's their first time or a return visit.

I've explored AWS's documentation and experimented with the post-confirmation Lambda trigger, but it seems like this doesn't provide all the necessary information to authenticate a user during sign-up. For example, it seems like I can't use the AdminInitiateAuth method in my Lambda function as I don't have the (randomly generated) user password generated by the mobile app to authenticate with (as the flow skips password entry entirely).

Is there a way to immediately issue an access token right after the account is confirmed, so users can proceed without the need to log in again? I'm looking for a solution that allows them to complete the process with just one verification code, covering both sign-up and sign-in.

Any help or advice on how to implement this kind of combined sign-up/sign-in flow using AWS Cognito would be greatly appreciated!

1 Answer
0

Hi Rx,

Let's dig into your issue.

Streamlining Access Token Issuance After Account Confirmation in AWS Cognito

To streamline a passwordless sign-up/sign-in flow in AWS Cognito, here’s how to unify account confirmation and token issuance, ensuring users don’t have to re-login after verification.


1. Customize the Flow with Lambda Triggers

  • Use the CustomMessage Trigger to send a single, user-friendly email containing the one-time verification code. Make it clear in the email that the code will log them in after confirmation, avoiding confusion.
  • Leverage the PostConfirmation Trigger to automate authentication and issue tokens immediately after the code is verified.

2. Automate Token Issuance After Verification

To authenticate users and issue tokens after email confirmation:

  • Call the AdminInitiateAuth API within the PostConfirmation Trigger to log the user in.
  • Use the CUSTOM_AUTH flow to bypass password requirements, making the process truly passwordless.
  • Programmatically generate the tokens (id_token, access_token, and refresh_token) and return them to the app.

Example PostConfirmation Lambda Function:

const AWS = require('aws-sdk');
const cognito = new AWS.CognitoIdentityServiceProvider();

exports.handler = async (event) => {
    const { userPoolId, userName } = event;

    try {
        // Call AdminInitiateAuth to authenticate the user
        const authParams = {
            AuthFlow: 'CUSTOM_AUTH', // Allows bypassing password requirements
            ClientId: process.env.CLIENT_ID, // Your Cognito App Client ID
            UserPoolId: userPoolId,
            AuthParameters: { USERNAME: userName },
        };

        const authResponse = await cognito.adminInitiateAuth(authParams).promise();

        // Return the tokens to the app
        event.response = {
            idToken: authResponse.AuthenticationResult.IdToken, // ID token for identity info
            accessToken: authResponse.AuthenticationResult.AccessToken, // Access token for APIs
            refreshToken: authResponse.AuthenticationResult.RefreshToken, // Refresh token for session renewal
        };

        return event;
    } catch (error) {
        console.error('Error during token issuance:', error);
        throw new Error('Failed to authenticate the user.');
    }
};

3. Simplify the Process for Returning Users

  • Check if the user’s email is already confirmed using the AdminGetUser API and their email_verified attribute.
  • If verified, skip the confirmation code and directly authenticate them using AdminInitiateAuth. This avoids redundant steps for returning users.

4. Address Common Edge Cases

  • Expired or Lost Codes: Allow users to easily request a new verification code through your app.
  • Rate Limiting: Use Amazon SES for email delivery and set up monitoring with CloudWatch to prevent throttling issues.
  • Session Security: Store tokens securely on the client side using device-appropriate methods like Secure Storage (mobile apps) or encrypted cookies (web).

Conclusion

By combining AWS Cognito triggers with programmatic token issuance, you can create a seamless passwordless login flow. This eliminates unnecessary friction and delivers an experience users expect from modern apps.

Let me know if you need further guidance on Lambda implementation or any part of this workflow. Happy to help!

Cheers, Aaron! 😊🚀

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.