Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/domains/community/api/fetchComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const postComments = async (postId: number | ParamValue, content: string)
};

export async function updateComment(
accessToken: string | null,
postId: number | ParamValue,
commentId: number,
content: string
Expand All @@ -57,8 +56,8 @@ export async function updateComment(
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
credentials: 'include',
body: JSON.stringify({ content }),
});

Expand All @@ -69,16 +68,10 @@ export async function updateComment(
}
}

export async function deleteComment(
accessToken: string | null,
postId: number | ParamValue,
commentId: number
): Promise<void> {
export async function deleteComment(postId: number | ParamValue, commentId: number): Promise<void> {
const response = await fetch(`${getApi}/posts/${postId}/comments/${commentId}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${accessToken}`,
},
credentials: 'include',
});

if (!response.ok) {
Expand Down
5 changes: 2 additions & 3 deletions src/domains/community/detail/Comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ type Props = {
};

function Comment({ postId }: Props) {
const { user, accessToken } = useAuthStore(
const { user } = useAuthStore(
useShallow((state) => ({
user: state.user,
accessToken: state.accessToken,
}))
);
const {
Expand All @@ -29,7 +28,7 @@ function Comment({ postId }: Props) {
handleAskDeleteComment,
handleConfirmDelete,
loadMoreComments,
} = useComments(postId, user, accessToken);
} = useComments(postId, user);

return (
<>
Expand Down
6 changes: 3 additions & 3 deletions src/domains/community/hook/useComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CommentType } from '../types/post';
import { User } from '@/domains/shared/store/auth';
import { ParamValue } from 'next/dist/server/request/params';

export function useComments(postId: ParamValue, user: User | null, accessToken?: string | null) {
export function useComments(postId: ParamValue, user: User | null) {
const [comments, setComments] = useState<CommentType[] | null>(null);
const [isEnd, setIsEnd] = useState(false);
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -31,7 +31,7 @@ export function useComments(postId: ParamValue, user: User | null, accessToken?:
return;
}
try {
await updateComment(accessToken!, postId, commentId, content);
await updateComment(postId, commentId, content);
setComments((prev) =>
prev
? prev.map((comment) =>
Expand Down Expand Up @@ -59,7 +59,7 @@ export function useComments(postId: ParamValue, user: User | null, accessToken?:
if (!deleteTarget) return;

try {
await deleteComment(accessToken!, deleteTarget.postId, deleteTarget.commentId);
await deleteComment(deleteTarget.postId, deleteTarget.commentId);
setComments((prev) =>
prev ? prev.filter((c) => c.commentId !== deleteTarget.commentId) : prev
);
Expand Down
8 changes: 8 additions & 0 deletions src/domains/login/components/ClientInitHook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

import { useFetchInterceptor } from '@/shared/hook/useFetchInterceptor';
import { useIdleLogout } from '../hook/useIdleLogout';
import { useEffect } from 'react';
import { useAuthStore } from '@/domains/shared/store/auth';

function ClientInitHook() {
const checkAuth = useAuthStore((state) => state.checkAuth);

useIdleLogout();
useFetchInterceptor();

useEffect(() => {
checkAuth();
}, [checkAuth]);
return null;
}
export default ClientInitHook;
2 changes: 1 addition & 1 deletion src/domains/login/hook/useIdleLogout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useLogout } from './useLogout';
import { useToast } from '@/shared/hook/useToast';
import { useAuthStore } from '@/domains/shared/store/auth';

const IDLE_TIMEOUT = 4 * 60 * 60 * 1000;
const IDLE_TIMEOUT = 1 * 60 * 1000;

export const useIdleLogout = () => {
const handleLogout = useLogout();
Expand Down
13 changes: 3 additions & 10 deletions src/domains/recipe/api/fetchRecipeComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const getRecipeComment = async (cocktailId: number): Promise<CommentType[
};

export async function updateComment(
accessToken: string | null,
postId: number,
commentId: number,
content: string
Expand All @@ -32,8 +31,8 @@ export async function updateComment(
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
credentials: 'include',
body: JSON.stringify({ content }),
});

Expand All @@ -44,16 +43,10 @@ export async function updateComment(
}
}

export async function deleteRecipeComment(
accessToken: string | null,
cocktailId: number,
commentId: number
): Promise<void> {
export async function deleteRecipeComment(cocktailId: number, commentId: number): Promise<void> {
const response = await fetch(`${getApi}/cocktails/${cocktailId}/comments/${commentId}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${accessToken}`,
},
credentials: 'include',
});

if (!response.ok) {
Expand Down
10 changes: 3 additions & 7 deletions src/domains/recipe/api/useRecipeComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { CommentType } from '@/domains/community/types/post';
import { deleteRecipeComment, getRecipeComment, updateComment } from './fetchRecipeComment';
import { useToast } from '@/shared/hook/useToast';

export function useRecipeComments(
cocktailId: number,
user: User | null,
accessToken: string | null
) {
export function useRecipeComments(cocktailId: number, user: User | null) {
const [comments, setComments] = useState<CommentType[] | null>(null);
const [isEnd, setIsEnd] = useState(false);
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -36,7 +32,7 @@ export function useRecipeComments(
return;
}
try {
await updateComment(accessToken!, cocktailId, commentId, content);
await updateComment(cocktailId, commentId, content);
setComments((prev) =>
prev
? prev.map((comment) =>
Expand All @@ -62,7 +58,7 @@ export function useRecipeComments(
if (!deleteTarget) return;

try {
await deleteRecipeComment(accessToken!, deleteTarget.cocktailId, deleteTarget.commentId);
await deleteRecipeComment(deleteTarget.cocktailId, deleteTarget.commentId);
setComments((prev) =>
prev ? prev.filter((c) => c.commentId !== deleteTarget.commentId) : prev
);
Expand Down
5 changes: 2 additions & 3 deletions src/domains/recipe/components/details/RecipeComment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ interface Props {
}

function RecipeComment({ cocktailId }: Props) {
const { user, accessToken } = useAuthStore(
const { user } = useAuthStore(
useShallow((state) => ({
user: state.user,
accessToken: state.accessToken,
}))
);

Expand Down Expand Up @@ -60,7 +59,7 @@ function RecipeComment({ cocktailId }: Props) {
deleteTarget,
handleConfirmDelete,
setDeleteTarget,
} = useRecipeComments(cocktailId, user, accessToken);
} = useRecipeComments(cocktailId, user);

return (
<div className="mb-10 border-t-1 border-gray">
Expand Down
125 changes: 72 additions & 53 deletions src/domains/shared/store/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getApi } from '@/app/api/config/appConfig';
import { create } from 'zustand';
import { persist } from 'zustand/middleware';

export interface User {
id: string;
Expand All @@ -13,68 +12,88 @@ export interface User {

interface AuthState {
user: User | null;
accessToken: string | null;
isLoggedIn: boolean;
setUser: (user: User, token: string) => void;
setUser: (user: User) => void;
logout: () => Promise<void>;
loginWithProvider: (provider: User['provider']) => void;

updateUser: () => Promise<User | null>;
checkAuth: () => Promise<User | null>;
}

export const useAuthStore = create<AuthState>()(
persist(
(set) => ({
user: null,
accessToken: null,
isLoggedIn: false,
export const useAuthStore = create<AuthState>()((set) => ({
user: null,
isLoggedIn: false,

loginWithProvider: (provider) => {
window.location.href = `${getApi}/oauth2/authorization/${provider}`;
},
loginWithProvider: (provider) => {
window.location.href = `${getApi}/oauth2/authorization/${provider}`;
},

setUser: (user, token) => {
const updatedUser = { ...user, abv_degree: 5.0 };
set({ user: updatedUser, accessToken: token, isLoggedIn: true });
},
setUser: (user) => {
const updatedUser = { ...user, abv_degree: user.abv_degree ?? 5.0 };
set({ user: updatedUser, isLoggedIn: true });
},

logout: async () => {
try {
await fetch(`${getApi}/user/auth/logout`, {
method: 'POST',
credentials: 'include',
});
set({ user: null, accessToken: null, isLoggedIn: false });
} catch (err) {
console.error('๋กœ๊ทธ์•„์›ƒ ์‹คํŒจ', err);
}
},
// ๋กœ๊ทธ์•„์›ƒ
logout: async () => {
try {
await fetch(`${getApi}/user/auth/logout`, {
method: 'POST',
credentials: 'include',
});
set({ user: null, isLoggedIn: false });
} catch (err) {
console.error('๋กœ๊ทธ์•„์›ƒ ์‹คํŒจ', err);
} finally {
set({ user: null, isLoggedIn: false });
}
},

updateUser: async () => {
try {
const res = await fetch(`${getApi}/user/auth/refresh`, {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
});
// idle + refresh ์‹œ ํ˜ธ์ถœ
updateUser: async () => {
try {
const res = await fetch(`${getApi}/user/auth/refresh`, {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
});

if (!res.ok) throw new Error('ํ† ํฐ ๊ฐฑ์‹  ์‹คํŒจ');
const data = await res.json();
const userInfo = data?.data?.user;
const accessToken = data?.data?.accessToken;
if (!res.ok) throw new Error('ํ† ํฐ ๊ฐฑ์‹  ์‹คํŒจ');

if (userInfo && accessToken) {
set({ user: userInfo, accessToken, isLoggedIn: true });
return userInfo;
}
return null;
} catch (err) {
console.error('updateUser ์‹คํŒจ', err);
set({ accessToken: null, user: null, isLoggedIn: false });
return null;
}
},
}),
{ name: 'auth-storage' } // localStorage key
)
);
const data = await res.json();
const userInfo = data?.data?.user;

if (userInfo) {
set({ user: userInfo, isLoggedIn: true });
return userInfo;
}
return null;
} catch (err) {
console.error('updateUser ์‹คํŒจ', err);
set({ user: null, isLoggedIn: false });
return null;
}
},

// ์‹œ์ž‘ ์‹œ ๋กœ๊ทธ์ธ ์ƒํƒœ ํ™•์ธ
checkAuth: async () => {
try {
const res = await fetch(`${getApi}/user/auth/me`, {
method: 'GET',
credentials: 'include',
});
if (!res.ok) throw new Error('์ธ์ฆ ์‹คํŒจ');

const data = await res.json();
const userInfo = data?.data?.user;
if (userInfo) {
set({ user: userInfo, isLoggedIn: true });
return userInfo;
}
return null;
} catch {
set({ user: null, isLoggedIn: false });
return null;
}
},
}));