Open danoshi opened 6 months ago
For completness below my HTML:
return (
<main className="flex min-h-screen flex-col p-6">
{/* ... Image and other code remains the same */}
<div className="flex justify-center flex-col gap-4 pt-20 md:pt-0 md:flex-row">
<Button onClick={handleSignOut}> Sign Out</Button>
{!showChangePassword && !showMFASetup && !showMFAVerify && (
<form onSubmit={handleSignIn}>
<Label htmlFor="email">Email</Label>
<Input
className="mt-2 mb-4 bg-transparent rounded-full "
type="username"
id="username"
placeholder="Email"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
<Label htmlFor="password">Password</Label>
<Input
className="mt-2 mb-4 bg-transparent rounded-full "
type="password"
id="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<Button type="submit" className="w-full mt-6 rounded-full ">
Login
</Button>
{error && <p className="text-red-500">{error}</p>}
</form>
)}
{showChangePassword && (
<form onSubmit={handleSignIn}>
<Input
className="mt-2 mb-4 bg-transparent rounded-full "
type="password"
placeholder="New Password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
/>
<Input
className="mt-2 mb-4 bg-transparent rounded-full "
type="password"
placeholder="Confirm New Password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
/>
<Button type="submit" className="w-full mt-6 rounded-full ">
Change Password
</Button>
</form>
)}
{showMFASetup && (
<form onSubmit={handleSignIn}>
{/* ... MFA setup form (provide instructions and input for MFA code) */}
<p>Please scan the QR code with your MFA app.</p>
{/* The code which is generated (display as QR code or clickable link) */}
<Canvas
text={setupUri}
options={{
errorCorrectionLevel: 'M',
margin: 3,
scale: 4,
width: 200,
color: {
dark: '#010599FF',
light: '#FFBF60FF',
},
}}
/>
{/* Input for the MFA code from the user */}
<Input
className="mt-2 mb-4 bg-transparent rounded-full"
type="text"
placeholder="Enter OTP code"
value={otpCode}
onChange={(e) => setOtpCode(e.target.value)}
/>
<Button type="submit" className="w-full mt-6 rounded-full">
Confirm MFA
</Button>
</form>
)}
{showMFAVerify && (
<form onSubmit={handleSignIn}>
{/* ... MFA verification form (input for MFA code) */}
<Input
className="mt-2 mb-4 bg-transparent rounded-full"
type="text"
placeholder="Enter OTP code"
value={otpCode}
onChange={(e) => setOtpCode(e.target.value)}
/>
<Button type="submit" className="w-full mt-6 rounded-full">
Submit
</Button>
</form>
)}
</div>
</main>
);
@danoshi, can you clarify what application you're using for your QR code? The code mismatch error is usually due to there being a "off by one" scenario due to API's running 2x, but want to understand how you've set up Auth better to diagnose this.
I actually scanned the QR code in my google authenticator app. To create the QR code i used the npm package next-qrcode.
Here is how I set it up:
import { useQRCode } from 'next-qrcode';
const { Canvas } = useQRCode();
<Canvas
text={setupUri} // the url from the object
options={{
errorCorrectionLevel: 'M',
margin: 3,
scale: 4,
width: 200,
color: {
dark: '#010599FF',
light: '#FFBF60FF',
},
}}
/>
@danoshi, can you confirm if you're getting the secrets back from Cognito when making your Authentication calls? Are you able to use the third party Authenticator app to manually input the code rather than using the QR code?
Hi, as you can see below I can confirm that I get the secret back and yes I tried to scan the code without success, as well as manuelly input it into the google authenticator app but this as well without success.
I always get code mismatch in the console as an error. | EnableSoftwareTokenMFAException: |
---|---|
x-amzn-errormessage | Code mismatch |
-- | -- |
x-amzn-requestid | 54d1174c-3f64-4222-adf4-3cd82d32b4c8 |
-- | -- |
Is there any update? @cwomack Unfortunately, I have the requirement to use my own custom UI so there is no way for me to use the Authenticator app.
hello @danoshi . Sorry for any inconvenience using the library. Can you share the request payload after calling confirmSignIn
with the 6 digit otp code retrieved from your authenticator app ?
also how is your app retrieving this otp code ?
Here is the payload
{"UserCode":"881868","Session":"AYABeOJlH5hsFwQrcuHccqKgvmUAHQABAAdTZXJ2aWNlABBDb2duaXRvVXNlclBvb2xzAAEAB2F3cy1rbXMATmFybjphd3M6a21zOmV1LWNlbnRyYWwtMTo1OTA0OTA4MDk4NTg6a2V5LzRkMmU1YTdmLTFjZDctNDljOS04ZmFjLTJkOWRjYjVjZWY5ZgC4AQIBAHhKQAoXAvz_-IkJpO_9S8oXPTdmr1hCCizB3KQHYdT5uAEdzkzPUcXMkbEznl7lGhRwAAAAfjB8BgkqhkiG9w0BBwagbzBtAgEAMGgGCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQMINHi4mLQMTZ7NgS2AgEQgDt1lJQ0bRzWe6__NxrNNWs4B2sIzVmoNcNxDqph6r0mch3CpIo8zXVnULU8NDHqTiFQKhtdbDmSY_R2vgIAAAAADAAAEAAAAAAAAAAAAAAAAAA1j6tAHsfuw3BVZrZvBQGG_____wAAAAEAAAAAAAAAAAAAAAEAAAEY9-DCREK3K9XfORIm5e7NktQAPdd421k3_CAUbRgOxYsjowFwKKobimvBfGJ6G88orNOu-GCeQ-_zwhjez9yqnMV6-TQv_6GzbcrBL6_fKY73azdwXNpjPSWUKIOx7B9hQopU-eZn02y5J_2MAO81boNAxh8AXxDQiWU7heCYdfosP6C9hvgY0eJrej0VTwCfVU5fTsv5hxlk94hJSytRCHbZwXLBYmFkCbC6lXu35iQER8FUeo4I8rEtzwMkdxfRrUigXWDPXyNbYNHfyU10-quP3eMwAUL16OB0FlinQNd_xld4fM1Syk992olgmsUgIxJlVbMk7aIZ1xP_aTpbzOeHz81z-GmjUe8pVBlZKGWgkGcHxKrDSc7s3jFozAde45FRKMtuWkQ"}
Request URL: https://cognito-idp.eu-central-1.amazonaws.com/ Request Method: POST Status Code: 400 Bad Request Referrer Policy: strict-origin-when-cross-origin Access-Control-Allow-Origin: * Access-Control-Expose-Headers: x-amzn-RequestId,x-amzn-ErrorType,x-amzn-ErrorMessage,Date Content-Length: 70 Content-Type: application/x-amz-json-1.1 Date: Fri, 17 May 2024 15:15:17 GMT X-Amzn-Errormessage: Code mismatch X-Amzn-Errortype: EnableSoftwareTokenMFAException: X-Amzn-Requestid: f3c995f9-1423-4289-94e5-ef503239d61f
As you can see in the code which I provided above I have an input field where I add the insert the mfa code and with a button I submit it
else if (nextStep.signInStep === 'CONTINUE_SIGN_IN_WITH_TOTP_SETUP') {
setShowMFASetup(true);
const totpSetupDetails = nextStep.totpSetupDetails;
const appName = 'my_app_name';
const setupUri = totpSetupDetails.getSetupUri(appName);
console.log(setupUri);
setsetupUri(setupUri.href);
const challengeResponse = otpCode;
const result = await confirmSignIn({
challengeResponse,
});
console.log(result);
<form onSubmit={handleSignIn}>
{/* ... MFA setup form (provide instructions and input for MFA code) */}
<p>Please scan the QR code with your MFA app.</p>
{/* The code which is generated (display as QR code or clickable link) */}
<Canvas
text={setupUri}
options={{
errorCorrectionLevel: 'M',
margin: 3,
scale: 4,
width: 200,
color: {
dark: '#010599FF',
light: '#FFBF60FF',
},
}}
/>
{/* Input for the MFA code from the user */}
<Input
className="mt-2 mb-4 bg-transparent rounded-full"
type="text"
placeholder="Enter the mfa"
value={otpCode}
onChange={(e) => setMfaCode(e.target.value)}
/>
<Button type="submit" className="w-full mt-6 rounded-full">
Confirm MFA
</Button>
</form>
Please let me know if I can provide more details to have it sorted out as soon as possible @israx @cwomack
I think the issue is that calling confirmSignIn
in the same block of the'CONTINUE_SIGN_IN_WITH_TOTP_SETUP'
clause is using a previous OTP code, hence the code miss match.
Can you separate the call of confirmSignIn
after getting the 'CONTINUE_SIGN_IN_WITH_TOTP_SETUP'
step ?
The flow should be something like:
receive 'CONTINUE_SIGN_IN_WITH_TOTP_SETUP'
step -> display QR code and input field -> retrieve otp code -> submit form -> call confirmSignIn
API
Thanks to your suggestions, the code works now @israx @cwomack ! I have two related questions:
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
Authentication
Amplify Version
v6
Amplify Categories
auth
Backend
Amplify CLI
Environment information
Describe the bug
My current use case is as follows: I manually create users in the Cognito pool, so I only use the signIn function. When a user receives their credentials via email, they will go to the page and attempt to log in for the first time. After the first login, they will be redirected to the change password page because it is mandatory. I receive the flag "CONFIRM_SIGN_IN_WITH_NEW_PASSWORD_REQUIRED", and the password change works fine when executing the following two lines.
Now, my user has the following flag: 'CONTINUE_SIGN_IN_WITH_TOTP_SETUP'. What is currently happening is that I create the QR code with the URL, scan it with my phone, but when I enter the code and submit it, I receive a 'Code Mismatch' error in the console.
Here is the code
I logged the setupUri object and what I see is that the username field is empty. So, my assumption is that it couldn't bind the MFA code with the user.
Here is the output of the log:
Here as well, the output after the successful challenge response when I changed the password:
Expected behavior
The MFA token matches, and the user successfully finishes the setup process.
Reproduction steps
In the describe the bug section I entered the code snippets.
Code Snippet
Log output
aws-exports.js
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response