Verify token on startup

This commit is contained in:
Melon 2023-12-14 11:46:04 +00:00
parent a10ed84d6a
commit 76b24f3f68
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
6 changed files with 20 additions and 13 deletions

View File

@ -1,5 +1,5 @@
<script lang="ts">
import type {SvelteComponent} from "svelte";
import {onMount, type SvelteComponent} from "svelte";
import GeneralView from "./views/GeneralView.svelte";
import RoutesView from "./views/RoutesView.svelte";
import RedirectsView from "./views/RedirectsView.svelte";
@ -8,6 +8,7 @@
import {loginStore, parseJwt, type LoginStore} from "./stores/login";
import {openLoginPopup} from "./utils/login-popup";
import {domainOption} from "./stores/domain-option";
import {apiVerify} from "./utils/api-request";
let sidebarOptions: Array<{name: string; view: typeof SvelteComponent<{}>}> = [
{name: "General", view: GeneralView},
@ -31,6 +32,10 @@
if (!jwt) return [];
return jwt.per.filter((x: string) => x.startsWith("domain:owns=")).map((x: string) => x.slice("domain:owns=".length));
}
onMount(() => {
apiVerify().catch(() => {});
});
</script>
<header>
@ -103,7 +108,9 @@
height: 70px;
padding: 0 32px;
background-color: #2c2c2c;
box-shadow: 0 4px 8px #0003, 0 6px 20px #00000030;
box-shadow:
0 4px 8px #0003,
0 6px 20px #00000030;
gap: 16px;
z-index: 1;
position: relative;
@ -198,7 +205,9 @@
footer {
padding: 8px;
background-color: #2c2c2c;
box-shadow: 0 -4px 8px #0003, 0 -6px 20px #00000030;
box-shadow:
0 -4px 8px #0003,
0 -6px 20px #00000030;
display: flex;
flex-direction: row;
justify-content: space-between;

View File

@ -1,6 +1,6 @@
<script lang="ts">
import type {Writable} from "svelte/store";
import {type CSPair, noCPair, noSPair, noPair, yesCPair} from "../types/cspair";
import {type CSPair, noCPair, noSPair, yesCPair} from "../types/cspair";
import {redirectKeys, redirectEqual, type Redirect} from "../types/target";
import Flags from "./Flags.svelte";
import RedirectCode from "./RedirectCode.svelte";

View File

@ -1,6 +1,7 @@
import {get} from "svelte/store";
import {getBearer, loginStore} from "../stores/login";
const TOKEN_VERIFY_API = import.meta.env.VITE_SSO_ORIGIN + "/refresh";
const TOKEN_REFRESH_API = import.meta.env.VITE_SSO_ORIGIN + "/refresh";
export async function apiRequest(url: string, init?: RequestInit): Promise<Response> {
@ -33,3 +34,7 @@ export async function apiRequest(url: string, init?: RequestInit): Promise<Respo
init.headers = {...init.headers, Authorization: getBearer()};
return await fetch(url, init);
}
export async function apiVerify() {
return await apiRequest(TOKEN_VERIFY_API);
}

View File

@ -1,9 +1,7 @@
<script lang="ts">
import {domainOption} from "../stores/domain-option";
import {getBearer} from "../stores/login";
import {type Cert, certsTable} from "../stores/certs";
import {apiRequest} from "../utils/api-request";
import {onMount} from "svelte";
const apiOrchid = import.meta.env.VITE_API_ORCHID;

View File

@ -1,6 +1,5 @@
<script lang="ts">
import {domainOption} from "../stores/domain-option";
import {getBearer} from "../stores/login";
import {type Site, sitesTable} from "../stores/sites";
import {apiRequest} from "../utils/api-request";

View File

@ -6,10 +6,7 @@
<script lang="ts">
import {apiRequest} from "../utils/api-request";
import {writable, type Writable} from "svelte/store";
import {getBearer} from "../stores/login";
import type {CSPair} from "../types/cspair";
import {domainOption} from "../stores/domain-option";
import {type CountStats, tableCountStats} from "../stores/target";
@ -77,9 +74,8 @@
})
.sort((a, _) => (a.type === "del" ? -1 : a.type === "ins" ? 1 : 0))
.map(x => {
x.v.p = fetch(apiUrl, {
x.v.p = apiRequest(apiUrl, {
method: x.type == "del" ? "DELETE" : "POST",
headers: {Authorization: getBearer()},
body: JSON.stringify(x.type == "del" ? {src: (x.v.server as T).src} : x.v.client),
}).then(x => {
if (x.status !== 200) throw new Error("Unexpected status code: " + x.status);