implement navbar user controls
This commit is contained in:
parent
579f16fc51
commit
6ab8db31c3
2 changed files with 77 additions and 2 deletions
75
web/pw-server/src/lib/components/navbar/UserControls.svelte
Normal file
75
web/pw-server/src/lib/components/navbar/UserControls.svelte
Normal file
|
@ -0,0 +1,75 @@
|
|||
<script lang="ts">
|
||||
import { get_session_token } from "$lib/auth";
|
||||
|
||||
import { onMount } from "svelte";
|
||||
|
||||
let user = null;
|
||||
|
||||
onMount(async () => {
|
||||
const session_token = get_session_token();
|
||||
if (!session_token) {
|
||||
return;
|
||||
}
|
||||
|
||||
let response = await fetch("/api/users/me", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${session_token}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw response.statusText;
|
||||
}
|
||||
|
||||
user = await response.json();
|
||||
});
|
||||
|
||||
function signOut() {
|
||||
// TODO: destroy session on server
|
||||
user = null;
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="user-controls">
|
||||
{#if user}
|
||||
<div class="current-user-name">
|
||||
{user["username"]}
|
||||
</div>
|
||||
<div class="sign-out" on:click={signOut}>Sign out</div>
|
||||
{:else}
|
||||
<a class="account-href" href="login">Sign in</a>
|
||||
<a class="account-href" href="register">Sign up</a>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style lang="scss">
|
||||
@mixin navbar-item {
|
||||
font-size: 18px;
|
||||
font-family: Helvetica, sans-serif;
|
||||
padding: 4px 8px;
|
||||
}
|
||||
|
||||
.account-href {
|
||||
@include navbar-item;
|
||||
color: #eee;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.current-user-name {
|
||||
@include navbar-item;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.sign-out {
|
||||
@include navbar-item;
|
||||
color: #ccc;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.user-controls {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
</style>
|
|
@ -1,5 +1,5 @@
|
|||
<script lang="ts">
|
||||
import NavbarUserSection from "$lib/components/navbar/NavbarUserSection.svelte";
|
||||
import UserControls from "$lib/components/navbar/UserControls.svelte";
|
||||
|
||||
import "./style.css";
|
||||
</script>
|
||||
|
@ -9,7 +9,7 @@
|
|||
<div class="navbar-main">
|
||||
<a href="/">PlanetWars</a>
|
||||
</div>
|
||||
<NavbarUserSection />
|
||||
<UserControls />
|
||||
</div>
|
||||
<slot />
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue