Skip to content

Commit

Permalink
test: test
Browse files Browse the repository at this point in the history
  • Loading branch information
foodeco committed Jun 9, 2023
1 parent cba6640 commit 1ac8646
Show file tree
Hide file tree
Showing 11 changed files with 1,193 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/Apis/Token/token.ts
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()}`;
}
58 changes: 58 additions & 0 deletions src/Apis/adminAuth.ts
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;
Loading

0 comments on commit 1ac8646

Please sign in to comment.