import {decodeToken, isExpired} from "react-jwt";
import {requests} from "../../requests";
import axios from "axios";

class TokenUtil {

    checkToken(token) {
        return true;
        if (token == null || isExpired(token)) {
            return false;
        }

        this.refreshToken();
        return true;
    }

    getToken() {
        return localStorage.getItem("token");
    }

    refreshToken() {
        const decoded = decodeToken(this.getToken());
        const currentTime = Date.now() / 1000;
        if (decoded.exp - currentTime > 43200) {
            return
        }

        axios.post(requests.auth.refresh, {}, {headers: {'Authorization': this.getToken()}})
            .then((r) => {
                localStorage.setItem("token", r.data.token)
            })
    }
}

export const tokenUtil = new TokenUtil();