Implement login frontend

This commit is contained in:
ReiMerc 2023-12-08 09:15:10 +01:00
parent 5141ebf0f1
commit 7c3bd135d1
3 changed files with 37 additions and 10 deletions

View File

@ -5,9 +5,10 @@ function request(method, path, data = null) {
if (xhr.status >= 200 && xhr.status < 300) if (xhr.status >= 200 && xhr.status < 300)
resolve(xhr.responseText ? JSON.parse(xhr.responseText) : null); resolve(xhr.responseText ? JSON.parse(xhr.responseText) : null);
else else
reject(xhr.responseText ? JSON.parse(xhr.responseText).title : "HTTP " + xhr.status); reject(xhr.responseText || "HTTP " + xhr.status);
} }
xhr.onerror = () => reject("Something went wrong"); xhr.onerror = () => reject("Something went wrong");
xhr.withCredentials = true;
xhr.open(method, import.meta.env.VITE_API_URL + path); xhr.open(method, import.meta.env.VITE_API_URL + path);
if (data) xhr.setRequestHeader("Content-Type", "application/json"), if (data) xhr.setRequestHeader("Content-Type", "application/json"),
xhr.send(JSON.stringify(data)); xhr.send(JSON.stringify(data));

View File

@ -1,17 +1,43 @@
<script setup> <script setup>
import { request } from "../assets/helpers.js";
import { ref } from "vue";
const username = ref(null);
const password = ref(null);
const error = ref("");
function login() {
request("POST", "/login", { username: username.value.value, password: password.value.value })
.then(success)
.catch(err => error.value = err);
}
function success() {
alert("Successfully logged in");
}
</script> </script>
<template> <template>
<main> <main>
<h1>LOGIN</h1> <h1>LOGIN</h1>
<br> <br>
<span>Brugernavn</span><br>
<input id="usernameInput" type="text"> <label>
<br><br> Username <br>
<span>Password</span><br> <input ref="username" type="text" required>
<input id="passwordInput" type="password"> </label>
<br><br><br>
<button>Login</button> <br><br>
<label>
Password <br>
<input ref="password" type="password" required>
</label>
<p class="error">{{ error }}</p>
<button @click="login">Login</button>
</main> </main>
</template> </template>

View File

@ -26,14 +26,14 @@ function success() {
<label> <label>
Username <br> Username <br>
<input ref="username" type="text"> <input ref="username" type="text" required>
</label> </label>
<br><br> <br><br>
<label> <label>
Password <br> Password <br>
<input ref="password" type="password"> <input ref="password" type="password" required>
</label> </label>
<p class="error">{{ error }}</p> <p class="error">{{ error }}</p>