-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
로그인 기능 개발
- Loading branch information
Showing
15 changed files
with
299 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { getToken } from '@_utils/tokenManager'; | ||
|
||
type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; | ||
|
||
const DEFAULT_HEADERS = { | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
const BASE_URL = `${process.env.BASE_URL}/v1`; | ||
|
||
function addBaseUrl(endpoint: string) { | ||
if (endpoint[0] !== '/') endpoint = '/' + endpoint; | ||
return BASE_URL + endpoint; | ||
} | ||
|
||
function getHeaders(token?: string) { | ||
const headers = new Headers(DEFAULT_HEADERS); | ||
if (token) { | ||
headers.append('Authorization', `Bearer ${token}`); | ||
} | ||
return headers; | ||
} | ||
|
||
async function request( | ||
method: Method, | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
isRequiredAuth: boolean = false, | ||
) { | ||
const url = addBaseUrl(endpoint); | ||
const token = isRequiredAuth ? getToken() : undefined; | ||
|
||
const options: RequestInit = { | ||
method, | ||
headers: getHeaders(token), | ||
...config, | ||
}; | ||
|
||
if (method !== 'GET') { | ||
options.body = JSON.stringify(data); | ||
} | ||
|
||
return await fetch(url, options); | ||
} | ||
|
||
async function get( | ||
endpoint: string, | ||
config: RequestInit = {}, | ||
isRequiredAuth: boolean = false, | ||
) { | ||
return request('GET', endpoint, {}, config, isRequiredAuth); | ||
} | ||
|
||
async function post( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
isRequiredAuth: boolean = false, | ||
) { | ||
return request('POST', endpoint, data, config, isRequiredAuth); | ||
} | ||
|
||
async function put( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
isRequiredAuth: boolean = false, | ||
) { | ||
return request('PUT', endpoint, data, config, isRequiredAuth); | ||
} | ||
|
||
async function patch( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
isRequiredAuth: boolean = false, | ||
) { | ||
return request('PATCH', endpoint, data, config, isRequiredAuth); | ||
} | ||
|
||
/** | ||
* delete는 예약어로 사용할 수 없는 함수 이름이라 부득이 `deleteMethod`로 이름을 지었습니다. | ||
*/ | ||
async function deleteMethod( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
isRequiredAuth: boolean = false, | ||
) { | ||
return request('DELETE', endpoint, data, config, isRequiredAuth); | ||
} | ||
|
||
const ApiClient = { | ||
async getWithoutAuth(endpoint: string, config: RequestInit = {}) { | ||
return get(endpoint, config, false); | ||
}, | ||
async getWithAuth(endpoint: string, config: RequestInit = {}) { | ||
return get(endpoint, config, true); | ||
}, | ||
|
||
async postWithAuth( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
) { | ||
return post(endpoint, data, config, true); | ||
}, | ||
async postWithoutAuth( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
) { | ||
return post(endpoint, data, config, false); | ||
}, | ||
|
||
async putWithAuth( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
) { | ||
return put(endpoint, data, config, true); | ||
}, | ||
async putWithoutAuth( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
) { | ||
return put(endpoint, data, config, false); | ||
}, | ||
|
||
async patchWithAuth( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
) { | ||
return patch(endpoint, data, config, true); | ||
}, | ||
async patchWithoutAuth( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
) { | ||
return patch(endpoint, data, config, false); | ||
}, | ||
|
||
async deleteWithAuth( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
) { | ||
return deleteMethod(endpoint, data, config, true); | ||
}, | ||
async deleteWithoutAuth( | ||
endpoint: string, | ||
data: object = {}, | ||
config: RequestInit = {}, | ||
) { | ||
return deleteMethod(endpoint, data, config, false); | ||
}, | ||
}; | ||
|
||
export default ApiClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import ApiClient from './apiClient'; | ||
import { checkStatus } from './apiconfig'; | ||
|
||
export const login = async (loginInputInfo: { nickname: string }) => { | ||
const response = await ApiClient.postWithoutAuth( | ||
'auth/login', | ||
loginInputInfo, | ||
); | ||
checkStatus(response); | ||
return response.json(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,21 @@ | ||
import ENDPOINTS from '@_apis/endPoints'; | ||
import { GetMoim, GetMoims } from '@_apis/responseTypes'; | ||
// ./src/apis/gets.ts | ||
import { MoimInfo } from '@_types/index'; | ||
import { checkStatus, defaultOptions } from './apiconfig'; | ||
import ApiClient from './apiClient'; | ||
import { GetMoim, GetMoims } from './responseTypes'; | ||
import { checkStatus } from './apiconfig'; | ||
|
||
const defaultGetOptions = { | ||
method: 'GET', | ||
...defaultOptions, | ||
}; | ||
export const getMoims = async (): Promise<MoimInfo[]> => { | ||
const url = ENDPOINTS.moims; | ||
|
||
const response = await fetch(url, defaultGetOptions); | ||
|
||
const response = await ApiClient.getWithAuth('moim'); | ||
checkStatus(response); | ||
const json = (await response.json()) as GetMoims; | ||
|
||
const json: GetMoims = await response.json(); | ||
return json.data.moims; | ||
}; | ||
|
||
export const getMoim = async (moimId: number): Promise<MoimInfo> => { | ||
const url = `${ENDPOINTS.moim}/${moimId}`; | ||
|
||
const response = await fetch(url, defaultGetOptions); | ||
|
||
const response = await ApiClient.getWithAuth(`moim/${moimId}`); | ||
checkStatus(response); | ||
|
||
const json = (await response.json()) as GetMoim; | ||
const json: GetMoim = await response.json(); | ||
return json.data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,21 @@ | ||
import { checkStatus, defaultOptions } from './apiconfig'; | ||
|
||
import ENDPOINTS from '@_apis/endPoints'; | ||
import { MoimInputInfo } from '@_types/index'; | ||
import { PostMoim } from '@_apis/responseTypes'; | ||
import ApiClient from './apiClient'; | ||
import { PostMoim } from './responseTypes'; | ||
import { checkStatus } from './apiconfig'; | ||
|
||
const defaultPostOptions = { | ||
method: 'POST', | ||
...defaultOptions, | ||
}; | ||
export const postMoim = async (moim: MoimInputInfo): Promise<number> => { | ||
const url = ENDPOINTS.moim; | ||
|
||
const options = { | ||
...defaultPostOptions, | ||
body: JSON.stringify(moim), | ||
}; | ||
|
||
const response = await fetch(url, options); | ||
const response = await ApiClient.postWithAuth('moim', moim); | ||
|
||
checkStatus(response); | ||
|
||
const json = (await response.json()) as PostMoim; | ||
const json: PostMoim = await response.json(); | ||
return json.data; | ||
}; | ||
|
||
export const postJoinMoim = async (moimId: number, nickname: string) => { | ||
const url = `${ENDPOINTS.moims}/join`; | ||
const options = { | ||
...defaultPostOptions, | ||
body: JSON.stringify({ moimId, nickname }), | ||
}; | ||
|
||
const response = await fetch(url, options); | ||
|
||
const response = await ApiClient.postWithAuth('moim/join', { | ||
moimId, | ||
nickname, | ||
}); | ||
await checkStatus(response); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import ROUTES from '@_constants/routes'; | ||
import { checkAuthentication } from '@_utils/checkAuthentication'; | ||
import { PropsWithChildren } from 'react'; | ||
import { Navigate, useLocation } from 'react-router-dom'; | ||
|
||
export default function ProtectedRoute(props: PropsWithChildren) { | ||
const { children } = props; | ||
|
||
const isAuthenticated = checkAuthentication(); | ||
const location = useLocation(); | ||
|
||
return isAuthenticated ? ( | ||
children | ||
) : ( | ||
<Navigate to={ROUTES.login} state={{ from: location }} replace /> | ||
); | ||
} |
Oops, something went wrong.