_layout.svelte 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <script lang="typescript" context="module">
  2. import type { AppSession, PageData, PreloadContext } from "src/utils/session";
  3. import type { AuthInfo } from "src/routes/login/check";
  4. import { goto } from "@sapper/app";
  5. export async function preload(this: PreloadContext, { path }: PageData, session: AppSession) {
  6. const authResult = await this.fetch("/login/check");
  7. const result = (await authResult.json()) as AuthInfo;
  8. console.log(`Auth: ${result.loggedIn}`);
  9. if (path.startsWith("/login/")) {
  10. if (result.loggedIn) {
  11. return this.redirect(302, "/");
  12. } else {
  13. return result;
  14. }
  15. }
  16. if (path != "/" && !result.loggedIn) {
  17. return this.redirect(302, "/");
  18. }
  19. if (path != "/rules/create" && result.moderator) {
  20. return this.redirect(302, "/rules/create");
  21. }
  22. return result;
  23. }
  24. </script>
  25. <script lang="typescript">
  26. import { stores } from "@sapper/app";
  27. const { page } = stores();
  28. export let loggedIn!: boolean;
  29. let path: string;
  30. $: path = $page.path.slice(1);
  31. </script>
  32. <style global>
  33. @import "tailwindcss/base";
  34. @import "tailwindcss/components";
  35. @import "tailwindcss/utilities";
  36. * {
  37. @apply font-sans;
  38. }
  39. nav {
  40. @apply fixed w-screen h-12 shadow-lg flex flex-row justify-end items-center px-4;
  41. background-color: #0a0d13;
  42. a {
  43. @apply text-white px-5 py-3;
  44. &:hover {
  45. @apply bg-gray-800 cursor-pointer;
  46. }
  47. }
  48. }
  49. </style>
  50. <svelte:head>
  51. <title>
  52. {path ? path.charAt(0).toUpperCase() + path.slice(1) : "Index"}
  53. </title>
  54. </svelte:head>
  55. {#if loggedIn}
  56. <nav>
  57. <a href="/logout">Log out</a>
  58. </nav>
  59. {/if}
  60. <div class="flex items-center justify-center bg-gray-900 h-screen w-screen">
  61. <slot />
  62. </div>