2024-06-30 18:55:39 -04:00
|
|
|
import Cookies from 'js-cookie'
|
|
|
|
import { API_URL } from '../Config'
|
2024-12-26 02:13:47 -05:00
|
|
|
import { login, RefreshToken } from './Fetcher'
|
|
|
|
|
|
|
|
import { Preferences } from '@capacitor/preferences'
|
|
|
|
|
|
|
|
class ApiManager {
|
|
|
|
constructor(){
|
|
|
|
this.customServerURL = API_URL
|
|
|
|
this.initialized = false
|
|
|
|
}
|
|
|
|
async init(){
|
|
|
|
if(this.initialized){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const { value: serverURL } = await Preferences.get({ key: 'customServerUrl' });
|
|
|
|
|
|
|
|
this.customServerURL = serverURL || this.apiURL
|
|
|
|
this.initialized = true
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
getApiURL(){
|
|
|
|
return this.customServerURL
|
|
|
|
}
|
|
|
|
updateApiURL(url){
|
|
|
|
this.customServerURL = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const apiManager = new ApiManager();
|
|
|
|
|
|
|
|
|
2024-06-30 18:55:39 -04:00
|
|
|
export function Fetch(url, options) {
|
2024-12-26 02:13:47 -05:00
|
|
|
|
2024-06-30 18:55:39 -04:00
|
|
|
if (!isTokenValid()) {
|
|
|
|
// store current location in cookie
|
2024-12-26 02:13:47 -05:00
|
|
|
Cookies.set('ca_redirect', window.location.pathname);
|
2024-06-30 18:55:39 -04:00
|
|
|
// Assuming you have a function isTokenValid() that checks token validity
|
2024-12-26 02:13:47 -05:00
|
|
|
window.location.href = '/login'; // Redirect to login page
|
2024-06-30 18:55:39 -04:00
|
|
|
// return Promise.reject("Token is not valid");
|
|
|
|
}
|
|
|
|
if (!options) {
|
2024-12-26 02:13:47 -05:00
|
|
|
options = {};
|
2024-06-30 18:55:39 -04:00
|
|
|
}
|
2024-12-26 02:13:47 -05:00
|
|
|
options.headers = { ...options.headers, ...HEADERS() };
|
|
|
|
|
|
|
|
const baseURL = apiManager.getApiURL();
|
2024-06-30 18:55:39 -04:00
|
|
|
|
2024-12-26 02:13:47 -05:00
|
|
|
const fullURL = `${baseURL}${url}`;
|
|
|
|
return fetch(fullURL, options);
|
2024-06-30 18:55:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export const HEADERS = () => {
|
|
|
|
return {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: 'Bearer ' + localStorage.getItem('ca_token'),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isTokenValid = () => {
|
|
|
|
const expiration = localStorage.getItem('ca_expiration')
|
|
|
|
const token = localStorage.getItem('ca_token')
|
|
|
|
|
|
|
|
if (localStorage.getItem('ca_token')) {
|
|
|
|
const now = new Date()
|
|
|
|
const expire = new Date(expiration)
|
|
|
|
if (now < expire) {
|
|
|
|
if (now.getTime() + 24 * 60 * 60 * 1000 > expire.getTime()) {
|
|
|
|
refreshAccessToken()
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
localStorage.removeItem('ca_token')
|
|
|
|
localStorage.removeItem('ca_expiration')
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const refreshAccessToken = () => {
|
2024-12-26 02:13:47 -05:00
|
|
|
RefreshToken()
|
|
|
|
.then(res => {
|
2024-06-30 18:55:39 -04:00
|
|
|
if (res.status === 200) {
|
|
|
|
res.json().then(data => {
|
|
|
|
localStorage.setItem('ca_token', data.token)
|
|
|
|
localStorage.setItem('ca_expiration', data.expire)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return res.json().then(error => {
|
|
|
|
console.log(error)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|