260303_01_ENH: TanStack Query 적용
This commit is contained in:
@@ -5,99 +5,67 @@ import {Button} from "@/components/ui/button";
|
||||
import {Card, CardContent, CardHeader} from "@/components/ui/card";
|
||||
import {Input} from "@/components/ui/input";
|
||||
import React, {useEffect, useRef, useState} from "react";
|
||||
import { nextApi } from "@/lib/http-client";
|
||||
import {useSearchParams} from "next/navigation";
|
||||
import {LoginUser} from "@/types/auth";
|
||||
import {useRouter, useSearchParams} from "next/navigation";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { useLoading } from "@/hooks/useLoading";
|
||||
import { useStorage } from "@/hooks/useStorage"
|
||||
import { StorageKeys } from "@/store/storage/storage-keys";
|
||||
import {apiError} from "@/lib/http-util";
|
||||
import {NONE_HTTP_ERROR} from "@/const/commonConst";
|
||||
import {useLoginMutation} from "@/query/auth";
|
||||
|
||||
const LoginPage = () => {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const { auth, setFromLoginResponse } = useAuth();
|
||||
const { withLoadingAsync } = useLoading();
|
||||
const typeParam = searchParams.get("type") ?? "";
|
||||
|
||||
const { auth } = useAuth();
|
||||
const loginMutation = useLoginMutation();
|
||||
|
||||
const { get: getSeatNumberStorage, set: setSeatNumberStorage } = useStorage(StorageKeys.SeatNumber);
|
||||
const { get: getLoginTypeStorage, set: setLoginTypeStorage } = useStorage(StorageKeys.LoginType);
|
||||
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const [userId, setUserId] = useState<string>("");
|
||||
const [userPw, setUserPw] = useState<string>("");
|
||||
const [seatNum, setSeatNum] = useState<string>(getSeatNumberStorage());
|
||||
const [loginType, setLoginType] = useState<string>(getLoginTypeStorage());
|
||||
const [seatNum, setSeatNum] = useState<string>("");
|
||||
const [loginType, setLoginType] = useState<string>("");
|
||||
|
||||
const passwordInputRef = useRef<HTMLInputElement | null>(null);
|
||||
const seatNumberInputRef = useRef<HTMLInputElement | null>(null);
|
||||
|
||||
const tryLogin = () => {
|
||||
if(!userId){
|
||||
alert("아이디를 입력해 주세요");
|
||||
return;
|
||||
}
|
||||
if(!userPw){
|
||||
alert("PW를 입력해 주세요");
|
||||
return;
|
||||
}
|
||||
if(loginType === "call" && !seatNum){
|
||||
alert("자리번호를 입력해 주세요");
|
||||
return;
|
||||
}
|
||||
const tryLogin = async () => {
|
||||
if (!userId) return alert("아이디를 입력해 주세요");
|
||||
if (!userPw) return alert("PW를 입력해 주세요");
|
||||
if (loginType === "call" && !seatNum) return alert("자리번호를 입력해 주세요");
|
||||
|
||||
setError(null);
|
||||
const loginRequestBody = {userId, userPw};
|
||||
const loginRequestOptions = {autoLogoutOnAuthError: false};
|
||||
|
||||
// withLoading 래퍼로 API 호출 전/후 로딩 표시를 자동으로 처리
|
||||
return withLoadingAsync(() =>
|
||||
nextApi.post<LoginUser>(
|
||||
"/login", loginRequestBody, loginRequestOptions
|
||||
).then(async (data) => { // Jotai 기반 전역 인증 상태 갱신
|
||||
try {
|
||||
setSeatNumberStorage(seatNum);
|
||||
setLoginTypeStorage(loginType);
|
||||
} catch (e) {
|
||||
// 저장 실패는 치명적이지 않으므로 콘솔 로깅만
|
||||
console.warn("Failed to persist SeatNumber to secure storage:", e);
|
||||
}
|
||||
|
||||
setFromLoginResponse(data, seatNum);
|
||||
}).catch((e) => {
|
||||
const err = apiError(e);
|
||||
console.error(err.status, err.code, err.message);
|
||||
|
||||
if (err.status === 401 || err.status === 403) {
|
||||
setError("아이디 또는 비밀번호를 다시 확인해주세요.");
|
||||
}
|
||||
else if(err.code === NONE_HTTP_ERROR) {
|
||||
setError("알 수 없는 오류가 발생했습니다. 잠시 후 다시 시도해주세요.");
|
||||
}
|
||||
else {
|
||||
setError(`로그인에 실패했습니다. (status: ${err.status})`);
|
||||
}
|
||||
})
|
||||
);
|
||||
try {
|
||||
await loginMutation.mutateAsync({ userId, userPw, seatNum, loginType });
|
||||
} catch (e) {
|
||||
const err = apiError(e);
|
||||
if (err.status === 401 || err.status === 403) setError("아이디 또는 비밀번호를 다시 확인해주세요.");
|
||||
else if (err.code === NONE_HTTP_ERROR) setError("알 수 없는 오류가 발생했습니다. 잠시 후 다시 시도해주세요.");
|
||||
else setError(`로그인에 실패했습니다. (status: ${err.status})`);
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if(auth.user){
|
||||
window.location.replace("/");
|
||||
router.replace("/");
|
||||
}
|
||||
}, [auth.user]);
|
||||
|
||||
// URL 쿼리 파라미터에서 type 수신
|
||||
useEffect(() => {
|
||||
const typeParam = searchParams.get("type") ?? "";
|
||||
let seatNumTemp = "";
|
||||
|
||||
if(typeParam === "call") {
|
||||
seatNumTemp = getSeatNumberStorage();
|
||||
}
|
||||
setSeatNum(seatNumTemp);
|
||||
setLoginType(typeParam);
|
||||
}, [searchParams]);
|
||||
|
||||
if (typeParam === "call") {
|
||||
setSeatNum(getSeatNumberStorage() ?? "");
|
||||
} else {
|
||||
setSeatNum("");
|
||||
}
|
||||
|
||||
}, [typeParam]);
|
||||
|
||||
const handleIdKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (e) => {
|
||||
if (e.key === "Enter") {
|
||||
@@ -202,6 +170,7 @@ const LoginPage = () => {
|
||||
<Button
|
||||
type="submit"
|
||||
className="w-full mt-6 h-11"
|
||||
disabled={loginMutation.isPending}
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
tryLogin();
|
||||
|
||||
Reference in New Issue
Block a user