A Pen by Lilik Setiyawan on CodePen.
This file contains hidden or 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
import * as axios from "axios"; | |
import { getCookie } from "./utils"; | |
export default class Api { | |
constructor() { | |
this.api_token = null; | |
this.client = null; | |
this.api_url = process.env.REACT_APP_API_ENDPOINT; | |
} |
This file contains hidden or 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
import React from "react"; | |
import "./App.css"; | |
import Api from "./helper/api"; | |
function App() { | |
const api = new Api(); | |
const fetchUser = () => { | |
api | |
.getUserList() |
This file contains hidden or 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
import * as axios from "axios"; | |
import { getCookie, refreshToken } from "./utils"; | |
export default class Api { | |
constructor() { | |
this.api_token = null; | |
this.client = null; | |
this.api_url = process.env.REACT_APP_API_ENDPOINT; | |
} |
This file contains hidden or 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
export async function refreshToken() { | |
const token = getCookie("ACCESS_TOKEN"); | |
const refresh_token = getCookie("REFRESH_TOKEN"); | |
const SERVER_DOMAIN = process.env.REACT_APP_API_ENDPOINT; | |
const current_time = new Date(); | |
if (token !== "") { | |
const decodeToken = jwt(token); | |
if (current_time.getTime() > decodeToken.exp * 1000) { | |
return Axios.post(`${SERVER_DOMAIN}v1/auth/refresh`, { | |
refreshToken: refresh_token, |