Skip to content

Commit

Permalink
fix(dormitory-admin): 로그인 response 값 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kimjh11130 committed Aug 21, 2023
1 parent 6e526ad commit 93d6ad3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
4 changes: 2 additions & 2 deletions services/dormitory-admin/src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -56,7 +56,7 @@ instanceArr.map((instance) => {
const beforeRefresh = customCookie.get.refresh_token();
if (!beforeRefresh) throw error;

const response = await axios.put<UserToken>(
const response = await axios.put<UserRefreshToken>(
`${xquareBaseUrl}/users/login`,
{},
{
Expand Down
11 changes: 9 additions & 2 deletions services/dormitory-admin/src/apis/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 16 additions & 4 deletions services/dormitory-admin/src/apis/user.ts
Original file line number Diff line number Diff line change
@@ -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<UserToken>(`${process.env.NEXT_PUBLIC_API_BASE_URL}/users/login`, {
axios.post<UserLoginToken>(`${process.env.NEXT_PUBLIC_API_BASE_URL}/users/login`, {
...param,
device_token: 'qwefsdfejk',
}),
Expand All @@ -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');
}
},
Expand Down
13 changes: 10 additions & 3 deletions services/dormitory-admin/src/libs/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 93d6ad3

Please sign in to comment.