[로그인 기능 개선] 로그인 처리 로직 간소화 및 에러 메시지 개선, 로그아웃 기능 리팩토링
This commit is contained in:
@@ -86,21 +86,14 @@ async function signIn() {
|
|||||||
isLoading.value = true;
|
isLoading.value = true;
|
||||||
errorMessage.value = "";
|
errorMessage.value = "";
|
||||||
|
|
||||||
try {
|
const result = await userStore.login(userId.value, password.value);
|
||||||
const result = await userStore.login(userId.value, password.value);
|
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
// 로그인 성공 시 홈으로 이동
|
await router.push("/");
|
||||||
await router.push("/");
|
} else {
|
||||||
} else {
|
errorMessage.value = result.error;
|
||||||
errorMessage.value = result.error || "로그인에 실패했습니다.";
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
errorMessage.value = "로그인 중 오류가 발생했습니다.";
|
|
||||||
console.error("로그인 오류:", error);
|
|
||||||
} finally {
|
|
||||||
isLoading.value = false;
|
|
||||||
}
|
}
|
||||||
|
isLoading.value = false;
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -16,53 +16,30 @@ export const useUserStore = defineStore(
|
|||||||
userId: string;
|
userId: string;
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
// 액션
|
|
||||||
const login = async (userId: string, password: string) => {
|
const login = async (userId: string, password: string) => {
|
||||||
try {
|
const { success, data, description } = await useApi<
|
||||||
// 실제 API 호출로 대체할 수 있습니다
|
ApiResponse<LoginData>
|
||||||
|
>("/login", {
|
||||||
|
method: "post",
|
||||||
|
body: { userId, password },
|
||||||
|
});
|
||||||
|
|
||||||
const { success, data } = await useApi<ApiResponse<LoginData>>(
|
if (success) {
|
||||||
"/login",
|
user.value = data;
|
||||||
{
|
isLoggedIn.value = true;
|
||||||
method: "post",
|
return { success, data };
|
||||||
body: { userId, password },
|
} else {
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (success) {
|
|
||||||
user.value = data;
|
|
||||||
isLoggedIn.value = true;
|
|
||||||
} else {
|
|
||||||
throw new Error("아이디 또는 비밀번호가 올바르지 않습니다.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return { success };
|
|
||||||
} catch (error: any) {
|
|
||||||
console.log(error);
|
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error:
|
error: description,
|
||||||
error?.response?.status === 401
|
|
||||||
? "아이디 또는 비밀번호가 올바르지 않습니다."
|
|
||||||
: error instanceof Error
|
|
||||||
? error.message
|
|
||||||
: "로그인에 실패했습니다.",
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const logout = async () => {
|
const logout = () => {
|
||||||
try {
|
user.value = null;
|
||||||
await useApi("/members/logout", {
|
isLoggedIn.value = false;
|
||||||
method: "post",
|
useApi("/members/logout", { method: "post" });
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("로그아웃 요청 실패:", error);
|
|
||||||
} finally {
|
|
||||||
// 로컬 상태 정리
|
|
||||||
user.value = null;
|
|
||||||
isLoggedIn.value = false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const checkAuth = () => {
|
const checkAuth = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user