Skip to content

Commit

Permalink
Merge pull request #60 from My-Own-Weapon/feat/#59-loginCheck
Browse files Browse the repository at this point in the history
session 만료시에 login page로 리다이렉팅 기능 구현 - #59
  • Loading branch information
santaiscoming authored Jul 15, 2024
2 parents 59fca12 + 365a02d commit 0dd937c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/app/(post)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use client';

import BackHeader from '@/components/BackHeader';
import { useCheckSession } from '@/hooks/useCheckSession';
import { ReactNode } from 'react';

export default function layout({
export default function Layout({
children,
}: Readonly<{ children: ReactNode }>) {
useCheckSession();

return (
<>
<BackHeader />
Expand Down
5 changes: 4 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import { apiService } from '@/services/apiService';
import { Post, PostCardProps } from '@/components/Post';
import CategoryList from '@/components/CategoryList';
import LiveFriends, { Friend } from '@/components/LiveFriendsList';
import { useCheckSession } from '@/hooks/useCheckSession';

import s from './page.module.scss';

consoleArt();

interface PostReponse extends PostCardProps {
memberUsername: string;
category: string;
Expand All @@ -31,12 +34,12 @@ export type CategoriesKR = keyof typeof categoryMap;
export type CategoriesEN = (typeof categoryMap)[CategoriesKR];

export default function Home() {
consoleArt();
const [allPosts, setAllPosts] = useState<PostReponse[]>([]);
const [filteredPosts, setFilteredPosts] = useState<PostReponse[]>([]);
const [liveFriends, setLiveFriends] = useState<Friend[]>([]);
const [selectedCategory, setSelectedCategory] =
useState<CategoriesKR>('전부');
useCheckSession();
const path = usePathname();

const handleCategoryClick: MouseEventHandler = (e) => {
Expand Down
22 changes: 22 additions & 0 deletions src/hooks/useCheckSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { apiService } from '@/services/apiService';
import { useRouter } from 'next/navigation';
import { useEffect } from 'react';

export const useCheckSession = () => {
const router = useRouter();

useEffect(() => {
async function checkSession() {
try {
await apiService.checkSession();
} catch (error) {
if (error instanceof Error) {
alert(error.message);
router.push('/entry');
}
}
}

checkSession();
}, [router]);
};
17 changes: 17 additions & 0 deletions src/services/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ class ApiService {
return res.text();
}

async checkSession() {
const res = await fetch(`${this.baseUrl}/session`, {
method: 'GET',
credentials: 'include',
});

if (!res.ok) {
const { status, error } = await res.json();

throw new Error(
`[${status}: ${error}] 세션이 만료되었습니다. 다시 로그인해주세요`,
);
}

return res.text();
}

async fetchPosts() {
const res = await fetch(`${this.baseUrl}/posts`, {
method: 'GET',
Expand Down

0 comments on commit 0dd937c

Please sign in to comment.