diff --git a/services/dormitory-admin/src/apis/index.ts b/services/dormitory-admin/src/apis/index.ts index 861f71b..9bddabd 100644 --- a/services/dormitory-admin/src/apis/index.ts +++ b/services/dormitory-admin/src/apis/index.ts @@ -3,7 +3,7 @@ import { deleteCookie, getCookie, setCookie } from 'cookies-next'; import { customCookie } from '../libs/cookie'; import toast from 'react-hot-toast'; import router from 'next/router'; -import { UserToken } from './types'; +import { UserRefreshToken } from './types'; // const testToken = // 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI0NmZhNTQ1YS00NjA1LTQyMjgtYWE5OS1iZTNjZTBhMGIzMjMiLCJyb2xlIjoiRE9SIiwiZXhwIjoxNjc3ODM3NTY1LCJ0eXBlIjoiQUNDRVNTX1RPS0VOIiwiYXV0aG9yaXRpZXMiOlsiRE9SIl19.Ltfdsw3i9ADqXByA3RceznBySN3A68JRoswUNj67_04'; @@ -56,7 +56,7 @@ instanceArr.map((instance) => { const beforeRefresh = customCookie.get.refresh_token(); if (!beforeRefresh) throw error; - const response = await axios.put( + const response = await axios.put( `${xquareBaseUrl}/users/login`, {}, { diff --git a/services/dormitory-admin/src/apis/types.ts b/services/dormitory-admin/src/apis/types.ts index d2c7236..6071d76 100644 --- a/services/dormitory-admin/src/apis/types.ts +++ b/services/dormitory-admin/src/apis/types.ts @@ -53,13 +53,20 @@ export interface UserLogin { password: string; } -export interface UserToken { +export interface UserLoginToken { access_token: string; + access_token_expire_at: string; refresh_token: string; - expire_at: string; + refresh_token_expire_at: string; role: string; } +export interface UserRefreshToken { + access_token: string; + refresh_token: string; + expire_at: string; +} + export interface Category { category_id: string; name: string; diff --git a/services/dormitory-admin/src/apis/user.ts b/services/dormitory-admin/src/apis/user.ts index 9248a69..08de6cc 100644 --- a/services/dormitory-admin/src/apis/user.ts +++ b/services/dormitory-admin/src/apis/user.ts @@ -1,14 +1,15 @@ import { useRouter } from 'next/router'; import { useMutation } from 'react-query'; -import { UserLogin, UserToken } from './types'; +import { UserLogin, UserLoginToken } from './types'; import toast from 'react-hot-toast'; import { customCookie } from '../libs/cookie'; import axios from 'axios'; + export const useLoginMutation = () => { const router = useRouter(); return useMutation( async (param: UserLogin) => - axios.post(`${process.env.NEXT_PUBLIC_API_BASE_URL}/users/login`, { + axios.post(`${process.env.NEXT_PUBLIC_API_BASE_URL}/users/login`, { ...param, device_token: 'qwefsdfejk', }), @@ -17,12 +18,23 @@ export const useLoginMutation = () => { toast.error('로그인에 실패하였습니다.'); }, onSuccess: (res) => { - const { access_token, refresh_token, expire_at, role } = res.data; + const { + access_token, + refresh_token, + access_token_expire_at, + refresh_token_expire_at, + role, + } = res.data; if (role !== 'DOR') { toast.error('권한이 없습니다.\n권한이 있는 아이디로 로그인해주세요.'); } else { toast.success('로그인에 성공하였습니다.'); - customCookie.set.token(access_token, refresh_token, expire_at); + customCookie.set.token( + access_token, + refresh_token, + access_token_expire_at, + refresh_token_expire_at, + ); router.push('/point'); } }, diff --git a/services/dormitory-admin/src/libs/cookie.ts b/services/dormitory-admin/src/libs/cookie.ts index 18aa6ae..2f843a5 100644 --- a/services/dormitory-admin/src/libs/cookie.ts +++ b/services/dormitory-admin/src/libs/cookie.ts @@ -6,12 +6,19 @@ export const customCookie = { refresh_token: () => getCookie('refresh_token'), }, set: { - token: (access: string, refresh: string, expires: string) => { + token: ( + access: string, + refresh: string, + accessExpires: string, + refreshExpires?: string, + ) => { const date = new Date(); date.setDate(date.getDate() + 5); - setCookie('access_token', access, { expires: new Date(expires) }); - setCookie('refresh_token', refresh, { expires: date }); + setCookie('access_token', access, { expires: new Date(accessExpires) }); + setCookie('refresh_token', refresh, { + expires: refreshExpires ? new Date(refreshExpires) : date, + }); }, }, remove: {