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(vue-demo): double opt in registration #1489

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/funny-chicken-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/composables-next": minor
---

Changed `registration` method in the `useUser` composable. Because of changes in the double opt-in on registration flow in the Shopware backend we are adjusting this method on our side. In new approach we are checking `active` and `doubleOptInRegistration` properties that represents current status of the user.
5 changes: 5 additions & 0 deletions .changeset/light-games-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vue-demo-store": patch
---

Fix registration form for the double opt in registration flow
patzick marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 25 additions & 0 deletions packages/composables/src/useUser/useUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ describe("useUser", () => {
expect(vm.isLoggedIn).toBe(true);
});

it("register function with double opt-in option", async () => {
BrocksiNet marked this conversation as resolved.
Show resolved Hide resolved
const { vm, injections } = useSetup(() => useUser());
injections.apiClient.invoke.mockResolvedValue({
data: {
active: true,
doubleOptInRegistration: true,
id: "test123",
guest: false,
},
});

await vm.register(REGISTRATION_DATA);

expect(injections.apiClient.invoke).toHaveBeenCalledWith(
expect.stringContaining("register"),
expect.objectContaining({
body: {
...REGISTRATION_DATA,
storefrontUrl: "http://localhost:3000", // This is the default value from the useInternationalization
},
}),
);
expect(vm.isLoggedIn).toBe(false);
});

it("logout", async () => {
const { vm, injections } = useSetup(() => useUser());
injections.apiClient.invoke.mockResolvedValue({ data: {} });
Expand Down
8 changes: 6 additions & 2 deletions packages/composables/src/useUser/useUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,12 @@ export function useUser(): UseUserReturn {
storefrontUrl: getStorefrontUrl(),
},
});
_user.value = data;
if (_user.value?.active) await refreshSessionContext();
// Update the user data in the context if the user is active and not using double opt-in registration set in the Shopware Admin
if (data.active && !data.doubleOptInRegistration) {
_user.value = data;
await refreshSessionContext();
BrocksiNet marked this conversation as resolved.
Show resolved Hide resolved
}

return data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,13 @@ const invokeSubmit = async () => {
try {
loading.value = true;
const response = await register(state);
if (response && response.active) router.push("/");
else if (response && !response.active) {
if (response && response.doubleOptInRegistration) {
Object.assign(state, JSON.parse(JSON.stringify(initialState)));
showDoubleOptInBox.value = true;
await nextTick();
doubleOptInBox.value?.scrollIntoView();
$v.value.$reset();
}
} else if (response && response.active) router.push("/");
BrocksiNet marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
if (error instanceof ApiClientError) {
const errors = resolveApiErrors(error.details.errors);
Expand Down
Loading