-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,69 @@ | ||
<script lang="ts"> | ||
import { goto } from '$app/navigation'; | ||
import { base } from '$app/paths'; | ||
import { users } from '$lib/stores/userStore'; | ||
import { authCookieLogout } from '$lib/client/services.gen'; | ||
import { type UserRead } from '$lib/client/types.gen'; | ||
import { currentUser } from '$lib/stores/userStore'; | ||
import { Button, Heading, Popover } from 'flowbite-svelte'; | ||
import { onDestroy } from 'svelte'; | ||
import { _ } from 'svelte-i18n'; | ||
import { get } from 'svelte/store'; | ||
import AlertMessage from './AlertMessage.svelte'; | ||
export let triggeredBy = ''; | ||
let loggedIn: string | null = users.get()['loggedIn']; | ||
let userData: UserData | null = loggedIn ? users.fetch(loggedIn) : null; | ||
const unsubscribe = users.subscribe((currentUserData) => { | ||
loggedIn = currentUserData['loggedIn']; | ||
let userData: UserRead | null = get(currentUser); | ||
let showAlert: boolean = false; | ||
let alertMessage: string = $_('login.alertMessageError'); | ||
if (loggedIn && loggedIn !== null) { | ||
userData = users.fetch(loggedIn); | ||
} | ||
const unsubscribe = currentUser.subscribe((data) => { | ||
console.log('data: ', data); | ||
userData = data; | ||
}); | ||
function logout() { | ||
users.get()['loggedIn'] = null; | ||
userData = null; | ||
async function logout(): Promise<void> { | ||
const response = await authCookieLogout(); | ||
if (response.error) { | ||
console.log('Error during logout: ', response.response.status, response.error.detail); | ||
showAlert = true; | ||
alertMessage += ': ' + response.error.detail; | ||
} else { | ||
console.log('Successful logout of user ', userData.email, response.response.status); | ||
userData = null; | ||
goto(`/${base}`); | ||
} | ||
} | ||
onDestroy(unsubscribe); | ||
$: console.log('userData: ', userData); | ||
</script> | ||
|
||
<Popover {triggeredBy} class="text-gray-700 dark:text-gray-400"> | ||
{#if showAlert} | ||
<AlertMessage | ||
title={$_('login.alertMessageTitle')} | ||
message={alertMessage} | ||
onclick={() => { | ||
showAlert = false; | ||
}} | ||
/> | ||
{/if} | ||
{#if userData !== null} | ||
<div class="mx-auto flex flex-col items-center justify-center"> | ||
<p class="m-2 w-full rounded-lg border p-2 text-lg font-semibold">{userData.name}</p> | ||
<Button href={`${base}/`} on:click={logout} size="lg">Logout</Button> | ||
<div class="mx-auto mb-6 flex flex-col items-center justify-center space-y-6"> | ||
<p class="m-2 w-full rounded-lg p-2 font-semibold">{userData.email}</p> | ||
<Button class="m-2 w-full" on:click={logout} size="lg" | ||
>{$_('login.profileButtonLabelLogout')}</Button | ||
> | ||
</div> | ||
{:else} | ||
<div class="mx-auto mb-6 flex flex-col items-center justify-center space-y-6"> | ||
<Heading tag="h3">Willkommen!</Heading> | ||
<Button href="{base}/userLand/userLogin">Einloggen oder Registrieren</Button> | ||
<Heading tag="h3" class="mx-auto flex w-full justify-center" | ||
>{$_('login.profileTitleDefault')}</Heading | ||
> | ||
<Button class="m-2 w-full" href="{base}/userLand/userLogin" size="lg" | ||
>{$_('login.profileButtonLabelDefault')}</Button | ||
> | ||
</div> | ||
{/if} | ||
</Popover> |