Skip to content

Commit

Permalink
Merge pull request #231 from Kernel360/fix-react-cookie
Browse files Browse the repository at this point in the history
환경 설정: 쿠키 에러 수정
  • Loading branch information
bottlewook authored Feb 29, 2024
2 parents a136a0c + 5c0512f commit 7736c76
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/app/my-page/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import styles from './page.module.scss';

const cx = classNames.bind(styles);

const TOP_MARGIN = 96;
const BOTTOM_MARGIN = 97;

function MyProfilePage() {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [cookies, removeCookie] = useCookies(['token']);
Expand All @@ -40,18 +43,15 @@ function MyProfilePage() {
logout();
};

const topMargin = 96;
const bottomMargin = 97;

if (isLoggedIn === false) {
return (
<>
<Confirmation
title={REQUIRED_LOGIN.title}
description={REQUIRED_LOGIN.description}
options={REQUIRED_LOGIN.options}
topMargin={topMargin}
bottomMargin={bottomMargin}
topMargin={TOP_MARGIN}
bottomMargin={BOTTOM_MARGIN}
/>
<BottomNav />
</>
Expand Down
5 changes: 1 addition & 4 deletions src/hooks/useLoggedOut.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { useCookies } from 'react-cookie';

import { useQueryClient } from '@tanstack/react-query';
import { useRouter } from 'next/navigation';

import { useAppDispatch } from '@stores/hooks';
import { clearUserId } from '@stores/slices/userSlice';
import { removeCookie } from '@utils/cookies';

function useLoggedOut() {
const router = useRouter();
const dispatch = useAppDispatch();
const query = useQueryClient();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [cookies, removeCookie] = useCookies(['token']);

const logout = (redirectPath = '/') => {
dispatch(clearUserId());
Expand Down
10 changes: 4 additions & 6 deletions src/remote/api/common.api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-param-reassign */
import { useCookies } from 'react-cookie';

import axios, { InternalAxiosRequestConfig, AxiosError, AxiosResponse } from 'axios';

import { axiosInstance } from '@remote/api/instance.api';
import { getCookie } from '@utils/cookies';
import logOnDev from '@utils/logOnDev';

axiosInstance.interceptors.request.use(
Expand All @@ -12,11 +12,9 @@ axiosInstance.interceptors.request.use(
* request 직전 공통으로 진행할 작업
*/
if (config && config.headers) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [cookies, setCookie] = useCookies(['token']);

if (cookies.token) {
config.headers.Authorization = cookies.token as string;
const token = getCookie('token') as string;
if (token) {
config.headers.Authorization = token;
config.headers['Content-Type'] = 'application/json';
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/remote/queries/auth/useLogin.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { useCookies } from 'react-cookie';

import { useMutation } from '@tanstack/react-query';
import { useRouter } from 'next/navigation';

import { login } from '@remote/api/requests/auth/auth.post.api';
import { UserInfoType } from '@remote/api/types/auth';
import { useAppDispatch } from '@stores/hooks';
import { setUserId } from '@stores/slices/userSlice';
import { setCookie } from '@utils/cookies';

function useLogin() {
const router = useRouter();
const dispatch = useAppDispatch();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [cookies, setCookie] = useCookies(['token']);

const onSuccess = (data : UserInfoType) => {
const onSuccess = (data: UserInfoType) => {
const { id, jwtToken } = data.value;
const cookieOptions = { path: '/', maxAge: 60 * 15 };

Expand Down
19 changes: 19 additions & 0 deletions src/types/cookies.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface CookieGetOptions {
doNotParse?: boolean;
doNotUpdate?: boolean;
}
export interface CookieSetOptions {
path?: string;
expires?: Date;
maxAge?: number;
domain?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: boolean | 'none' | 'lax' | 'strict';
}
export interface CookieChangeOptions {
name: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value?: any;
options?: CookieSetOptions;
}
18 changes: 18 additions & 0 deletions src/utils/cookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Cookies } from 'react-cookie';

import { CookieGetOptions, CookieSetOptions } from '@/types/cookies.type';

const cookies = new Cookies();

export const setCookie = (name: string, value: string, options?: CookieSetOptions) => {
return cookies.set(name, value, { ...options });
};

export const getCookie = (name: string, options?: CookieGetOptions) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return cookies.get(name, options);
};

export const removeCookie = (name: string, options?: CookieSetOptions) => {
return cookies.remove(name, options);
};

0 comments on commit 7736c76

Please sign in to comment.