Skip to content

Commit

Permalink
fix(core): improve redirect behavior after change password on account…
Browse files Browse the repository at this point in the history
… page (#1338)
  • Loading branch information
bc-yevhenii-buliuk authored Sep 10, 2024
1 parent f4ac3a7 commit d50613a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-meals-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

improve redirect behavior after change password on account page
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const AccountStatusProvider = ({ children }: { children: ReactNode }) =>
const pathname = usePathname();

useEffect(() => {
// Reset account state when changing the route except the Account Page
if (pathname !== '/account/' && pathname !== '/login/') {
setAccountState(defaultState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
Input,
} from '~/components/ui/form';
import { Message } from '~/components/ui/message';
import { useRouter } from '~/i18n/routing';

import { useAccountStatusContext } from '../../_components/account-status-provider';
import { submitCustomerChangePasswordForm } from '../_actions/submit-customer-change-password-form';

const ChangePasswordFieldsSchema = z.object({
Expand Down Expand Up @@ -95,7 +95,6 @@ const SubmitButton = () => {
};

export const ChangePasswordForm = () => {
const router = useRouter();
const form = useRef<HTMLFormElement>(null);
const t = useTranslations('Account.ChangePassword');
const [state, formAction] = useFormState(submitCustomerChangePasswordForm, {
Expand All @@ -107,14 +106,18 @@ export const ChangePasswordForm = () => {
const [isNewPasswordValid, setIsNewPasswordValid] = useState(true);
const [isConfirmPasswordValid, setIsConfirmPasswordValid] = useState(true);

const { setAccountState } = useAccountStatusContext();

useEffect(() => {
if (state.status === 'success') {
setTimeout(() => {
void logout();
router.push('/login');
}, 2000);
void logout();

setAccountState({
status: 'success',
message: t('confirmChangePassword'),
});
}
}, [state, router]);
}, [state, setAccountState, t]);

let messageText = '';

Expand Down Expand Up @@ -157,7 +160,7 @@ export const ChangePasswordForm = () => {

return (
<>
{(state.status === 'error' || state.status === 'success') && (
{state.status === 'error' && (
<Message className="mb-8 w-full text-gray-500" variant={state.status}>
<p>{messageText}</p>
</Message>
Expand Down
6 changes: 5 additions & 1 deletion core/components/header/_actions/logout.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use server';

import { redirect } from 'next/navigation';

import { signOut } from '~/auth';

export const logout = async () => {
await signOut();
await signOut({ redirect: false });

redirect('/login');
};

0 comments on commit d50613a

Please sign in to comment.