_layout.svelte 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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) {
  24. if (!path.startsWith("/rules")) {
  25. if (result.moderator) {
  26. return this.redirect(302, "/rules/edit");
  27. }
  28. return this.redirect(302, "/rules");
  29. }
  30. if (!result.moderator && path != "/rules") {
  31. return this.redirect(302, "/rules");
  32. }
  33. }
  34. return result;
  35. }
  36. </script>
  37. <script lang="typescript">
  38. import { stores } from "@sapper/app";
  39. const { page } = stores();
  40. export let loggedIn!: boolean;
  41. let path: string;
  42. $: path = $page.path.slice(1);
  43. </script>
  44. <style global>
  45. @import "tailwindcss/base";
  46. @import "tailwindcss/components";
  47. @import "tailwindcss/utilities";
  48. * {
  49. @apply font-sans;
  50. }
  51. nav {
  52. @apply fixed w-screen h-12 shadow-lg flex flex-row justify-end items-center px-4 z-10;
  53. background-color: #0a0d13;
  54. a {
  55. @apply text-white px-5 py-3;
  56. &:hover {
  57. @apply bg-gray-800 cursor-pointer;
  58. }
  59. }
  60. }
  61. </style>
  62. <svelte:head>
  63. <title>{path ? path.charAt(0).toUpperCase() + path.slice(1) : 'Index'}</title>
  64. </svelte:head>
  65. {#if loggedIn}
  66. <nav><a href="/logout">Log out</a></nav>
  67. {/if}
  68. <div class="flex items-center justify-center bg-gray-900 h-screen w-screen">
  69. <slot />
  70. </div>