_layout.svelte 1.8 KB

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