diff --git a/src/apis/sendApi.ts b/src/apis/sendApi.ts index f23c7f7f..463e2f04 100644 --- a/src/apis/sendApi.ts +++ b/src/apis/sendApi.ts @@ -1,16 +1,22 @@ import axios from 'axios'; -import { AUTH_KEY } from '../constants/authkey'; -import httpService from '../constants/libs/httpService'; +import { AUTH_KEY, } from '../constants/authkey'; +import { SESSION_KEY } from '../constants/session'; +import sessionStorageService from '../libs/sessionStorageService'; +import authorization from '../libs/httpService'; let ck = `eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0YW1kNTk3MUBuYXZlci5jb20iLCJyb2xlcyI6Ik1BTkFHRVIiLCJpYXQiOjE2OTM4OTE1MzUsImV4cCI6MTY5Mzg5MzMzNX0.xvxGA-2tW3en2DKv-Q-ZaI38r4f2lNCO1M8kNSZwXhk` export const sendApi = { get: (url:string) => { - return axios.get( - `https://api.dessert-gallery.site${url}`, - // httpService.authorization(localStorageService.get(SESSION_ID)) - httpService.authorization(ck) - ); + console.log('sessionStorageService.get(SESSION_KEY', sessionStorageService.get(SESSION_KEY)) + if(sessionStorageService.get(SESSION_KEY) !== null){ + return axios.get( + `${AUTH_KEY.apiUrl}${url}`, + authorization(sessionStorageService.get(SESSION_KEY)) + ); + }else{ + return axios.get(`${AUTH_KEY.apiUrl}${url}`); + } }, diff --git a/src/constants/authkey.ts b/src/constants/authkey.ts index 52a0416d..3fa2190a 100644 --- a/src/constants/authkey.ts +++ b/src/constants/authkey.ts @@ -1,7 +1,7 @@ const { - REACT_APP_API_URI, + NEXT_PUBLIC_API_URL, } = process.env; export const AUTH_KEY = { - apiUrl: REACT_APP_API_URI, -}; \ No newline at end of file + apiUrl: NEXT_PUBLIC_API_URL, +}; diff --git a/src/constants/libs/httpService.ts b/src/constants/libs/httpService.ts deleted file mode 100644 index 28ed6796..00000000 --- a/src/constants/libs/httpService.ts +++ /dev/null @@ -1,11 +0,0 @@ -const config = { - authorization: (token: string) => { - return { - headers: { - Authorization: `Bearer ${token}`, - }, - }; - }, -}; - -export default config; \ No newline at end of file diff --git a/src/constants/session.ts b/src/constants/session.ts new file mode 100644 index 00000000..13069049 --- /dev/null +++ b/src/constants/session.ts @@ -0,0 +1 @@ +export const SESSION_KEY = 'JWTSessionStorage'; \ No newline at end of file diff --git a/src/libs/httpService.ts b/src/libs/httpService.ts new file mode 100644 index 00000000..5705729a --- /dev/null +++ b/src/libs/httpService.ts @@ -0,0 +1,10 @@ + +const authorization = (token:string|null) => { + return { + headers: { + Authorization: `${token}`, + }, + }; +}; + +export default authorization; \ No newline at end of file diff --git a/src/libs/sessionStorageService.ts b/src/libs/sessionStorageService.ts new file mode 100644 index 00000000..1a0555f7 --- /dev/null +++ b/src/libs/sessionStorageService.ts @@ -0,0 +1,13 @@ +const sessionStorageService = { + set: (key:string, value:string='') => { + return typeof window !== 'undefined' ? sessionStorage.setItem(key, value) : null; + }, + get: (key:string = '')=> { + return typeof window !== 'undefined' ? sessionStorage.getItem(key) : null; + }, + delete: (key:string = '') => { + return typeof window !== 'undefined' ? sessionStorage.removeItem(key) : null; + }, +}; + +export default sessionStorageService; \ No newline at end of file