-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,193 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import axios from 'axios'; | ||
//import qs from 'qs'; | ||
|
||
const { VITE_ACCESS_TOKEN, VITE_REDIRECT_URI } = import.meta.env; | ||
|
||
const ajax = axios.create({ | ||
baseURL: '/cafe24/oauth/token', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
Authorization: `Basic ${VITE_ACCESS_TOKEN}`, | ||
}, | ||
}); | ||
|
||
interface Token { | ||
access_token: string; | ||
expires_at: string; | ||
refresh_token: string; | ||
refresh_token_expires_at: string; | ||
client_id: string; | ||
mall_id: string; | ||
user_id: string; | ||
scopes: string[]; | ||
issued_at: string; | ||
shop_no: string; | ||
} | ||
|
||
let token: Token; | ||
|
||
export async function getToken() { | ||
console.log('get'); | ||
const params = new URLSearchParams(location.search); | ||
try { | ||
const { data } = await ajax.post('', { | ||
grant_type: 'authorization_code', | ||
redirect_uri: VITE_REDIRECT_URI, | ||
code: params.get('code'), | ||
}); | ||
token = data; | ||
console.log('request', data); | ||
setCookie( | ||
token.access_token, | ||
token.expires_at, | ||
token.refresh_token | ||
//token.refresh_token_expires_at | ||
); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
|
||
export async function refreshToken() { | ||
try { | ||
console.log('refresh'); | ||
const { data } = await ajax.post('', { | ||
grant_type: 'refresh_token', | ||
refresh_token: localStorage.getItem('refreshToken'), | ||
}); | ||
token = data; | ||
setCookie( | ||
token.access_token, | ||
token.expires_at, | ||
token.refresh_token | ||
//token.refresh_token_expires_at | ||
); | ||
console.log('refresh', data); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
|
||
function setCookie( | ||
accessToken: string, | ||
accessExpires: string, | ||
refreshToken: string | ||
//refreshExpires: string | ||
) { | ||
document.cookie = `accessToken=${accessToken}; path=/; expires=${new Date( | ||
accessExpires | ||
).toUTCString()}`; | ||
localStorage.setItem('refreshToken', refreshToken); | ||
// document.cookie = `refreshToken=${refreshToken}; path=/; expires=${new Date( | ||
// refreshExpires | ||
// ).toUTCString()}`; | ||
} |
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,58 @@ | ||
import axios from 'axios'; | ||
import { getToken, refreshToken } from './Token/token'; | ||
|
||
const { VITE_CAFE24_URL } = import.meta.env; | ||
const key = new RegExp(`accessToken=([^;]*)`); | ||
|
||
const ajax = axios.create({ | ||
baseURL: VITE_CAFE24_URL, | ||
params: { | ||
since_product_no: 20, | ||
}, | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
Authorization: `Basic ${key.test(document.cookie) ? RegExp.$1 : ''}`, | ||
}, | ||
}); | ||
|
||
ajax.interceptors.request.use( | ||
async (config) => { | ||
const key = new RegExp(`accessToken=([^;]*)`); | ||
console.log('config', config); | ||
config.headers['Authorization'] = `Bearer ${ | ||
key.test(document.cookie) ? RegExp.$1 : '' | ||
}`; | ||
|
||
return config; | ||
}, | ||
(error) => { | ||
console.log(error); | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
ajax.interceptors.response.use( | ||
(response) => { | ||
if (response.status === 404) { | ||
console.log('show 404 error page'); | ||
} | ||
return response; | ||
}, | ||
async (error) => { | ||
if (error.response?.status === 401) { | ||
const key = new RegExp(`accessToken=([^;]*)`); | ||
|
||
console.log(key.test(document.cookie)); | ||
if (!key.test(document.cookie) && !localStorage.getItem('refreshToken')) { | ||
await getToken(); | ||
} else { | ||
await refreshToken(); | ||
} | ||
error.config.headers = { | ||
Authorization: `Bearer ${key.test(document.cookie) ? RegExp.$1 : ''}`, | ||
}; | ||
} | ||
} | ||
); | ||
|
||
export default ajax; |
Oops, something went wrong.