Skip to content

Commit

Permalink
요청 헤더에 Authorization 필드 추가 (#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaymyong66 authored Jan 12, 2025
1 parent b1692dc commit ecf28c6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
15 changes: 12 additions & 3 deletions frontend/src/api/ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export class ApiClient {
this.credentials = credentials || 'same-origin';
}

private getAuthorizationHeader(): HeadersInit {
const token = localStorage.getItem('authorization');
return token ? { Authorization: token } : {};
}

async get(endpoint: string, params?: RequestParams): Promise<Response> {
const url = new URL(`${this.baseUrl}${endpoint}`);

Expand All @@ -63,7 +68,10 @@ export class ApiClient {
try {
const response = await fetch(url, {
method,
headers: this.headers,
headers: {
...this.headers,
...this.getAuthorizationHeader(),
},
credentials: this.credentials,
body: body ? JSON.stringify(body) : null,
});
Expand All @@ -83,13 +91,14 @@ export class ApiClient {
}

private async handleError(response: Response) {
const { errorCode, instance, detail } = await response.json();

if (response.status === HTTP_STATUS.UNAUTHORIZED) {
localStorage.removeItem('authorization');
localStorage.removeItem('name');
localStorage.removeItem('memberId');
}

const { errorCode, instance, detail } = await response.json();

throw new ApiError(getErrorMessage(errorCode, instance), response.status, errorCode, detail);
}
}
6 changes: 1 addition & 5 deletions frontend/src/api/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import type { LoginRequest, SignupRequest } from '@/types';

export const postSignup = async (signupInfo: SignupRequest) => await apiClient.post(END_POINTS.SIGNUP, signupInfo);

export const postLogin = async (loginInfo: LoginRequest) => {
const response = await apiClient.post(END_POINTS.LOGIN, loginInfo);

return await response.json();
};
export const postLogin = async (loginInfo: LoginRequest) => await apiClient.post(END_POINTS.LOGIN, loginInfo);

export const postLogout = async () => await apiClient.post(END_POINTS.LOGOUT, {});

Expand Down
5 changes: 2 additions & 3 deletions frontend/src/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export const API_URL = process.env.REACT_APP_API_URL ?? 'https://default-url.com

const httpHeader = {
'Content-Type': 'application/json',
};
const httpCredentials = 'include';
}

export const apiClient = new ApiClient(API_URL, httpHeader, httpCredentials);
export const apiClient = new ApiClient(API_URL, httpHeader);
12 changes: 8 additions & 4 deletions frontend/src/queries/authentication/useLoginMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ export const useLoginMutation = () => {
const { failAlert, successAlert } = useCustomContext(ToastContext);
const navigate = useCustomNavigate();

return useMutation<MemberInfo, Error, LoginRequest>({
return useMutation<Response, Error, LoginRequest>({
mutationFn: (loginInfo: LoginRequest) => postLogin(loginInfo),
onSuccess: (res) => {
const { memberId, name } = res;
onSuccess: async (res) => {
const authorization = res.headers.get('authorization');

const response = await res.json() as MemberInfo;
const { memberId, name } = response;

if (memberId && name) {
localStorage.setItem('name', String(name));
localStorage.setItem('memberId', String(memberId));
handleMemberInfo(res);
localStorage.setItem('authorization', String(authorization));
handleMemberInfo({ memberId, name });
handleLoginState(true);
successAlert('로그인 성공!');
navigate(END_POINTS.memberTemplates(memberId));
Expand Down
1 change: 1 addition & 0 deletions frontend/src/queries/authentication/useLogoutMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const useLogoutMutation = () => {
onSuccess: () => {
localStorage.removeItem('name');
localStorage.removeItem('memberId');
localStorage.removeItem('authorization');
handleLoginState(false);
successAlert('로그아웃 성공!');
},
Expand Down

0 comments on commit ecf28c6

Please sign in to comment.