Compare commits
2 commits
89510a0a67
...
104d785d51
Author | SHA1 | Date | |
---|---|---|---|
|
104d785d51 | ||
|
09369d83c1 |
7 changed files with 205 additions and 45 deletions
|
@ -1,8 +1,11 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="dark:bg-blue-900 min-h-screen">
|
<div class="dark:bg-blue-900 min-h-screen">
|
||||||
<v-nav />
|
<v-nav />
|
||||||
<main class="container mx-auto px-2 max-w-7xl sm:px-6">
|
<main class="container mx-auto px-2 sm:px-6 pt-8">
|
||||||
<router-view></router-view>
|
<suspense>
|
||||||
|
<template #default><router-view></router-view> </template>
|
||||||
|
<template #fallback>Loading</template>
|
||||||
|
</suspense>
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
105
src/api/index.ts
105
src/api/index.ts
|
@ -1,6 +1,13 @@
|
||||||
import { LoginResponse, token } from "./types";
|
import { cSite, LoginResponse, Site, token } from "./types";
|
||||||
import { get, set } from "idb-keyval";
|
import { get as idbGet, set as idbSet, del as idbDel } from "idb-keyval";
|
||||||
import { unref } from "vue";
|
|
||||||
|
export * from "./types";
|
||||||
|
|
||||||
|
const enum HTTPMethod {
|
||||||
|
GET = "GET",
|
||||||
|
POST = "POST",
|
||||||
|
PATCH = "PATCH",
|
||||||
|
}
|
||||||
|
|
||||||
const url = "http://192.168.178.5:8080/api";
|
const url = "http://192.168.178.5:8080/api";
|
||||||
let token: token;
|
let token: token;
|
||||||
|
@ -9,7 +16,7 @@ let ready = false;
|
||||||
export async function init() {
|
export async function init() {
|
||||||
if (ready) return;
|
if (ready) return;
|
||||||
|
|
||||||
await get("token").then((val: token) => {
|
await idbGet("token").then((val: token) => {
|
||||||
if (val !== undefined) {
|
if (val !== undefined) {
|
||||||
token = val;
|
token = val;
|
||||||
}
|
}
|
||||||
|
@ -17,36 +24,80 @@ export async function init() {
|
||||||
ready = true;
|
ready = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function isLoggedIn(): Promise<boolean> {
|
export async function logout(): Promise<void> {
|
||||||
await init();
|
token = { jwt: "", expiration: 0 };
|
||||||
return token !== undefined && token.expiration > Date.now();
|
await idbDel("token");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function login(user: string, password: string): Promise<boolean> {
|
export async function isLoggedIn(): Promise<boolean> {
|
||||||
await init();
|
await init();
|
||||||
let response = await fetch(`${url}/auth`, {
|
return token?.expiration > Date.now();
|
||||||
method: "POST",
|
}
|
||||||
headers: {
|
|
||||||
Accept: "application/json",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ username: user, password: password }),
|
|
||||||
});
|
|
||||||
const login: LoginResponse = await response.json();
|
|
||||||
|
|
||||||
if (response.ok) {
|
interface ReqOptions extends RequestInit {
|
||||||
if (login.success) {
|
method: HTTPMethod;
|
||||||
|
auth?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function request<T = unknown>(
|
||||||
|
endpoint: string,
|
||||||
|
options: ReqOptions,
|
||||||
|
data?: any
|
||||||
|
): Promise<T> {
|
||||||
|
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,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
};
|
||||||
|
options.body = JSON.stringify(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`${url}${endpoint}`, {
|
||||||
|
...options,
|
||||||
|
headers: { ...options.headers, Accept: "application/json" },
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(response.statusText);
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function login(
|
||||||
|
username: string,
|
||||||
|
password: string
|
||||||
|
): Promise<boolean> {
|
||||||
|
const login = await request<LoginResponse>(
|
||||||
|
"/auth",
|
||||||
|
{ method: HTTPMethod.POST },
|
||||||
|
{ username, password }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!login.success) {
|
||||||
|
throw new Error(login.message);
|
||||||
|
}
|
||||||
token = {
|
token = {
|
||||||
jwt: login.token,
|
jwt: login.token,
|
||||||
expiration: Date.parse(login.expiration),
|
expiration: Date.parse(login.expiration),
|
||||||
};
|
};
|
||||||
set("token", token).catch((error) => console.error(error));
|
idbSet("token", token).catch((error) => console.error(error));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
}
|
||||||
return Promise.reject(new Error(login.message));
|
|
||||||
}
|
export async function getSites(): Promise<Site[]> {
|
||||||
} else {
|
return request<Site[]>("/sites", { auth: true, method: HTTPMethod.GET });
|
||||||
const error = new Error(login.message);
|
}
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
export async function createSite(site: Site) {
|
||||||
|
return request<cSite>("/sites", { auth: true, method: HTTPMethod.POST });
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,3 +9,13 @@ export interface LoginResponse {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
token: string;
|
token: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Site {
|
||||||
|
default_prefix: string;
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface cSite {
|
||||||
|
"site-id": number;
|
||||||
|
}
|
||||||
|
|
|
@ -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"
|
class="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0"
|
||||||
>
|
>
|
||||||
<router-link
|
<router-link
|
||||||
:to="{ name: 'Login' }"
|
:to="{ name: 'Login', query: { logout: true } }"
|
||||||
class="text-white bg-blue-600 hover:bg-blue-800 px-3 py-2 rounded-md text-sm font-medium"
|
class="text-white bg-blue-600 hover:bg-blue-800 px-3 py-2 rounded-md text-sm font-medium"
|
||||||
>Logout</router-link
|
>{{ isLoggedIn ? "Logout" : "Login" }}</router-link
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -124,8 +124,9 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
//stolen from https://git.max-site.de/Eulinchen/eulinchen-frontend/src/branch/master/src/components/VNav.vue
|
//stolen from https://git.max-site.de/Eulinchen/eulinchen-frontend/src/branch/master/src/components/VNav.vue
|
||||||
|
|
||||||
import { ref, reactive, computed } from "vue";
|
import { ref, reactive, computed, watch } from "vue";
|
||||||
import { useRoute } from "vue-router";
|
import { useRoute } from "vue-router";
|
||||||
|
import { isLoggedIn as isLoggedinFromAPI } from "../api";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "VNav",
|
name: "VNav",
|
||||||
|
@ -137,16 +138,25 @@ export default {
|
||||||
name: "Dashboard",
|
name: "Dashboard",
|
||||||
to: "Home",
|
to: "Home",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Sites",
|
||||||
|
to: "Sites",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "Settings",
|
name: "Settings",
|
||||||
to: "Settings",
|
to: "Settings",
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
console.log(route);
|
|
||||||
const activeRoute = computed(() => route.name);
|
const activeRoute = computed(() => route.name);
|
||||||
|
|
||||||
return { title, open, items, activeRoute };
|
const isLoggedIn = ref(false);
|
||||||
|
const getLoginStatus = async () => {
|
||||||
|
isLoggedIn.value = await isLoggedinFromAPI();
|
||||||
|
};
|
||||||
|
watch(activeRoute, getLoginStatus);
|
||||||
|
|
||||||
|
return { title, open, items, activeRoute, isLoggedIn };
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { createWebHistory, createRouter } from "vue-router";
|
||||||
import Home from "../views/Home.vue";
|
import Home from "../views/Home.vue";
|
||||||
const Settings = () => import("../views/Settings.vue");
|
const Settings = () => import("../views/Settings.vue");
|
||||||
const Login = () => import("../views/Login.vue");
|
const Login = () => import("../views/Login.vue");
|
||||||
|
const Sites = () => import("../views/Sites.vue");
|
||||||
import { isLoggedIn } from "../api";
|
import { isLoggedIn } from "../api";
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
|
@ -15,10 +16,16 @@ const routes = [
|
||||||
name: "Settings",
|
name: "Settings",
|
||||||
component: Settings,
|
component: Settings,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: "/sites",
|
||||||
|
name: "Sites",
|
||||||
|
component: Sites,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
path: "/login",
|
path: "/login",
|
||||||
name: "Login",
|
name: "Login",
|
||||||
component: Login,
|
component: Login,
|
||||||
|
props: (route) => ({ logout: route.query.logout }),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
<template xmlns="http://www.w3.org/1999/html">
|
<template>
|
||||||
<div
|
<div
|
||||||
class="container mx-auto h-full flex justify-center items-center dark:text-white"
|
class="container mx-auto h-full flex justify-center items-center dark:text-white"
|
||||||
>
|
>
|
||||||
<div class="sm:w-3/4 md:w-1/3 w-full">
|
<div class="sm:w-3/4 md:w-1/3 w-full">
|
||||||
<h1 class="font-hairline mb-6 text-center">Login</h1>
|
<h1 class="font-hairline mb-6 text-center">Login</h1>
|
||||||
<div
|
<form
|
||||||
class="border-yellow-300 p-8 border-t-8 bg-white mb-6 rounded-lg shadow-lg"
|
class="border-yellow-300 p-8 border-t-8 bg-white mb-6 rounded-lg shadow-lg"
|
||||||
|
@submit.prevent="login"
|
||||||
>
|
>
|
||||||
<div class="mb-4">
|
<div class="mb-4">
|
||||||
<label class="font-bold text-gray-800 block mb-2"
|
<label class="font-bold text-gray-800 block mb-2"
|
||||||
|
@ -33,29 +34,37 @@
|
||||||
<div class="flex items-center justify-between">
|
<div class="flex items-center justify-between">
|
||||||
<button
|
<button
|
||||||
class="bg-green-600 hover:bg-green-400 text-white font-bold py-2 px-4 rounded"
|
class="bg-green-600 hover:bg-green-400 text-white font-bold py-2 px-4 rounded"
|
||||||
@click="login"
|
type="submit"
|
||||||
>
|
>
|
||||||
Login
|
Login
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script lang="ts">
|
||||||
import { ref } from "vue";
|
import { ref, toRefs } from "vue";
|
||||||
import { login as apiLogin } from "../api";
|
import { login as apiLogin, logout as apiLogout } from "../api";
|
||||||
import router from "../router";
|
import router from "../router";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "Login",
|
name: "Login",
|
||||||
setup() {
|
props: {
|
||||||
|
logout: {
|
||||||
|
type: String,
|
||||||
|
required: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
async setup(props) {
|
||||||
|
const { logout } = toRefs(props);
|
||||||
const user = ref("");
|
const user = ref("");
|
||||||
const password = ref("");
|
const password = ref("");
|
||||||
async function login() {
|
async function login() {
|
||||||
try {
|
try {
|
||||||
let loginResp = await apiLogin(user.value, password.value);
|
let loginResp = await apiLogin(user.value, password.value);
|
||||||
|
|
||||||
if (loginResp) {
|
if (loginResp) {
|
||||||
router.push({ name: "Home" });
|
router.push({ name: "Home" });
|
||||||
}
|
}
|
||||||
|
@ -64,6 +73,11 @@ export default {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (logout.value !== undefined) {
|
||||||
|
console.log("LOGOUT");
|
||||||
|
await apiLogout();
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
user,
|
user,
|
||||||
password,
|
password,
|
||||||
|
|
65
src/views/Sites.vue
Normal file
65
src/views/Sites.vue
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
<template>
|
||||||
|
<div
|
||||||
|
class="dark:text-white container mx-auto h-full flex justify-center items-center"
|
||||||
|
>
|
||||||
|
<table class="table-fixed shadow-lg">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th
|
||||||
|
class="w-1/2 bg-blue-100 dark:bg-blue-900 border text-left px-8 py-4"
|
||||||
|
>
|
||||||
|
Name
|
||||||
|
</th>
|
||||||
|
<th
|
||||||
|
class="w-1/4 bg-blue-100 dark:bg-blue-900 border text-left px-8 py-4"
|
||||||
|
>
|
||||||
|
ID
|
||||||
|
</th>
|
||||||
|
<th
|
||||||
|
class="w-1/4 bg-blue-100 dark:bg-blue-900 border text-left px-8 py-4"
|
||||||
|
>
|
||||||
|
Default Prefix
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr v-for="site in sites">
|
||||||
|
<td class="border px-8 py-4">{{ site.name }}</td>
|
||||||
|
<td class="border px-8 py-4">{{ site.id }}</td>
|
||||||
|
<td class="border px-8 py-4">{{ site.default_prefix }}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="dark:text-white container mx-auto h-full flex justify-center items-center mt-12"
|
||||||
|
>
|
||||||
|
<h1 class="font-hairline mb-6 text-center">New Site</h1>
|
||||||
|
<form>
|
||||||
|
<label
|
||||||
|
>Name
|
||||||
|
<input type="text" placeholder="Name" />
|
||||||
|
</label>
|
||||||
|
<label>
|
||||||
|
Default Prefix
|
||||||
|
<input type="text" placeholder="Default Prefix" />
|
||||||
|
</label>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script lang="ts">
|
||||||
|
import { getSites, Site } from "../api";
|
||||||
|
import { ref } from "vue";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
name: "Sites",
|
||||||
|
setup() {
|
||||||
|
const sites = ref<Site[]>([]);
|
||||||
|
getSites().then((s) => void (sites.value = s));
|
||||||
|
return { sites };
|
||||||
|
},
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped></style>
|
Loading…
Add table
Reference in a new issue