I'm trying to implement user login with username and password on a spa using aws cognito, but I keep getting error on the auth flow.
Previously I was using 'amazon-cognito-identity-js' and it was all ok, but I'm moving to '@aws-sdk/client-cognito-identity-provider' and now I can't even login my user.
From web console I can see that Cognito app client enabled auth flows are
ALLOW_REFRESH_TOKEN_AUTH
ALLOW_CUSTOM_AUTH
ALLOW_USER_SRP_AUTH
.
I tried almost every AuthFlowType
value but browser console logs following error or ask for additional required params based on the auth flow tested.
Browser console log error
Uncaught (in promise) InvalidParameterException: Initiate Auth method not supported.
Login command code:
command = new AdminInitiateAuthCommand({
ClientId: AWS_COGNITO_APP_CLIENT_ID,
UserPoolId: AWS_COGNITO_USER_POOL_ID,
AuthFlow: AuthFlowType.<WHAT_VALUE_TO_INSERT_?>,
AuthParameters: {
USERNAME: username,
PASSWORD: password
}
});
Update#1
I added the ALLOW_USER_PASSWORD_AUTH
flag to the allowed auth flows for the app client from the web console and used the AuthFlowType.USER_PASSWORD_AUTH
value to login, but still I'm getting the same error.
An option that works for SPA is to use the Amplify Libraries, it has an Authentication API which abstracts the logic to call Cognito user pools to authenticate. You can configure the Amplify Authentication library as per this link:
https://docs.amplify.aws/lib/auth/start/q/platform/js/#re-use-existing-authentication-resource
After that you can sign in using the Auth.signIn method: https://docs.amplify.aws/lib/auth/emailpassword/q/platform/js/#sign-in