diff --git a/src/App.vue b/src/App.vue index ad1f234..7dd64f3 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,11 +1,8 @@ diff --git a/src/api/index.ts b/src/api/index.ts index 17e082d..45fbe74 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,13 +1,6 @@ -import { cSite, LoginResponse, Site, token } from "./types"; -import { get as idbGet, set as idbSet, del as idbDel } from "idb-keyval"; - -export * from "./types"; - -const enum HTTPMethod { - GET = "GET", - POST = "POST", - PATCH = "PATCH", -} +import { LoginResponse, token } from "./types"; +import { get, set } from "idb-keyval"; +import { unref } from "vue"; const url = "http://192.168.178.5:8080/api"; let token: token; @@ -16,7 +9,7 @@ let ready = false; export async function init() { if (ready) return; - await idbGet("token").then((val: token) => { + await get("token").then((val: token) => { if (val !== undefined) { token = val; } @@ -24,80 +17,36 @@ export async function init() { ready = true; } -export async function logout(): Promise { - token = { jwt: "", expiration: 0 }; - await idbDel("token"); -} - export async function isLoggedIn(): Promise { await init(); - return token?.expiration > Date.now(); + return token !== undefined && token.expiration > Date.now(); } -interface ReqOptions extends RequestInit { - method: HTTPMethod; - auth?: boolean; -} - -async function request( - endpoint: string, - options: ReqOptions, - data?: any -): Promise { - if (options.auth) { - await init(); - options.headers = { - ...options.headers, - Authorization: `Bearer ${token.jwt}`, - }; - } - if ( - options.method === HTTPMethod.POST || - options.method === HTTPMethod.PATCH - ) { - options.headers = { - ...options.headers, +export async function login(user: string, password: string): Promise { + await init(); + let response = await fetch(`${url}/auth`, { + method: "POST", + headers: { + Accept: "application/json", "Content-Type": "application/json", - }; - options.body = JSON.stringify(data); - } - - const response = await fetch(`${url}${endpoint}`, { - ...options, - headers: { ...options.headers, Accept: "application/json" }, + }, + body: JSON.stringify({ username: user, password: password }), }); - if (!response.ok) { - throw new Error(response.statusText); + const login: LoginResponse = await response.json(); + + if (response.ok) { + if (login.success) { + token = { + jwt: login.token, + expiration: Date.parse(login.expiration), + }; + set("token", token).catch((error) => console.error(error)); + return true; + } else { + return Promise.reject(new Error(login.message)); + } + } else { + const error = new Error(login.message); + return Promise.reject(error); } - return response.json(); -} - -export async function login( - username: string, - password: string -): Promise { - const login = await request( - "/auth", - { method: HTTPMethod.POST }, - { username, password } - ); - - if (!login.success) { - throw new Error(login.message); - } - token = { - jwt: login.token, - expiration: Date.parse(login.expiration), - }; - idbSet("token", token).catch((error) => console.error(error)); - - return true; -} - -export async function getSites(): Promise { - return request("/sites", { auth: true, method: HTTPMethod.GET }); -} - -export async function createSite(site: Site) { - return request("/sites", { auth: true, method: HTTPMethod.POST }); } diff --git a/src/api/types.ts b/src/api/types.ts index 13e4541..9fc9390 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -9,13 +9,3 @@ export interface LoginResponse { success: boolean; token: string; } - -export interface Site { - default_prefix: string; - id: number; - name: string; -} - -export interface cSite { - "site-id": number; -} diff --git a/src/components/VNav.vue b/src/components/VNav.vue index b3bccd9..7480f25 100644 --- a/src/components/VNav.vue +++ b/src/components/VNav.vue @@ -88,9 +88,9 @@ class="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0" > {{ isLoggedIn ? "Logout" : "Login" }}Logout @@ -124,9 +124,8 @@ diff --git a/src/router/index.ts b/src/router/index.ts index dd2cb9b..e15d33a 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -2,7 +2,6 @@ import { createWebHistory, createRouter } from "vue-router"; import Home from "../views/Home.vue"; const Settings = () => import("../views/Settings.vue"); const Login = () => import("../views/Login.vue"); -const Sites = () => import("../views/Sites.vue"); import { isLoggedIn } from "../api"; const routes = [ @@ -16,16 +15,10 @@ const routes = [ name: "Settings", component: Settings, }, - { - path: "/sites", - name: "Sites", - component: Sites, - }, { path: "/login", name: "Login", component: Login, - props: (route) => ({ logout: route.query.logout }), }, ]; diff --git a/src/views/Login.vue b/src/views/Login.vue index 99994d6..7ea86c5 100644 --- a/src/views/Login.vue +++ b/src/views/Login.vue @@ -1,12 +1,11 @@ -