router now working, implemented login and logout

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-06-30 23:40:26 +02:00
parent 56cdedcdb8
commit 956281e749
7 changed files with 113 additions and 24 deletions

View File

@@ -21,6 +21,7 @@
{#if translations_ready }
{#if user.name }
<!-- https://github.com/notnotsamuel/svelte-tiny-router -->
<Router>
<Menu />
<Route path="/" component={Home} />

View File

@@ -1,6 +1,8 @@
<script>
import { onMount } from 'svelte';
import { t } from '../translations.svelte.js';
import { tryLogin } from '../user.svelte.js';
import { checkUser, tryLogin } from '../user.svelte.js';
let credentials = { username : null, password : null }
function doLogin(ev){
@@ -10,6 +12,10 @@
function init(element){
element.focus();
}
onMount(async () => {
await checkUser();
});
</script>
<style>
label { display: block; margin: 5px; }

View File

@@ -1,17 +1,13 @@
<script>
import { logout, user } from '../user.svelte.js';
import { useTinyRouter } from 'svelte-tiny-router';
import { t } from '../translations.svelte.js';
import { user } from '../user.svelte.js';
const router = useTinyRouter();
function gotoAbout(){
router.navigate('/about');
}
function logout(){
user.name = null;
}
</script>
<nav>
<a on:click={() => router.navigate('/')}>{t('nav.Home')}</a>

View File

@@ -2,9 +2,29 @@ export const user = $state({
name : null
})
export async function checkUser(){
var url = `${location.protocol}//${location.host.replace('5173','8080')}/api/user/whoami`;
let response = await fetch(url,{
credentials: 'include'
});
if (response.ok){
const json = await response.json();
for (var key of Object.keys(json)) user[key] = json[key];
}
}
export async function logout(){
var url = `${location.protocol}//${location.host.replace('5173','8080')}/api/user/logout`;
await fetch(url,{
credentials: 'include'
});
user.name = null;
}
export async function tryLogin(credentials){
var url = `${location.protocol}//${location.host.replace('5173','8080')}/api/user/login`;
let response = await fetch(url,{
credentials: 'include',
headers: {
'Content-Type':'application/json'
},