Ver código fonte

Add logout + autonav

ghorsington 3 anos atrás
pai
commit
8efd142837

+ 31 - 4
web/src/routes/_layout.svelte

@@ -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 />	

+ 22 - 0
web/src/routes/login/check.ts

@@ -0,0 +1,22 @@
+import { Request as ExpressRequest, Response as ExpressResponse, json } from "express";
+import { rpcClient } from "src/utils/rpc";
+
+export interface AuthInfo {
+    loggedIn: boolean;
+    moderator: boolean;
+}
+
+type CheckResult = Promise<ExpressResponse<CheckResult>>;
+export const get = async (req: ExpressRequest, res: ExpressResponse): CheckResult => {
+    if (!req.session?.userId) {
+        return res.json({
+            loggedIn: false,
+            moderator: false,
+        });
+    }
+    const { authorised } = await rpcClient.userAuthorised({ userId: req.session.userId });
+    return res.json({
+        loggedIn: true,
+        moderator: authorised,
+    });
+};

+ 2 - 1
web/src/routes/login/discord_auth.svelte

@@ -18,7 +18,8 @@
       ok = result.ok;
       error = result.error;
     } else {
-      await goto("/");
+        // Force auth to happen again
+        window.open("/", "_self");
     }
   });
 </script>

+ 6 - 0
web/src/routes/logout.ts

@@ -0,0 +1,6 @@
+import { Request as ExpressRequest, Response as ExpressResponse } from "express";
+
+export const get = async (req: ExpressRequest, res: ExpressResponse): Promise<void> => {
+    req.session = null;
+    return res.redirect("/");
+};

+ 1 - 1
web/src/routes/rules/create.svelte

@@ -18,7 +18,7 @@
   import { onMount } from "svelte";
   import "easymde/dist/easymde.min.css";
   import { fade } from "svelte/transition";
-  import type { MDText } from "./md_interfaces";
+  import type { MDText } from "./md";
   import { Option } from "@shared/common/async_utils";
 
   export let rulesText!: string;

+ 4 - 1
web/src/routes/rules/md.ts

@@ -4,7 +4,10 @@ import { join } from "path";
 import { ENVIRONMENT } from "src/utils/environment";
 import { Option } from "@shared/common/async_utils";
 import { rpcClient } from "src/utils/rpc";
-import { MDText } from "./md_interfaces";
+
+export interface MDText {
+    text: string;
+}
 
 const FILE_PATH = join(ENVIRONMENT.dataPath, "rules.md");
 

+ 0 - 3
web/src/routes/rules/md_interfaces.ts

@@ -1,3 +0,0 @@
-export interface MDText {
-    text: string;
-}

+ 0 - 1
web/src/server.ts

@@ -43,7 +43,6 @@ const createSapperServer = async (): Promise<Express> => {
         sirv("static", { dev }),
         sapper.middleware({
             session: (req): AppSession => ({
-                userId: req.session?.userId,
             }),
         }),
     );

+ 1 - 1
web/src/utils/session.ts

@@ -1,5 +1,5 @@
+// eslint-disable-next-line @typescript-eslint/no-empty-interface
 export interface AppSession {
-    userId?: string;
 }
 
 export interface PageData {