initial commit
This commit is contained in:
78
utils/http.js
Normal file
78
utils/http.js
Normal file
@@ -0,0 +1,78 @@
|
||||
import {appConfig} from "../config/app";
|
||||
import {TokenUtil} from "./token";
|
||||
import axios from "axios";
|
||||
|
||||
const instance = axios.create({
|
||||
baseURL: appConfig.apiUrl,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
instance.interceptors.request.use(
|
||||
(config) => {
|
||||
if (TokenUtil.accessToken) {
|
||||
config.headers["Authorization"] = 'Bearer ' + TokenUtil.accessToken; // for Node.js Express back-end
|
||||
}
|
||||
config.headers["ngrok-skip-browser-warning"] = true
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
instance.interceptors.response.use(
|
||||
(res) => {
|
||||
return res;
|
||||
},
|
||||
async (err) => {
|
||||
const originalConfig = err.config;
|
||||
|
||||
if (originalConfig.url !== "/auth/login" && err.response) {
|
||||
// Access Token was expired
|
||||
if (err.response.status === 401 && !originalConfig._retry) {
|
||||
originalConfig._retry = true;
|
||||
|
||||
try {
|
||||
if (TokenUtil.refreshToken) {
|
||||
await authenticationRepository.api.refreshToken()
|
||||
}
|
||||
return instance(originalConfig);
|
||||
} catch (_error) {
|
||||
return Promise.reject(_error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject(err);
|
||||
}
|
||||
);
|
||||
|
||||
export const http = {
|
||||
fetcher: async (url) => {
|
||||
const resp = await instance.get(appConfig.apiUrl + url);
|
||||
|
||||
return resp.data;
|
||||
},
|
||||
get: async (url, opts = {}) => {
|
||||
const resp = await instance.post(appConfig.apiUrl + url);
|
||||
|
||||
return resp.data;
|
||||
},
|
||||
post: async (url, data, opts) => {
|
||||
const resp = await instance.post(appConfig.apiUrl + url, data);
|
||||
|
||||
return resp.data;
|
||||
},
|
||||
put: async (url, data, opts) => {
|
||||
const resp = await instance.put(appConfig.apiUrl + url, data);
|
||||
|
||||
return resp.data;
|
||||
},
|
||||
del: async (url, opts) => {
|
||||
const resp = await instance.delete(appConfig.apiUrl + url);
|
||||
|
||||
return resp.data;
|
||||
},
|
||||
};
|
||||
79
utils/token.js
Normal file
79
utils/token.js
Normal file
@@ -0,0 +1,79 @@
|
||||
const atob = require('atob');
|
||||
|
||||
export class TokenUtil {
|
||||
static accessToken = null;
|
||||
static refreshToken = null;
|
||||
static username = null;
|
||||
static password = null;
|
||||
static isBought = null;
|
||||
|
||||
static loadToken() {
|
||||
if (typeof window === "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
const accessToken = localStorage.getItem('access_token');
|
||||
const username = localStorage.getItem('username');
|
||||
const password = localStorage.getItem('password');
|
||||
const isBought = localStorage.getItem('isBought');
|
||||
|
||||
if (accessToken) {
|
||||
TokenUtil.setAccessToken(accessToken);
|
||||
}
|
||||
|
||||
if (username) {
|
||||
TokenUtil.setUsername(username);
|
||||
}
|
||||
|
||||
if (password) {
|
||||
TokenUtil.setPassword(password);
|
||||
}
|
||||
|
||||
if (isBought) {
|
||||
TokenUtil.setIsBought(isBought);
|
||||
}
|
||||
}
|
||||
|
||||
static persistToken() {
|
||||
if (TokenUtil.accessToken != null) {
|
||||
localStorage.setItem('access_token', TokenUtil.accessToken);
|
||||
} else {
|
||||
localStorage.removeItem('access_token');
|
||||
}
|
||||
}
|
||||
|
||||
static persistIsBought() {
|
||||
if (TokenUtil.isBought != null) {
|
||||
localStorage.setItem('is_bought', TokenUtil.isBought);
|
||||
} else {
|
||||
localStorage.removeItem('is_bought');
|
||||
}
|
||||
}
|
||||
|
||||
static setAccessToken(accessToken) {
|
||||
TokenUtil.accessToken = accessToken;
|
||||
}
|
||||
|
||||
static setUsername(username) {
|
||||
TokenUtil.username = username;
|
||||
}
|
||||
|
||||
static setPassword(password) {
|
||||
TokenUtil.password = password;
|
||||
}
|
||||
|
||||
static setIsBought(isBought) {
|
||||
TokenUtil.isBought = isBought;
|
||||
}
|
||||
|
||||
static clearAccessToken() {
|
||||
TokenUtil.accessToken = null;
|
||||
}
|
||||
|
||||
static decodedToken() {
|
||||
if (TokenUtil.accessToken) {
|
||||
return JSON.parse(atob(TokenUtil.accessToken.split('.')[1]));
|
||||
}
|
||||
return {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user