ff-admin/src/views/Login.vue

193 lines
6.1 KiB
Vue
Raw Normal View History

2024-08-23 14:42:32 +02:00
<template>
<div class="grow flex items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
<div class="max-w-md w-full space-y-8 pb-20">
<div class="flex flex-col items-center gap-4">
2025-04-24 16:49:14 +02:00
<AppLogo />
2025-01-18 16:45:03 +01:00
<h2 class="text-center text-4xl font-extrabold text-gray-900">
2025-04-25 08:18:27 +02:00
{{ clubName }}
2025-01-18 16:45:03 +01:00
</h2>
2024-08-23 14:42:32 +02:00
</div>
2025-05-05 14:21:22 +02:00
<form class="flex flex-col gap-2" @submit.prevent="submit">
2024-08-23 14:42:32 +02:00
<div class="-space-y-px">
2025-05-05 14:21:22 +02:00
<div class="relative">
<input
id="username"
name="username"
type="text"
required
placeholder="Benutzer"
:class="routine == '' ? '' : 'rounded-b-none!'"
:value="username"
:disabled="username != ''"
/>
<div v-if="usernameStatus" class="h-full flex items-center justify-center w-5 absolute top-0 right-2">
<Spinner v-if="usernameStatus == 'loading'" class="my-auto" />
<SuccessCheckmark v-else-if="usernameStatus == 'success'" />
<FailureXMark v-else-if="usernameStatus == 'failed'" />
</div>
2024-08-23 14:42:32 +02:00
</div>
2025-05-05 14:21:22 +02:00
<div v-if="routine != ''">
2024-10-08 10:43:16 +02:00
<input
2025-05-05 14:21:22 +02:00
v-if="routine == 'totp'"
2024-10-08 10:43:16 +02:00
id="totp"
name="totp"
type="text"
required
placeholder="TOTP"
2025-04-12 15:17:44 +02:00
class="rounded-t-none!"
2024-10-08 10:43:16 +02:00
autocomplete="off"
/>
2025-05-05 14:21:22 +02:00
<input
v-else
id="password"
name="password"
type="password"
required
placeholder="Passwort"
class="rounded-t-none!"
2025-05-06 08:38:28 +02:00
autocomplete="current-password"
2025-05-05 14:21:22 +02:00
/>
2024-08-23 14:42:32 +02:00
</div>
</div>
2025-05-05 14:21:22 +02:00
<p v-if="username != ''" class="w-fit self-end text-primary cursor-pointer" @click="resetRoutine">
Benutzer wechseln
</p>
<RouterLink :to="{ name: 'reset-start' }" class="w-fit self-end text-primary">Zugang verloren</RouterLink>
2024-08-23 14:42:32 +02:00
<div class="flex flex-row gap-2">
<button type="submit" primary :disabled="loginStatus == 'loading' || loginStatus == 'success'">
2025-05-05 14:21:22 +02:00
{{ routine == "" ? "Benutzer prüfen" : "anmelden" }}
2024-08-23 14:42:32 +02:00
</button>
<Spinner v-if="loginStatus == 'loading'" class="my-auto" />
<SuccessCheckmark v-else-if="loginStatus == 'success'" />
<FailureXMark v-else-if="loginStatus == 'failed'" />
</div>
<p v-if="loginError" class="text-center">{{ loginError }}</p>
</form>
2024-11-23 12:11:26 +01:00
<FormBottomBar />
2024-08-23 14:42:32 +02:00
</div>
</div>
</template>
<script setup lang="ts">
import { defineComponent } from "vue";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
2024-09-17 16:44:02 +02:00
import { resetAllPiniaStores } from "@/helpers/piniaReset";
2024-11-23 12:11:26 +01:00
import FormBottomBar from "@/components/FormBottomBar.vue";
2025-04-24 16:49:14 +02:00
import AppLogo from "@/components/AppLogo.vue";
2025-04-25 08:18:27 +02:00
import { mapState } from "pinia";
import { useConfigurationStore } from "@/stores/configuration";
2025-05-05 14:21:22 +02:00
import { hashString } from "../helpers/crypto";
2024-08-23 14:42:32 +02:00
</script>
<script lang="ts">
export default defineComponent({
data() {
return {
loginStatus: undefined as undefined | "loading" | "success" | "failed",
2025-05-05 14:21:22 +02:00
usernameStatus: undefined as undefined | "loading" | "success" | "failed",
2024-08-23 14:42:32 +02:00
loginError: "" as string,
2025-05-05 14:21:22 +02:00
username: "" as string,
routine: "" as string,
2024-08-23 14:42:32 +02:00
};
},
2025-04-25 08:18:27 +02:00
computed: {
...mapState(useConfigurationStore, ["clubName"]),
},
2024-09-05 16:25:25 +02:00
mounted() {
resetAllPiniaStores();
2025-05-05 14:21:22 +02:00
this.username = localStorage.getItem("username") ?? "";
this.routine = localStorage.getItem("routine") ?? "";
if (this.username != "") {
this.$http
.post(`/auth/kickof`, {
username: this.username,
})
.then((result) => {
this.usernameStatus = "success";
this.routine = result.data.routine;
localStorage.setItem("routine", result.data.routine);
})
.catch((err) => {
this.usernameStatus = "failed";
this.loginError = err.response?.data;
});
}
2024-09-05 16:25:25 +02:00
},
2024-08-23 14:42:32 +02:00
methods: {
2025-05-05 14:21:22 +02:00
resetRoutine() {
this.routine = "";
this.username = "";
localStorage.removeItem("routine");
localStorage.removeItem("username");
},
submit(e: any) {
if (this.routine == "") this.kickof(e);
else this.login(e);
},
kickof(e: any) {
let formData = e.target.elements;
let username = formData.username.value;
this.usernameStatus = "loading";
this.loginError = "";
this.$http
.post(`/auth/kickof`, {
username: username,
})
.then((result) => {
this.usernameStatus = "success";
this.routine = result.data.routine;
this.username = username;
localStorage.setItem("routine", result.data.routine);
localStorage.setItem("username", username);
})
.catch((err) => {
this.usernameStatus = "failed";
this.loginError = err.response?.data;
})
.finally(() => {
setTimeout(() => {
this.usernameStatus = undefined;
this.loginError = "";
}, 2000);
});
},
async login(e: any) {
2024-08-23 14:42:32 +02:00
let formData = e.target.elements;
this.loginStatus = "loading";
this.loginError = "";
2025-05-05 14:21:22 +02:00
let secret = "";
if (this.routine == "totp") {
secret = formData.totp.value;
} else {
secret = await hashString(formData.password.value);
}
2024-08-23 14:42:32 +02:00
this.$http
.post(`/auth/login`, {
username: formData.username.value,
2025-05-05 14:21:22 +02:00
secret: secret,
2024-08-23 14:42:32 +02:00
})
.then((result) => {
this.loginStatus = "success";
2024-10-03 13:43:13 +02:00
localStorage.setItem("accessToken", result.data.accessToken);
localStorage.setItem("refreshToken", result.data.refreshToken);
setTimeout(() => {
this.$router.push(`/admin`);
}, 1000);
2024-08-23 14:42:32 +02:00
})
.catch((err) => {
this.loginStatus = "failed";
2025-02-10 11:34:12 +01:00
this.loginError = err.response?.data;
2024-08-23 14:42:32 +02:00
});
},
},
});
</script>