Skip to content

Conversation

@looks32
Copy link
Contributor

@looks32 looks32 commented Dec 10, 2025

저는 아름님 처럼 할 자신이 없어서
코드만 보는게 좋을 것 같아서 이렇게 정리했습니다.

강사님의 자료를 읽어보았지만 지금 수준에서는 이해도 되질 않네요.
이번주에 직접 설명을 들으면 좀 나을지도... 그래서

일단 분리를 한 다는 것에 중점을 두어서 해보았습니다.
일단 분리만 해도 많은 점이 개선되는 것 같습니다.
앞으로는 분리를 잘 하면서 작업을 해보아야겠습니다.

Copy link
Member

@aahreum aahreum left a comment

Choose a reason for hiding this comment

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

코멘트 남겼습니다~!!

Comment on lines +83 to +96
export function ProtectedRoute({ children }) {
const router = useRouter();
const token = storage.getToken();

useEffect(() => {
if (!token || isTokenExpired(token)) {
router.push('/login');
}
}, [token]);

if (!token) return null;
return children;
}

Copy link
Member

Choose a reason for hiding this comment

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

아 이렇게 Route로 하나 더 분리해볼 수도 있겠네요!!!
저는 이 부분을 생각을 못했던 것 같아요...🫠

Copy link
Contributor Author

Choose a reason for hiding this comment

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

이건 저도 gpt의 도움을 받아서 알게된 사실이에요!

Comment on lines +39 to +58
export const storage = {
getToken: () => localStorage.getItem('accessToken'),
};

// services/api.ts
// 서버와 통신만을 책임짐
// 백엔드와 통신만을 담당

// Pages, Hooks, Components 어디서든 API 로직 재사용 가능
// 에러 핸들링 일원화 가능
// 추후에 axios 같은 라이브러리로 변경시 여기에서만 바꿔주면 됨
export const api = {
getUser: async (token: string) => {
const res = await fetch('/api/user', {
headers: { Authorization: `Bearer ${token}` }
});
if (!res.ok) throw new Error('Failed to fetch');
return res.json();
}
};
Copy link
Member

Choose a reason for hiding this comment

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

객체 내부에 함수 선언하는 방식을 선호하시나요?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

그런건 없고 필요에 따라 사용하는 것 같아요!

Comment on lines +70 to +74
return useQuery({
queryKey: ['user', token],
queryFn: () => api.getUser(token!),
enabled: !!token && !isTokenExpired(token),
});
Copy link
Member

Choose a reason for hiding this comment

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

리액트 쿼리는 이렇게 사용하는거군요...😲

Comment on lines +40 to +41
getToken: () => localStorage.getItem('accessToken'),
};
Copy link
Member

Choose a reason for hiding this comment

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

만약 앱라우터 환경에서 사용하는거라고 가정한다면
window 타입 체크해도 좋을 것 같아요!

export const storage = {
  getToken() {
    if (typeof window === "undefined") return null;
    return localStorage.getItem("accessToken");
  }
};

아니면 getToken을 사용하는 곳에서는 'use Client'를 적어야 한다고 명시해도 좋구요!

Copy link
Contributor Author

@looks32 looks32 left a comment

Choose a reason for hiding this comment

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

댓글에 댓글 달때도 이런 코멘트를 달아야하는군요...

Comment on lines +39 to +58
export const storage = {
getToken: () => localStorage.getItem('accessToken'),
};

// services/api.ts
// 서버와 통신만을 책임짐
// 백엔드와 통신만을 담당

// Pages, Hooks, Components 어디서든 API 로직 재사용 가능
// 에러 핸들링 일원화 가능
// 추후에 axios 같은 라이브러리로 변경시 여기에서만 바꿔주면 됨
export const api = {
getUser: async (token: string) => {
const res = await fetch('/api/user', {
headers: { Authorization: `Bearer ${token}` }
});
if (!res.ok) throw new Error('Failed to fetch');
return res.json();
}
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

그런건 없고 필요에 따라 사용하는 것 같아요!

Comment on lines +83 to +96
export function ProtectedRoute({ children }) {
const router = useRouter();
const token = storage.getToken();

useEffect(() => {
if (!token || isTokenExpired(token)) {
router.push('/login');
}
}, [token]);

if (!token) return null;
return children;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

이건 저도 gpt의 도움을 받아서 알게된 사실이에요!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants