Added basic login call logic

This commit is contained in:
Nicolai Ort 2021-08-18 16:50:46 +02:00
parent 5263d1974a
commit 0aab082b17
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
3 changed files with 35 additions and 2 deletions

View File

@ -43,4 +43,15 @@ export default class Apiclient {
}) })
).status; ).status;
} }
static async login(username, password) {
return (
await axios.post(`https://kauft.es/api/auth/login`, {
auth: {
username,
password
}
})
).data;
}
} }

View File

@ -0,0 +1,7 @@
<script>
import '../../app.postcss';
</script>
<div class="bg-white dark:bg-gray-800 h-screen">
<slot />
</div>

View File

@ -1,7 +1,20 @@
<script> <script>
import Apiclient from '$lib/Apiclient';
import UserStore from '$lib/UserStore'; import UserStore from '$lib/UserStore';
$: logged_in = $UserStore.isLoggedIn; $: username = "";
$: password = "";
async function login() {
let login = await Apiclient.login(username, password);
if (login.status != 200 && login.status != 201) {
error = login.message;
} else {
UserStore.login(login);
location.replace("/");
}
}
</script> </script>
<div class="w-full max-w-sm mx-auto overflow-hidden bg-white rounded-lg shadow-md dark:bg-gray-800"> <div class="w-full max-w-sm mx-auto overflow-hidden bg-white rounded-lg shadow-md dark:bg-gray-800">
@ -21,6 +34,7 @@
type="text" type="text"
placeholder="Username" placeholder="Username"
aria-label="Username" aria-label="Username"
value={username}
/> />
</div> </div>
@ -30,11 +44,12 @@
type="password" type="password"
placeholder="Password" placeholder="Password"
aria-label="Password" aria-label="Password"
value={password}
/> />
</div> </div>
<div class="flex items-center justify-between mt-4"> <div class="flex items-center justify-between mt-4">
<button <button on:click={login}
class="px-4 py-2 leading-5 text-white transition-colors duration-200 transform bg-gray-700 rounded hover:bg-gray-600 focus:outline-none" class="px-4 py-2 leading-5 text-white transition-colors duration-200 transform bg-gray-700 rounded hover:bg-gray-600 focus:outline-none"
type="button" type="button"
> >