Skip to content

Commit

Permalink
feat(core): redirect to account from auth pages if logged in (#1741)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanceaclark authored Dec 11, 2024
1 parent 71d2d37 commit 5136fac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-cougars-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": minor
---

If a customer is already logged in, we want to redirect them back to their account pages if they are trying to hit one of the non-logged-in customer auth routes. The prevents any side effects that may occur trying to re-auth the client. This is done by providing a root layout.tsx page under the (auth) route group.
19 changes: 19 additions & 0 deletions core/app/[locale]/(default)/(auth)/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PropsWithChildren } from 'react';

import { auth } from '~/auth';
import { redirect } from '~/i18n/routing';

interface Props extends PropsWithChildren {
params: Promise<{ locale: string }>;
}

export default async function Layout({ children, params }: Props) {
const session = await auth();
const { locale } = await params;

if (session) {
redirect({ href: '/account', locale });
}

return children;
}
11 changes: 11 additions & 0 deletions core/tests/ui/e2e/login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ test('Login fails with invalid credentials', async ({ page }) => {
),
).toBeVisible();
});

test('If a customer is logged in, redirect to account pages', async ({ page, account }) => {
const customer = await account.create();

await customer.login();
await page.waitForURL('/account/');
await page.goto('/login');
await page.waitForURL('/account/');

await expect(page.getByRole('heading', { name: 'My Account' })).toBeVisible();
});

0 comments on commit 5136fac

Please sign in to comment.