_layout.svelte 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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", { credentials: "include" });
  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) {
  24. if (!path.startsWith("/rules")) {
  25. return this.redirect(302, "/rules");
  26. }
  27. if (!result.moderator && path != "/rules") {
  28. return this.redirect(302, "/rules");
  29. }
  30. }
  31. return result;
  32. }
  33. </script>
  34. <script lang="typescript">
  35. import { stores } from "@sapper/app";
  36. const { page } = stores();
  37. export let loggedIn!: boolean;
  38. export let moderator!: boolean;
  39. let path: string;
  40. $: {
  41. path = $page.path.slice(1);
  42. console.log(path);
  43. }
  44. const ROUTES_NAV = [
  45. { route: "rules", moderator: false, name: "Rules" },
  46. { route: "rules/edit", moderator: true, name: "Edit rules" },
  47. ];
  48. </script>
  49. <style global>
  50. @import "tailwindcss/base";
  51. @import "tailwindcss/components";
  52. @import "tailwindcss/utilities";
  53. * {
  54. @apply font-sans;
  55. }
  56. nav {
  57. @apply fixed w-screen h-12 shadow-lg flex flex-row items-center px-4 z-10;
  58. background-color: #0a0d13;
  59. a {
  60. @apply text-white px-5 py-3;
  61. &:hover {
  62. @apply bg-gray-800 cursor-pointer;
  63. }
  64. &.active {
  65. @apply bg-gray-800;
  66. }
  67. }
  68. }
  69. </style>
  70. {#if loggedIn}
  71. <nav>
  72. {#each ROUTES_NAV as route (route.route) }
  73. {#if !route.moderator || (route.moderator && moderator) }
  74. <a href="/{route.route}" class:active={route.route == path}>{route.name}</a>
  75. {/if}
  76. {/each}
  77. <span class="mx-auto"></span>
  78. <a href="/logout">Log out</a>
  79. </nav>
  80. {/if}
  81. <div class="flex items-center justify-center bg-gray-900 h-screen w-screen">
  82. <slot />
  83. </div>