refactor: 인증 및 로그인 로직 수정
This commit is contained in:
@@ -1,44 +1,47 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { useQuery } from "@tanstack/react-query";
|
|
||||||
import { client } from "@/shared/api";
|
import { client } from "@/shared/api";
|
||||||
import { ENDPOINT, AUTH_USER_ID } from "@/shared/config";
|
|
||||||
import { getCookie, useUserStore } from "@/shared/lib";
|
|
||||||
import type { IUserInfo } from "@/shared/store";
|
import type { IUserInfo } from "@/shared/store";
|
||||||
|
import { getCookie, useUserStore } from "@/shared/lib";
|
||||||
|
import { ENDPOINT, AUTH_USER_ID } from "@/shared/config";
|
||||||
import { LoginApi } from "../api/login-service";
|
import { LoginApi } from "../api/login-service";
|
||||||
|
|
||||||
const AuthWrapper = ({ children }: { children: React.ReactNode }) => {
|
const AuthWrapper = ({ children }: { children: React.ReactNode }) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { setUserInfo } = useUserStore((state) => state);
|
const { userInfo, setUserInfo } = useUserStore((state) => state);
|
||||||
|
|
||||||
useQuery({
|
async function authCheck() {
|
||||||
queryKey: [ENDPOINT.USER_SERVICE.USERS],
|
const userId = getCookie(AUTH_USER_ID);
|
||||||
queryFn: async () => {
|
if (!userId) {
|
||||||
|
throw new Error("Missing required authentication tokens");
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await client.get<IUserInfo>(
|
||||||
|
`${ENDPOINT.USER_SERVICE.USERS}/${userId}`,
|
||||||
|
);
|
||||||
|
if (!userInfo) setUserInfo(response.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const userId = getCookie(AUTH_USER_ID);
|
await authCheck();
|
||||||
|
|
||||||
if (!userId) {
|
|
||||||
throw new Error("Missing required authentication tokens");
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await client.get<IUserInfo>(
|
|
||||||
`${ENDPOINT.USER_SERVICE.USERS}/${userId}`,
|
|
||||||
);
|
|
||||||
setUserInfo(response.data);
|
|
||||||
return response;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await LoginApi.silentRefresh();
|
await LoginApi.silentRefresh();
|
||||||
|
await authCheck();
|
||||||
} catch (refreshError) {
|
} catch (refreshError) {
|
||||||
console.error("Token refresh failed:", refreshError);
|
console.error("Token refresh failed:", refreshError);
|
||||||
setUserInfo(null);
|
setUserInfo(null);
|
||||||
router.push("/auth/login");
|
router.push("/auth/login");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
})();
|
||||||
});
|
}, []);
|
||||||
|
|
||||||
return <>{children}</>;
|
return <>{children}</>;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ import Image from "next/image";
|
|||||||
import { images } from "@common/assets";
|
import { images } from "@common/assets";
|
||||||
import { LoginApi } from "@/entities/auth";
|
import { LoginApi } from "@/entities/auth";
|
||||||
import "./login.css";
|
import "./login.css";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
|
||||||
function LoginPage() {
|
function LoginPage() {
|
||||||
|
const router = useRouter();
|
||||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
@@ -13,7 +15,8 @@ function LoginPage() {
|
|||||||
const userId = formData.get("id") as string;
|
const userId = formData.get("id") as string;
|
||||||
const password = formData.get("password") as string;
|
const password = formData.get("password") as string;
|
||||||
|
|
||||||
LoginApi.login({ userId, password });
|
await LoginApi.login({ userId, password });
|
||||||
|
router.push("/site");
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user