Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(authenticator): propagate username from signUp actor to top level machine #4923

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/new-badgers-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@aws-amplify/ui": patch
"@aws-amplify/ui-vue": patch
---

fix(authenticator): propagate username from signUp actor to top level machine
1 change: 1 addition & 0 deletions packages/ui/src/machines/authenticator/actors/signUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export function signUpActor({ services }: SignUpMachineOptions) {
remoteError: context.remoteError,
step: context.step,
totpSecretCode: context.totpSecretCode,
username: context.username,
unverifiedUserAttributes: context.unverifiedUserAttributes,
}),
},
Expand Down
12 changes: 0 additions & 12 deletions packages/vue/src/components/__tests__/setup-totp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,6 @@ describe('SetupTotp', () => {
);
});

it('does not call getTotpCodeURL if totpCodeURL is not present', () => {
useAuthenticatorSpy.mockReturnValue(
reactive({
...mockServiceFacade,
totpSecretCode: undefined,
})
);

render(SetupTotp, { global: { components } });
expect(getTotpCodeURLSpy).not.toHaveBeenCalled();
});

it('renders error if present', async () => {
useAuthenticatorSpy.mockReturnValueOnce(
reactive({
Expand Down
20 changes: 8 additions & 12 deletions packages/vue/src/components/setup-totp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ const logger = new Logger('SetupTotp-logger');

// `facade` is manually typed to `UseAuthenticator` for temporary type safety.
const facade: UseAuthenticator = useAuthenticator();
const { updateForm, submitForm, toSignIn } = facade;
const { error, isPending, QRFields, totpSecretCode, username } = toRefs(facade);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QRFields, totpSecretCode, username are static values and do not require reactivity

const { updateForm, submitForm, toSignIn, totpSecretCode, username, QRFields } =
facade;
const { error, isPending } = toRefs(facade);

const { totpIssuer = 'AWSCognito', totpUsername = username.value } =
QRFields.value ?? {};
const { totpIssuer = 'AWSCognito', totpUsername = username } = QRFields ?? {};

const totpCodeURL = totpSecretCode.value
? getTotpCodeURL(totpIssuer, totpUsername!, totpSecretCode.value)
: null;
// `totpSecretCode` is typed as `string | null` but will always be populated by the machine here
const totpCodeURL = getTotpCodeURL(totpIssuer, totpUsername, totpSecretCode!);
Comment on lines -27 to +28
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Truthiness check of totpSecretCode.value is no longer needed. Before migrating to [email protected] the totpSecretCode value needed to be generated by an additional async call, this check was a remnant of the previously required handling.

totpSecretCode is now provided directly via the state machine from the payload of a successful call to signIn, signUp, or confirmSignUp depending on the end user's current account status


const qrCode = reactive({
qrCodeImageSource: '',
Expand All @@ -40,17 +39,14 @@ const { getCopyText, getCopiedText, getBackToSignInText, getConfirmText } =
const copyTextLabel = ref(getCopyText());

function copyText() {
if (totpSecretCode.value) {
navigator.clipboard.writeText(totpSecretCode.value);
if (totpSecretCode) {
navigator.clipboard.writeText(totpSecretCode);
}
copyTextLabel.value = getCopiedText();
}

// lifecycle hooks
onMounted(async () => {
if (!username.value || !totpCodeURL) {
return;
}
try {
qrCode.qrCodeImageSource = await QRCode.toDataURL(totpCodeURL);
} catch (error) {
Expand Down
Loading