aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

Login method is rejecting promise for new auth challenge. #9698

Closed dperconti closed 2 years ago

dperconti commented 2 years ago

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

Authentication, GraphQL API

Amplify Categories

auth

Environment information

``` # Put output below this line System: OS: macOS 11.6.1 CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Memory: 1.07 GB / 32.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 14.17.0 - /usr/local/bin/node Yarn: 1.22.17 - /usr/local/bin/yarn npm: 6.14.13 - /usr/local/bin/npm Browsers: Chrome: 99.0.4844.51 Safari: 15.1 npmPackages: @aws-amplify/cli: ^7.6.23 => 7.6.23 @formatjs/intl-pluralrules: ^4.0.28 => 4.2.1 @formatjs/intl-relativetimeformat: ^9.1.7 => 9.4.1 @fortawesome/fontawesome-free: ^5.15.3 => 5.15.4 @manaflair/redux-batch: ^1.0.0 => 1.0.0 @popperjs/core: ~2.10.1 => 2.10.2 @prism-react-renderer/dracula: undefined () @prism-react-renderer/duotoneDark: undefined () @prism-react-renderer/duotoneLight: undefined () @prism-react-renderer/github: undefined () @prism-react-renderer/nightOwl: undefined () @prism-react-renderer/nightOwlLight: undefined () @prism-react-renderer/oceanicNext: undefined () @prism-react-renderer/okaidia: undefined () @prism-react-renderer/palenight: undefined () @prism-react-renderer/prism: undefined () @prism-react-renderer/shadesOfPurple: undefined () @prism-react-renderer/synthwave84: undefined () @prism-react-renderer/theme-from-vscode: 1.0.0 @prism-react-renderer/ultramin: undefined () @prism-react-renderer/vsDark: undefined () @prism-react-renderer/vsLight: undefined () @reduxjs/toolkit: ^1.6.0 => 1.7.1 @reduxjs/toolkit-query: 1.0.0 @reduxjs/toolkit-query-react: 1.0.0 @testing-library/jest-dom: ^5.11.4 => 5.16.1 @testing-library/react: ^11.1.0 => 11.2.7 @testing-library/user-event: ^12.1.10 => 12.8.3 @types/bootstrap: ^5.0.16 => 5.1.8 @types/chart.js: 2.9.32 => 2.9.32 @types/jest: ^26.0.15 => 26.0.24 @types/node: ^12.0.0 => 12.20.41 (10.17.60, 16.11.26) @types/nouislider: 15.0.0 => 15.0.0 @types/object-path: 0.11.0 => 0.11.0 @types/prismjs: 1.16.5 => 1.16.5 @types/qs: 6.9.7 => 6.9.7 @types/react: ^17.0.0 => 17.0.38 @types/react-copy-to-clipboard: 5.0.0 => 5.0.0 @types/react-dom: ^17.0.0 => 17.0.11 @types/react-redux: 7.1.16 => 7.1.16 (7.1.21) @types/react-table: ^7.7.9 => 7.7.9 animate.css: ^4.1.1 => 4.1.1 apexcharts: ^3.27.1 => 3.32.1 aws-amplify: ^4.3.16 => 4.3.16 axios: ^0.21.1 => 0.21.4 axios-mock-adapter: ^1.19.0 => 1.20.0 bootstrap: ^5.1.3 => 5.1.3 bootstrap-icons: ^1.7.2 => 1.7.2 chart.js: ^3.3.2 => 3.7.0 chart.js-auto: undefined () chart.js-helpers: undefined () clsx: ^1.1.1 => 1.1.1 del: ^6.0.0 => 6.0.0 (4.1.1) formik: ^2.2.9 => 2.2.9 line-awesome: ^1.3.0 => 1.3.0 lint-staged: ^11.0.0 => 11.2.6 mini-css-extract-plugin: 1.6.2 => 1.6.2 (0.11.3) new-plugin-package: 1.0.0 nouislider: ^15.2.0 => 15.5.0 object-path: ^0.11.5 => 0.11.8 path: ^0.12.7 => 0.12.7 prettier: ^2.3.2 => 2.5.1 (2.3.2, 1.19.1) prism-react-renderer: ^1.2.1 => 1.2.1 prism-react-renderer-example: 1.0.0 prism-themes: ^1.7.0 => 1.9.0 prismjs: ^1.23.0 => 1.25.0 qs: 6.10.3 => 6.10.3 (6.9.7, 6.9.6) react: ^17.0.2 => 17.0.2 react-apexcharts: ^1.3.9 => 1.3.9 react-bootstrap: ^2.1.0 => 2.2.0 react-bootstrap/AbstractModalHeader: undefined () react-bootstrap/Accordion: undefined () react-bootstrap/AccordionBody: undefined () react-bootstrap/AccordionButton: undefined () react-bootstrap/AccordionCollapse: undefined () react-bootstrap/AccordionContext: undefined () react-bootstrap/AccordionHeader: undefined () react-bootstrap/AccordionItem: undefined () react-bootstrap/AccordionItemContext: undefined () react-bootstrap/Alert: undefined () react-bootstrap/Anchor: undefined () react-bootstrap/Badge: undefined () react-bootstrap/BootstrapModalManager: undefined () react-bootstrap/Breadcrumb: undefined () react-bootstrap/BreadcrumbItem: undefined () react-bootstrap/Button: undefined () react-bootstrap/ButtonGroup: undefined () react-bootstrap/ButtonToolbar: undefined () react-bootstrap/Card: undefined () react-bootstrap/CardGroup: undefined () react-bootstrap/CardHeader: undefined () react-bootstrap/CardHeaderContext: undefined () react-bootstrap/CardImg: undefined () react-bootstrap/Carousel: undefined () react-bootstrap/CarouselCaption: undefined () react-bootstrap/CarouselItem: undefined () react-bootstrap/CloseButton: undefined () react-bootstrap/Col: undefined () react-bootstrap/Collapse: undefined () react-bootstrap/Container: undefined () react-bootstrap/Dropdown: undefined () react-bootstrap/DropdownButton: undefined () react-bootstrap/DropdownContext: undefined () react-bootstrap/DropdownItem: undefined () react-bootstrap/DropdownMenu: undefined () react-bootstrap/DropdownToggle: undefined () react-bootstrap/ElementChildren: undefined () react-bootstrap/Fade: undefined () react-bootstrap/Feedback: undefined () react-bootstrap/Figure: undefined () react-bootstrap/FigureCaption: undefined () react-bootstrap/FigureImage: undefined () react-bootstrap/FloatingLabel: undefined () react-bootstrap/Form: undefined () react-bootstrap/FormCheck: undefined () react-bootstrap/FormCheckInput: undefined () react-bootstrap/FormCheckLabel: undefined () react-bootstrap/FormContext: undefined () react-bootstrap/FormControl: undefined () react-bootstrap/FormFloating: undefined () react-bootstrap/FormGroup: undefined () react-bootstrap/FormLabel: undefined () react-bootstrap/FormRange: undefined () react-bootstrap/FormSelect: undefined () react-bootstrap/FormText: undefined () react-bootstrap/Image: undefined () react-bootstrap/InputGroup: undefined () react-bootstrap/InputGroupContext: undefined () react-bootstrap/ListGroup: undefined () react-bootstrap/ListGroupItem: undefined () react-bootstrap/Modal: undefined () react-bootstrap/ModalBody: undefined () react-bootstrap/ModalContext: undefined () react-bootstrap/ModalDialog: undefined () react-bootstrap/ModalFooter: undefined () react-bootstrap/ModalHeader: undefined () react-bootstrap/ModalTitle: undefined () react-bootstrap/Nav: undefined () react-bootstrap/NavContext: undefined () react-bootstrap/NavDropdown: undefined () react-bootstrap/NavItem: undefined () react-bootstrap/NavLink: undefined () react-bootstrap/Navbar: undefined () react-bootstrap/NavbarBrand: undefined () react-bootstrap/NavbarCollapse: undefined () react-bootstrap/NavbarContext: undefined () react-bootstrap/NavbarOffcanvas: undefined () react-bootstrap/NavbarToggle: undefined () react-bootstrap/Offcanvas: undefined () react-bootstrap/OffcanvasBody: undefined () react-bootstrap/OffcanvasHeader: undefined () react-bootstrap/OffcanvasTitle: undefined () react-bootstrap/OffcanvasToggling: undefined () react-bootstrap/Overlay: undefined () react-bootstrap/OverlayTrigger: undefined () react-bootstrap/PageItem: undefined () react-bootstrap/Pagination: undefined () react-bootstrap/Placeholder: undefined () react-bootstrap/PlaceholderButton: undefined () react-bootstrap/Popover: undefined () react-bootstrap/PopoverBody: undefined () react-bootstrap/PopoverHeader: undefined () react-bootstrap/ProgressBar: undefined () react-bootstrap/Ratio: undefined () react-bootstrap/Row: undefined () react-bootstrap/SSRProvider: undefined () react-bootstrap/Spinner: undefined () react-bootstrap/SplitButton: undefined () react-bootstrap/Stack: undefined () react-bootstrap/Switch: undefined () react-bootstrap/Tab: undefined () react-bootstrap/TabContainer: undefined () react-bootstrap/TabContent: undefined () react-bootstrap/TabPane: undefined () react-bootstrap/Table: undefined () react-bootstrap/Tabs: undefined () react-bootstrap/ThemeProvider: undefined () react-bootstrap/Toast: undefined () react-bootstrap/ToastBody: undefined () react-bootstrap/ToastContainer: undefined () react-bootstrap/ToastContext: undefined () react-bootstrap/ToastFade: undefined () react-bootstrap/ToastHeader: undefined () react-bootstrap/ToggleButton: undefined () react-bootstrap/ToggleButtonGroup: undefined () react-bootstrap/Tooltip: undefined () react-bootstrap/TransitionWrapper: undefined () react-bootstrap/createChainedFunction: undefined () react-bootstrap/createUtilityClasses: undefined () react-bootstrap/createWithBsPrefix: undefined () react-bootstrap/divWithClassName: undefined () react-bootstrap/getTabTransitionComponent: undefined () react-bootstrap/helpers: undefined () react-bootstrap/safeFindDOMNode: undefined () react-bootstrap/transitionEndListener: undefined () react-bootstrap/triggerBrowserReflow: undefined () react-bootstrap/types: undefined () react-bootstrap/useOverlayOffset: undefined () react-bootstrap/usePlaceholder: undefined () react-bootstrap/useWrappedRefWithWarning: undefined () react-copy-to-clipboard: ^5.0.3 => 5.0.4 react-dom: ^17.0.2 => 17.0.2 react-inlinesvg: ^2.3.0 => 2.3.0 react-intl: ^5.20.4 => 5.24.1 react-is: ^17.0.2 => 17.0.2 (16.13.1) react-query: ^3.34.12 => 3.34.16 react-query-devtools: 2.6.3 => 2.6.3 react-redux: ^7.2.4 => 7.2.6 react-router-dom: 6.2.1 => 6.2.1 react-scripts: 4.0.3 => 4.0.3 react-table: ^7.7.0 => 7.7.0 react-topbar-progress-indicator: ^4.1.0 => 4.1.1 redux: ^4.1.0 => 4.1.2 redux-persist: ^6.0.0 => 6.0.0 redux-persist/integration/react: undefined () redux-saga: ^1.1.3 => 1.1.3 redux-saga/effects: undefined () rtlcss-webpack-plugin: ^4.0.6 => 4.0.6 sass: 1.32.1 => 1.32.1 socicon: ^3.0.5 => 3.0.5 typescript: ^4.1.2 => 4.5.4 (4.4.4, 3.9.10) v: ^0.3.0 => 0.3.0 web-vitals: ^1.0.1 => 1.1.2 webpack-cli: ^4.8.0 => 4.9.1 yup: ^0.32.9 => 0.32.11 npmGlobalPackages: @aws-amplify/cli: 7.6.9 npm: 6.14.13 yarn: 1.22.17 ```

Describe the bug

Following documentation from here, it suggests that, when a new auth challenge is needed, the promise from login would resolve, providing the user object. This should occur when a new auth challenge is proposed. For example:

Auth.signIn(username, password)
.then(user => {
    if (user.challengeName === 'NEW_PASSWORD_REQUIRED') {
        const { requiredAttributes } = user.challengeParam; // the array of required attributes, e.g ['email', 'phone_number']
        Auth.completeNewPassword(
            user,               // the Cognito User Object
            newPassword,       // the new password
            // OPTIONAL, the required attributes
            {
              email: 'xxxx@example.com',
              phone_number: '1234567890'
            }
        ).then(user => {
            // at this time the user is logged in if no MFA required
            console.log(user);
        }).catch(e => {
          console.log(e);
        });
    } else {
        // other situations
    }
}).catch(e => {
    console.log(e);
});

After implementing this code, the promise is not resolved and instead rejected, resulting in the second to last line of just console logging the error. EG;

Screen Shot 2022-03-11 at 4 32 37 PM

Expected behavior

The auth call to to login would result in a resolved promise, providing the user object and the new auth challenge.

Reproduction steps

  1. Follow & implement the above code
  2. Create a user that needs to reset password
  3. See error

Code Snippet

import { Auth } from 'aws-amplify';

Auth.signIn(username, password)
.then(user => {
    if (user.challengeName === 'NEW_PASSWORD_REQUIRED') {
        const { requiredAttributes } = user.challengeParam; // the array of required attributes, e.g ['email', 'phone_number']
        Auth.completeNewPassword(
            user,               // the Cognito User Object
            newPassword,       // the new password
            // OPTIONAL, the required attributes
            {
              email: 'xxxx@example.com',
              phone_number: '1234567890'
            }
        ).then(user => {
            // at this time the user is logged in if no MFA required
            console.log(user);
        }).catch(e => {
          console.log(e);
        });
    } else {
        // other situations
    }
}).catch(e => {
    console.log(e);
});

Log output

``` VM15:1 POST https://cognito-idp.us-east-1.amazonaws.com/ 400 ```
### aws-exports.js

/* eslint-disable */
// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.

const awsmobile = {
    "aws_project_region": "us-east-1",
    "aws_cognito_identity_pool_id": "us-east-1:",
    "aws_cognito_region": "us-east-1",
    "aws_user_pools_id": "us-east-",
    "aws_user_pools_web_client_id": "",
    "oauth": {},
    "aws_cognito_username_attributes": [
        "EMAIL"
    ],
    "aws_cognito_social_providers": [],
    "aws_cognito_signup_attributes": [
        "EMAIL"
    ],
    "aws_cognito_mfa_configuration": "OFF",
    "aws_cognito_mfa_types": [
        "SMS"
    ],
    "aws_cognito_password_protection_settings": {
        "passwordPolicyMinLength": 8,
        "passwordPolicyCharacters": []
    },
    "aws_cognito_verification_mechanisms": [
        "EMAIL"
    ],
    "aws_appsync_graphqlEndpoint": "",
    "aws_appsync_region": "us-east-1",
    "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
    "aws_appsync_apiKey": "",
    "aws_cognito_login_mechanisms": [
        "EMAIL"
    ]
};

export default awsmobile;

Other Information

Response from request:

curl 'https://cognito-idp.us-east-1.amazonaws.com/' \
  -H 'authority: cognito-idp.us-east-1.amazonaws.com' \
  -H 'cache-control: max-age=0' \
  -H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Google Chrome";v="99"' \
  -H 'x-amz-user-agent: aws-amplify/5.0.4 js' \
  -H 'content-type: application/x-amz-json-1.1' \
  -H 'x-amz-target: AWSCognitoIdentityProviderService.InitiateAuth' \
  -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36' \
  --data-raw '{"AuthFlow":"USER_SRP_AUTH","ClientId":"...","AuthParameters":{"USERNAME":"...","SRP_A":"..."},"ClientMetadata":{}}' \
  --compressed
{"__type":"PasswordResetRequiredException","message":"Password reset required for the user"}
dperconti commented 2 years ago

Screenshot taken from AWS cognito user pool console: Screen Shot 2022-03-14 at 8 35 06 AM

The user titled RESET_REQUIRED is the user that is causing the login method to error. After looking through issues, it looks like this has been missing since January of 2019: https://github.com/aws-amplify/amplify-js/issues/2602. Are there any plans to have this implemented in amplify-js?

dperconti commented 2 years ago

Also potentially related: https://github.com/aws-amplify/aws-sdk-ios/issues/2298

vyomr13 commented 2 years ago

If your user is in status of RESET_REQUIRED, you would have to use the forgot password flow. If you are getting this error message from Auth.signIn, you need to use the Auth.forgotPassword function instead to send the user a verification code that you can then pass to Auth.forgotPasswordSubmit along with a new password the user inputs. Then the user should be able to login using Auth.signIn.

vyomr13 commented 2 years ago

Hi 👋 Closing this as we have not heard back from you. If you are still experiencing this issue and in need of assistance, please feel free to comment and provide us with any information previously requested by our team members so we can re-open this issue and be better able to assist you.

Thank you!

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server amplify-help forum.