Skip to content
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
34 changes: 33 additions & 1 deletion apps/web/src/apis/Auth/postEmailLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,45 @@ import { useMutation } from "@tanstack/react-query";

import type { AxiosError } from "axios";
import { useRouter } from "next/navigation";
import { SKIP_GLOBAL_ERROR_TOAST_META } from "@/lib/react-query/errorToastMeta";
import { showIconToast } from "@/lib/toast/showIconToast";
import useAuthStore from "@/lib/zustand/useAuthStore";
import { getCommunityRedirectOrFallback } from "@/utils/authRedirect";
import { type AuthRedirectOptions, authApi, type EmailLoginRequest, type EmailLoginResponse } from "./api";

const EMAIL_LOGIN_FAILURE_MESSAGE = "์ด๋ฉ”์ผ ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.";
const EMAIL_LOGIN_REQUEST_FAILURE_MESSAGE = "๋กœ๊ทธ์ธ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.";

type EmailLoginErrorResponse = {
message?: string;
};

const getEmailLoginErrorMessage = (error: AxiosError<EmailLoginErrorResponse>) => {
const responseMessage = error.response?.data?.message;

if (responseMessage) {
return responseMessage;
}

if (error.response?.status === 401) {
return EMAIL_LOGIN_FAILURE_MESSAGE;
}

return EMAIL_LOGIN_REQUEST_FAILURE_MESSAGE;
};

/**
* @description ์ด๋ฉ”์ผ ๋กœ๊ทธ์ธ์„ ์œ„ํ•œ useMutation ์ปค์Šคํ…€ ํ›…
*/
const usePostEmailAuth = ({ redirectPath }: AuthRedirectOptions = {}) => {
const { setAccessToken } = useAuthStore();
const router = useRouter();

return useMutation<EmailLoginResponse, AxiosError, EmailLoginRequest>({
return useMutation<EmailLoginResponse, AxiosError<EmailLoginErrorResponse>, EmailLoginRequest>({
mutationFn: (data) => authApi.postEmailLogin(data),
// ๋กœ๊ทธ์ธ API๋Š” publicAxiosInstance๋ฅผ ์‚ฌ์šฉํ•ด ์ „์—ญ 401 ์ธํ„ฐ์…‰ํ„ฐ/ํ† ์ŠคํŠธ๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ
// ์ „์—ญ ์—๋Ÿฌ ํ† ์ŠคํŠธ๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ  ์ด mutation์—์„œ ์ง์ ‘ ์ฒ˜๋ฆฌํ•œ๋‹ค.
meta: SKIP_GLOBAL_ERROR_TOAST_META,
onSuccess: (data) => {
const { accessToken } = data;

Expand All @@ -31,6 +56,13 @@ const usePostEmailAuth = ({ redirectPath }: AuthRedirectOptions = {}) => {
router.push(getCommunityRedirectOrFallback(redirectPath));
}, 100);
},
onError: (error) => {
const message = getEmailLoginErrorMessage(error);

// ๋กœ๊ทธ์ธ ์‹คํŒจ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ž๊ฒฉ์ฆ๋ช…์„ ๊ณ ์ณ ๊ณง๋ฐ”๋กœ ์žฌ์‹œ๋„ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„
// ๋™์ผ ๋ฉ”์‹œ์ง€ ์–ต์ œ(dedupe)๋ฅผ ๋„๊ณ  ๋งค ์‹œ๋„๋งˆ๋‹ค ํ† ์ŠคํŠธ๋ฅผ ๋…ธ์ถœํ•œ๋‹ค.
showIconToast("logo", message, { dedupe: false });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Prevent duplicate toasts for Enter submissions

When a user submits the login form with Enter, LoginContent.tsx invokes handleSubmit(onSubmit)() in its input onKeyDown handler and then allows the same key event to perform the form's native submit, producing two mutations before isPending can re-render the form. With deduplication explicitly disabled here, both error callbacks now render identical stacked toasts for a single keypress; remove/coalesce the duplicate submission path or otherwise suppress errors from the same user action while still allowing later retries.

Useful? React with ๐Ÿ‘ย / ๐Ÿ‘Ž.

},
});
};

Expand Down
8 changes: 0 additions & 8 deletions apps/web/src/app/login/LoginContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ const LoginContent = () => {
postEmailAuth(data);
};

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
handleSubmit(onSubmit)();
}
};

const handleBack = () => {
if (window.history.length > 1) {
router.back();
Expand Down Expand Up @@ -97,7 +91,6 @@ const LoginContent = () => {
{...register("email", {
onChange: handleEmailChange,
})}
onKeyDown={handleKeyDown}
/>
{errors.email && <p className="mt-1 text-red-500 typo-regular-4">{errors.email.message}</p>}
</div>
Expand All @@ -112,7 +105,6 @@ const LoginContent = () => {
placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ"
className="w-full rounded-lg border border-k-100 px-5 py-3 font-serif text-k-400 typo-regular-4 focus:outline-none"
{...register("password")}
onKeyDown={handleKeyDown}
/>
{errors.password && <p className="mt-1 text-red-500 typo-regular-4">{errors.password.message}</p>}
</div>
Expand Down
18 changes: 14 additions & 4 deletions apps/web/src/lib/toast/showIconToast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ const TOAST_DURATION = 3000;
const TOAST_COOLDOWN = 3500;
const activeToastKeys = new Set<string>();

export const showIconToast = (icon: ToastIconKey, message: string) => {
export type ShowIconToastOptions = {
/** ๋™์ผํ•œ ์•„์ด์ฝ˜+๋ฉ”์‹œ์ง€ ํ† ์ŠคํŠธ๋ฅผ TOAST_COOLDOWN ๋™์•ˆ ์–ต์ œํ• ์ง€ ์—ฌ๋ถ€ (๊ธฐ๋ณธ true) */
dedupe?: boolean;
};

export const showIconToast = (icon: ToastIconKey, message: string, options?: ShowIconToastOptions) => {
const Icon = ICONS[icon];
const key = `${icon}:${message}`;
const dedupe = options?.dedupe ?? true;

if (activeToastKeys.has(key)) return;
activeToastKeys.add(key);
if (dedupe) {
if (activeToastKeys.has(key)) return;
activeToastKeys.add(key);
}

toast.custom(
() => (
Expand All @@ -36,5 +44,7 @@ export const showIconToast = (icon: ToastIconKey, message: string) => {
{ duration: TOAST_DURATION },
);

setTimeout(() => activeToastKeys.delete(key), TOAST_COOLDOWN);
if (dedupe) {
setTimeout(() => activeToastKeys.delete(key), TOAST_COOLDOWN);
}
};
Loading