|
@@ -1,19 +1,26 @@
|
|
|
<script lang="typescript" context="module">
|
|
|
import type { AppSession, PageData, PreloadContext } from "src/utils/session";
|
|
|
+ import type { AuthInfo } from "src/routes/login/check";
|
|
|
import { goto } from "@sapper/app";
|
|
|
|
|
|
export async function preload(this: PreloadContext, { path }: PageData, session: AppSession) {
|
|
|
+ const authResult = await this.fetch("/login/check");
|
|
|
+ const result = (await authResult.json()) as AuthInfo;
|
|
|
+ console.log(`Auth: ${result.loggedIn}`);
|
|
|
if (path.startsWith("/login/")) {
|
|
|
- if (session?.userId) {
|
|
|
+ if (result.loggedIn) {
|
|
|
return this.redirect(302, "/");
|
|
|
} else {
|
|
|
- return {};
|
|
|
+ return result;
|
|
|
}
|
|
|
}
|
|
|
- if (path != "/" && !session?.userId) {
|
|
|
+ if (path != "/" && !result.loggedIn) {
|
|
|
return this.redirect(302, "/");
|
|
|
}
|
|
|
- return {};
|
|
|
+ if (path != "/rules/create" && result.moderator) {
|
|
|
+ return this.redirect(302, "/rules/create");
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
</script>
|
|
|
|
|
@@ -21,6 +28,7 @@
|
|
|
import { stores } from "@sapper/app";
|
|
|
const { page } = stores();
|
|
|
|
|
|
+ export let loggedIn!: boolean;
|
|
|
let path: string;
|
|
|
$: path = $page.path.slice(1);
|
|
|
</script>
|
|
@@ -33,6 +41,19 @@
|
|
|
* {
|
|
|
@apply font-sans;
|
|
|
}
|
|
|
+
|
|
|
+nav {
|
|
|
+ @apply fixed w-screen h-12 shadow-lg flex flex-row justify-end items-center px-4;
|
|
|
+ background-color: #0a0d13;
|
|
|
+
|
|
|
+ a {
|
|
|
+ @apply text-white px-5 py-3;
|
|
|
+
|
|
|
+ &:hover {
|
|
|
+ @apply bg-gray-800 cursor-pointer;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
</style>
|
|
|
|
|
|
<svelte:head>
|
|
@@ -41,6 +62,12 @@
|
|
|
</title>
|
|
|
</svelte:head>
|
|
|
|
|
|
+{#if loggedIn}
|
|
|
+<nav>
|
|
|
+ <a href="/logout">Log out</a>
|
|
|
+</nav>
|
|
|
+{/if}
|
|
|
+
|
|
|
|
|
|
<div class="flex items-center justify-center bg-gray-900 h-screen w-screen">
|
|
|
<slot />
|