Some Code Cleanup
This commit is contained in:
@@ -17,38 +17,16 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {derived, writable} from "svelte/store";
|
||||
import {EventRepo} from "./event";
|
||||
import {FightRepo} from "./fight";
|
||||
import {PermsRepo} from "./perms";
|
||||
import {PageRepo} from "./page";
|
||||
import {DataRepo} from "./data";
|
||||
import {SchematicRepo} from "./schem";
|
||||
import {StatsRepo} from "./stats";
|
||||
import {AuthRepo} from "./auth";
|
||||
import {writable} from "svelte/store";
|
||||
|
||||
import { AES, enc } from "crypto-js";
|
||||
export const fetchWithToken = (token: string, url: string, params: RequestInit = {}) =>
|
||||
fetch(`${import.meta.env.PUBLIC_API_SERVER}${url}`, {...params, headers: {...(token !== "" ? {"Authorization": "Bearer " + (token)}:{}), "Content-Type": "application/json", ...params.headers}})
|
||||
.then(value => {
|
||||
if (value.status === 401) {
|
||||
tokenStore.set("");
|
||||
}
|
||||
return value;
|
||||
});
|
||||
|
||||
export const apiUrl = import.meta.env.PUBLIC_API_SERVER;
|
||||
const secret = import.meta.env.PUBLIC_SECRET;
|
||||
|
||||
function encryptToken(token: string): string {
|
||||
return AES.encrypt(token, secret).toString();
|
||||
}
|
||||
|
||||
function decryptToken(token: string): string {
|
||||
return AES.decrypt(token, secret).toString(enc.Utf8);
|
||||
}
|
||||
|
||||
export const fetchWithToken = (token: string, url: string, params: RequestInit = {}) => fetch(`${apiUrl}${url}`, {...params, headers: {...(token !== "" ? {"Authorization": "Bearer " + (token)}:{}), "Content-Type": "application/json", ...params.headers}});
|
||||
export const tokenStore = writable(decryptToken(localStorage.getItem("sw-session") ?? ""));
|
||||
tokenStore.subscribe((value) => localStorage.setItem("sw-session", encryptToken(value)));
|
||||
|
||||
export const eventRepo = derived(tokenStore, ($token) => new EventRepo($token));
|
||||
export const fightRepo = derived(tokenStore, ($token) => new FightRepo($token));
|
||||
export const permsRepo = derived(tokenStore, ($token) => new PermsRepo($token));
|
||||
export const pageRepo = derived(tokenStore, ($token) => new PageRepo($token));
|
||||
export const dataRepo = derived(tokenStore, ($token) => new DataRepo($token));
|
||||
export const schemRepo = derived(tokenStore, ($token) => new SchematicRepo($token));
|
||||
export const statsRepo = derived(tokenStore, ($token) => new StatsRepo($token));
|
||||
export const authRepo = derived(tokenStore, ($token) => new AuthRepo($token));
|
||||
export const tokenStore = writable((localStorage.getItem("sw-session") ?? ""));
|
||||
tokenStore.subscribe((value) => localStorage.setItem("sw-session", value));
|
||||
|
||||
Reference in New Issue
Block a user