jagregory / cognito-local

Local emulator for Amazon Cognito
MIT License
278 stars 67 forks source link

Confirmation code not being printed in cognito-local logs #382

Closed piyushk-sony closed 1 year ago

piyushk-sony commented 1 year ago

I'm using cognito-local with a simple full stack example to allow our users to add auth during local testing of the stack without having to provision a Cognito User Pool in AWS. I'm using it via docker (jagregory/cognito-local:latest) and docker-compose and the default configuration.

The README suggests that confirmation codes should be printed in the logs https://github.com/jagregory/cognito-local#confirmation-codes,. Is there a configuration option I need to enable to print those confirmations to the screen? I have confirmed that the SignUp request is being received and processed by Cognito-local and the user is added with "UserStatus" as "UNCOMFIRMED", but no sign up code is printed on the screen.

Notes:

piyushk-sony commented 1 year ago

More Information: I'm seeing is that GetUserAttributeVerificationCode is failing for the user because the user doesn't have an attribute for the verification code?

My SignUp request looked as follows: X-Amz-Target: AWSCognitoIdentityProviderService.SignUp Payload:

{"ClientId":"4z77kse4e7731bigsn07mgb3o","Username":"tester@test.com","Password":"test1234","UserAttributes":[{"Name":"email","Value":"tester@test.com"}],"ValidationData":null}

Response:

{"UserConfirmed":false,"UserSub":"9be9b1c3-7f9e-4e4c-9e06-597af551d13d"}

Here's the user as created by cognito-local:

 {
            "Username": "tester@test.com",
            "Attributes": [
                {
                    "Name": "sub",
                    "Value": "9be9b1c3-7f9e-4e4c-9e06-597af551d13d"
                },
                {
                    "Name": "email",
                    "Value": "tester@test.com"
                }
            ],
            "UserCreateDate": 1682524586,
            "UserLastModifiedDate": 1682524586,
            "Enabled": true,
            "UserStatus": "UNCONFIRMED"
        }
jagregory commented 1 year ago

Hey @piyushk-sony, sorry to hear you're having issues. The console logging does work (I use it myself), but there might be some combination of user pool setting you're missing or unexpected behaviour of cognito-local vs real cognito.

When a user signs up, a combination of the pool's AutoVerifiedAttributes and the user's Attributes are used to determine if a code should be sent to them. If it is sent it will be printed to the console.

For example, your user pool should have an AutoVerifiedAttributes set to email and your user should have an email attribute. If both of those aren't true a code won't be sent/printed. I can see your user has the email attribute, does your user pool have AutoVerifiedAttributes set?

This is the logic:

https://github.com/jagregory/cognito-local/blob/161d4f69821f655c62ee4a5d7f2d9da06939ab73/src/targets/signUp.ts#L37-L50

https://github.com/jagregory/cognito-local/blob/161d4f69821f655c62ee4a5d7f2d9da06939ab73/src/services/messageDelivery/deliveryMethod.ts#L5-L32

As for GetUserAttributeVerificationCode not working, that definitely sounds like a legit issue. Possibly the code isn't being saved against the user because of the above logic, when it should be saved even if the message isn't sent.

piyushk-sony commented 1 year ago

Thanks a lot @jagregory. That was exactly it. Creating the user-pool with --auto-verified-attributes email shows the confirmation code output.