Skip to content

Commit

Permalink
Merge pull request #9 from shimdokite/mainPage
Browse files Browse the repository at this point in the history
[FE] ✨ 페이지 접근 권한 구현
  • Loading branch information
shimdokite authored Apr 4, 2024
2 parents bbca0b4 + 3650c6d commit 3adb406
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 37 deletions.
27 changes: 27 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"autoprefixer": "10.4.15",
"axios": "^1.5.0",
"framer-motion": "^10.16.4",
"js-cookie": "^3.0.5",
"lodash": "^4.17.21",
"next": "13.4.19",
"postcss": "8.4.29",
Expand All @@ -36,6 +37,7 @@
},
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/js-cookie": "^3.0.6",
"@types/lodash": "^4.14.197",
"@types/sockjs-client": "^1.5.4",
"@typescript-eslint/eslint-plugin": "^6.5.0",
Expand Down
16 changes: 0 additions & 16 deletions client/src/app/admin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
'use client';

import { useRouter } from 'next/navigation';

import useUserStore from '@/stores/userStore';

import { CheckInquiry } from '@/components/admin';

import { ADMIN_USER_ID } from '@/constants/values';

export default function Admin() {
const router = useRouter();

const { userId } = useUserStore();

if (userId !== ADMIN_USER_ID) {
router.replace('/');
}

return (
<div className="h-full flex justify-center items-center">
<CheckInquiry />
Expand Down
16 changes: 0 additions & 16 deletions client/src/app/middleware.ts

This file was deleted.

17 changes: 12 additions & 5 deletions client/src/components/common/HeaderNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import useChatStore from '@/stores/chatStore';

import useDeleteGuestMutation from '@/hooks/mutation/useDeleteGuestMutation';

import removeCookiesForUserId from '@/utils/removeCookiesForUserId';

interface HeaderNavProps {
isMenuHover?: boolean;
isProfileHover?: boolean;
Expand All @@ -28,10 +30,7 @@ export default function HeaderNav({
const { mutate: onDeleteGuest } = useDeleteGuestMutation();

const logout = () => {
if (isGuestMode) {
onDeleteGuest();
}

removeCookiesForUserId();
setClear();

getSigninForm(false);
Expand All @@ -40,6 +39,14 @@ export default function HeaderNav({
router.push('/');
};

const guestLogout = () => {
if (isGuestMode) {
onDeleteGuest();
getSigninForm(false);
getSignupForm(false);
}
};

return (
<>
<div className="absolute">
Expand All @@ -64,7 +71,7 @@ export default function HeaderNav({

<div
className="flex justify-center py-2 px-[2px] cursor-pointer"
onClick={logout}>
onClick={isGuestMode ? guestLogout : logout}>
{isGuestMode ? '게스트 종료' : '로그아웃'}
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions client/src/hooks/mutation/useCreateGuestMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { postCreateGuest } from '@/api/guest';

import useUserStore from '@/stores/userStore';

import setCookiesByUserId from '@/utils/setCookiesByUserId';

const useCreateGuestMutation = () => {
const { setGuestMode } = useUserStore();

Expand All @@ -17,6 +19,8 @@ const useCreateGuestMutation = () => {
const displayName = `게스트 ${userId}`;
const profileImageUrl = '/assets/img/bg_default_profile.png';

setCookiesByUserId(userId);

setGuestMode({
userId,
accessToken,
Expand Down
3 changes: 3 additions & 0 deletions client/src/hooks/mutation/useDeleteGuestMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { deleteUser } from '@/api/history';

import useUserStore from '@/stores/userStore';

import removeCookiesForUserId from '@/utils/removeCookiesForUserId';

const useDeleteGuestMutation = () => {
const router = useRouter();

Expand All @@ -15,6 +17,7 @@ const useDeleteGuestMutation = () => {
mutationFn: () => deleteUser(),

onSuccess: () => {
removeCookiesForUserId();
setClear();

router.push('/');
Expand Down
5 changes: 5 additions & 0 deletions client/src/hooks/mutation/useSigninMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { SigninFormValue } from '@/types/common';

import { ALERT_TEXT } from '@/constants/contents';

import setCookiesByUserId from '@/utils/setCookiesByUserId';

const useSigninMutation = () => {
const router = useRouter();

Expand Down Expand Up @@ -46,6 +48,9 @@ const useSigninMutation = () => {
profileImageUrl,
});

//TODO: userId가 cookie에 잘 들어오는지 확인하기.
setCookiesByUserId(userId);

getSigninForm(false);
getSignupForm(false);

Expand Down
4 changes: 4 additions & 0 deletions client/src/hooks/useGoogleLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useRouter } from 'next/navigation';

import useUserStore from '@/stores/userStore';

import setCookiesByUserId from '@/utils/setCookiesByUserId';

const useGoogleLogin = () => {
const googleOauth = process.env.NEXT_PUBLIC_GOOGLE_OAUTH_URL;

Expand Down Expand Up @@ -31,6 +33,8 @@ const useGoogleLogin = () => {
displayName &&
profileImageUrl
) {
setCookiesByUserId(userId);

setGoogleUser({
userId,
accessToken,
Expand Down
21 changes: 21 additions & 0 deletions client/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NextRequest, NextResponse } from 'next/server';

import { ADMIN_USER_ID } from '@/constants/values';

export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico|fonts|images).*)'],
};

const protectedPage = ['/admin'];

export function middleware(request: NextRequest) {
const userId = String(request.cookies.get('userId'));
const currentPath = request.nextUrl.pathname;

if (userId !== ADMIN_USER_ID && protectedPage.includes(currentPath)) {
const url = request.nextUrl.clone();
url.pathname = '/';

return NextResponse.redirect(url);
}
}
7 changes: 7 additions & 0 deletions client/src/utils/removeCookiesForUserId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Cookies from 'js-cookie';

const removeCookiesForUserId = () => {
return Cookies.remove('userId');
};

export default removeCookiesForUserId;
7 changes: 7 additions & 0 deletions client/src/utils/setCookiesByUserId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Cookies from 'js-cookie';

const setCookiesByUserId = (userId: string) => {
return Cookies.set('userId', userId, { expires: 1 });
};

export default setCookiesByUserId;

0 comments on commit 3adb406

Please sign in to comment.