Skip to content

Commit f1a0542

Browse files
committed
Merge remote-tracking branch 'origin/dev' into feat/recommend#116
2 parents 5d160a7 + 91b3b8e commit f1a0542

File tree

8 files changed

+96
-89
lines changed

8 files changed

+96
-89
lines changed

src/domains/community/api/fetchComment.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export const postComments = async (postId: number | ParamValue, content: string)
4848
};
4949

5050
export async function updateComment(
51-
accessToken: string | null,
5251
postId: number | ParamValue,
5352
commentId: number,
5453
content: string
@@ -57,8 +56,8 @@ export async function updateComment(
5756
method: 'PATCH',
5857
headers: {
5958
'Content-Type': 'application/json',
60-
Authorization: `Bearer ${accessToken}`,
6159
},
60+
credentials: 'include',
6261
body: JSON.stringify({ content }),
6362
});
6463

@@ -69,16 +68,10 @@ export async function updateComment(
6968
}
7069
}
7170

72-
export async function deleteComment(
73-
accessToken: string | null,
74-
postId: number | ParamValue,
75-
commentId: number
76-
): Promise<void> {
71+
export async function deleteComment(postId: number | ParamValue, commentId: number): Promise<void> {
7772
const response = await fetch(`${getApi}/posts/${postId}/comments/${commentId}`, {
7873
method: 'DELETE',
79-
headers: {
80-
Authorization: `Bearer ${accessToken}`,
81-
},
74+
credentials: 'include',
8275
});
8376

8477
if (!response.ok) {

src/domains/community/detail/Comment.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ type Props = {
1212
};
1313

1414
function Comment({ postId }: Props) {
15-
const { user, accessToken } = useAuthStore(
15+
const { user } = useAuthStore(
1616
useShallow((state) => ({
1717
user: state.user,
18-
accessToken: state.accessToken,
1918
}))
2019
);
2120
const {
@@ -29,7 +28,7 @@ function Comment({ postId }: Props) {
2928
handleAskDeleteComment,
3029
handleConfirmDelete,
3130
loadMoreComments,
32-
} = useComments(postId, user, accessToken);
31+
} = useComments(postId, user);
3332

3433
return (
3534
<>

src/domains/community/hook/useComment.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { CommentType } from '../types/post';
55
import { User } from '@/domains/shared/store/auth';
66
import { ParamValue } from 'next/dist/server/request/params';
77

8-
export function useComments(postId: ParamValue, user: User | null, accessToken?: string | null) {
8+
export function useComments(postId: ParamValue, user: User | null) {
99
const [comments, setComments] = useState<CommentType[] | null>(null);
1010
const [isEnd, setIsEnd] = useState(false);
1111
const [isLoading, setIsLoading] = useState(false);
@@ -31,7 +31,7 @@ export function useComments(postId: ParamValue, user: User | null, accessToken?:
3131
return;
3232
}
3333
try {
34-
await updateComment(accessToken!, postId, commentId, content);
34+
await updateComment(postId, commentId, content);
3535
setComments((prev) =>
3636
prev
3737
? prev.map((comment) =>
@@ -59,7 +59,7 @@ export function useComments(postId: ParamValue, user: User | null, accessToken?:
5959
if (!deleteTarget) return;
6060

6161
try {
62-
await deleteComment(accessToken!, deleteTarget.postId, deleteTarget.commentId);
62+
await deleteComment(deleteTarget.postId, deleteTarget.commentId);
6363
setComments((prev) =>
6464
prev ? prev.filter((c) => c.commentId !== deleteTarget.commentId) : prev
6565
);

src/domains/login/components/ClientInitHook.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
import { useFetchInterceptor } from '@/shared/hook/useFetchInterceptor';
44
import { useIdleLogout } from '../hook/useIdleLogout';
5+
import { useEffect } from 'react';
6+
import { useAuthStore } from '@/domains/shared/store/auth';
57

68
function ClientInitHook() {
9+
const checkAuth = useAuthStore((state) => state.checkAuth);
10+
711
useIdleLogout();
812
useFetchInterceptor();
13+
14+
useEffect(() => {
15+
checkAuth();
16+
}, [checkAuth]);
917
return null;
1018
}
1119
export default ClientInitHook;

src/domains/recipe/api/fetchRecipeComment.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export const getRecipeComment = async (cocktailId: number): Promise<CommentType[
2222
};
2323

2424
export async function updateComment(
25-
accessToken: string | null,
2625
postId: number,
2726
commentId: number,
2827
content: string
@@ -32,8 +31,8 @@ export async function updateComment(
3231
method: 'PATCH',
3332
headers: {
3433
'Content-Type': 'application/json',
35-
Authorization: `Bearer ${accessToken}`,
3634
},
35+
credentials: 'include',
3736
body: JSON.stringify({ content }),
3837
});
3938

@@ -44,16 +43,10 @@ export async function updateComment(
4443
}
4544
}
4645

47-
export async function deleteRecipeComment(
48-
accessToken: string | null,
49-
cocktailId: number,
50-
commentId: number
51-
): Promise<void> {
46+
export async function deleteRecipeComment(cocktailId: number, commentId: number): Promise<void> {
5247
const response = await fetch(`${getApi}/cocktails/${cocktailId}/comments/${commentId}`, {
5348
method: 'DELETE',
54-
headers: {
55-
Authorization: `Bearer ${accessToken}`,
56-
},
49+
credentials: 'include',
5750
});
5851

5952
if (!response.ok) {

src/domains/recipe/api/useRecipeComment.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import { CommentType } from '@/domains/community/types/post';
55
import { deleteRecipeComment, getRecipeComment, updateComment } from './fetchRecipeComment';
66
import { useToast } from '@/shared/hook/useToast';
77

8-
export function useRecipeComments(
9-
cocktailId: number,
10-
user: User | null,
11-
accessToken: string | null
12-
) {
8+
export function useRecipeComments(cocktailId: number, user: User | null) {
139
const [comments, setComments] = useState<CommentType[] | null>(null);
1410
const [isEnd, setIsEnd] = useState(false);
1511
const [isLoading, setIsLoading] = useState(false);
@@ -36,7 +32,7 @@ export function useRecipeComments(
3632
return;
3733
}
3834
try {
39-
await updateComment(accessToken!, cocktailId, commentId, content);
35+
await updateComment(cocktailId, commentId, content);
4036
setComments((prev) =>
4137
prev
4238
? prev.map((comment) =>
@@ -62,7 +58,7 @@ export function useRecipeComments(
6258
if (!deleteTarget) return;
6359

6460
try {
65-
await deleteRecipeComment(accessToken!, deleteTarget.cocktailId, deleteTarget.commentId);
61+
await deleteRecipeComment(deleteTarget.cocktailId, deleteTarget.commentId);
6662
setComments((prev) =>
6763
prev ? prev.filter((c) => c.commentId !== deleteTarget.commentId) : prev
6864
);

src/domains/recipe/components/details/RecipeComment.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ interface Props {
1313
}
1414

1515
function RecipeComment({ cocktailId }: Props) {
16-
const { user, accessToken } = useAuthStore(
16+
const { user } = useAuthStore(
1717
useShallow((state) => ({
1818
user: state.user,
19-
accessToken: state.accessToken,
2019
}))
2120
);
2221

@@ -60,7 +59,7 @@ function RecipeComment({ cocktailId }: Props) {
6059
deleteTarget,
6160
handleConfirmDelete,
6261
setDeleteTarget,
63-
} = useRecipeComments(cocktailId, user, accessToken);
62+
} = useRecipeComments(cocktailId, user);
6463

6564
return (
6665
<div className="mb-10 border-t-1 border-gray">

src/domains/shared/store/auth.ts

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { getApi } from '@/app/api/config/appConfig';
22
import { create } from 'zustand';
3-
import { persist } from 'zustand/middleware';
43

54
export interface User {
65
id: string;
@@ -13,68 +12,88 @@ export interface User {
1312

1413
interface AuthState {
1514
user: User | null;
16-
accessToken: string | null;
1715
isLoggedIn: boolean;
18-
setUser: (user: User, token: string) => void;
16+
setUser: (user: User) => void;
1917
logout: () => Promise<void>;
2018
loginWithProvider: (provider: User['provider']) => void;
2119

2220
updateUser: () => Promise<User | null>;
21+
checkAuth: () => Promise<User | null>;
2322
}
2423

25-
export const useAuthStore = create<AuthState>()(
26-
persist(
27-
(set) => ({
28-
user: null,
29-
accessToken: null,
30-
isLoggedIn: false,
24+
export const useAuthStore = create<AuthState>()((set) => ({
25+
user: null,
26+
isLoggedIn: false,
3127

32-
loginWithProvider: (provider) => {
33-
window.location.href = `${getApi}/oauth2/authorization/${provider}`;
34-
},
28+
loginWithProvider: (provider) => {
29+
window.location.href = `${getApi}/oauth2/authorization/${provider}`;
30+
},
3531

36-
setUser: (user, token) => {
37-
const updatedUser = { ...user, abv_degree: 5.0 };
38-
set({ user: updatedUser, accessToken: token, isLoggedIn: true });
39-
},
32+
setUser: (user) => {
33+
const updatedUser = { ...user, abv_degree: user.abv_degree ?? 5.0 };
34+
set({ user: updatedUser, isLoggedIn: true });
35+
},
4036

41-
logout: async () => {
42-
try {
43-
await fetch(`${getApi}/user/auth/logout`, {
44-
method: 'POST',
45-
credentials: 'include',
46-
});
47-
set({ user: null, accessToken: null, isLoggedIn: false });
48-
} catch (err) {
49-
console.error('로그아웃 실패', err);
50-
}
51-
},
37+
// 로그아웃
38+
logout: async () => {
39+
try {
40+
await fetch(`${getApi}/user/auth/logout`, {
41+
method: 'POST',
42+
credentials: 'include',
43+
});
44+
set({ user: null, isLoggedIn: false });
45+
} catch (err) {
46+
console.error('로그아웃 실패', err);
47+
} finally {
48+
set({ user: null, isLoggedIn: false });
49+
}
50+
},
5251

53-
updateUser: async () => {
54-
try {
55-
const res = await fetch(`${getApi}/user/auth/refresh`, {
56-
method: 'POST',
57-
credentials: 'include',
58-
headers: { 'Content-Type': 'application/json' },
59-
});
52+
// idle + refresh 시 호출
53+
updateUser: async () => {
54+
try {
55+
const res = await fetch(`${getApi}/user/auth/refresh`, {
56+
method: 'POST',
57+
credentials: 'include',
58+
headers: { 'Content-Type': 'application/json' },
59+
});
6060

61-
if (!res.ok) throw new Error('토큰 갱신 실패');
62-
const data = await res.json();
63-
const userInfo = data?.data?.user;
64-
const accessToken = data?.data?.accessToken;
61+
if (!res.ok) throw new Error('토큰 갱신 실패');
6562

66-
if (userInfo && accessToken) {
67-
set({ user: userInfo, accessToken, isLoggedIn: true });
68-
return userInfo;
69-
}
70-
return null;
71-
} catch (err) {
72-
console.error('updateUser 실패', err);
73-
set({ accessToken: null, user: null, isLoggedIn: false });
74-
return null;
75-
}
76-
},
77-
}),
78-
{ name: 'auth-storage' } // localStorage key
79-
)
80-
);
63+
const data = await res.json();
64+
const userInfo = data?.data?.user;
65+
66+
if (userInfo) {
67+
set({ user: userInfo, isLoggedIn: true });
68+
return userInfo;
69+
}
70+
return null;
71+
} catch (err) {
72+
console.error('updateUser 실패', err);
73+
set({ user: null, isLoggedIn: false });
74+
return null;
75+
}
76+
},
77+
78+
// 시작 시 로그인 상태 확인
79+
checkAuth: async () => {
80+
try {
81+
const res = await fetch(`${getApi}/user/auth/me`, {
82+
method: 'GET',
83+
credentials: 'include',
84+
});
85+
if (!res.ok) throw new Error('인증 실패');
86+
87+
const data = await res.json();
88+
const userInfo = data?.data?.user;
89+
if (userInfo) {
90+
set({ user: userInfo, isLoggedIn: true });
91+
return userInfo;
92+
}
93+
return null;
94+
} catch {
95+
set({ user: null, isLoggedIn: false });
96+
return null;
97+
}
98+
},
99+
}));

0 commit comments

Comments
 (0)