Explorar o código

Implement verifying

ghorsington %!s(int64=3) %!d(string=hai) anos
pai
achega
d2cc9dbbee

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
+web_data/*
 build/
 data/
 lib/

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

@@ -21,11 +21,16 @@
     if (path != "/" && !result.loggedIn) {
       return this.redirect(302, "/");
     }
-    if (result.loggedIn && !path.startsWith("/rules")) {
-      if (result.moderator) {
-        return this.redirect(302, "/rules/edit");
+    if (result.loggedIn) {
+      if (!path.startsWith("/rules")) {
+        if (result.moderator) {
+          return this.redirect(302, "/rules/edit");
+        }
+        return this.redirect(302, "/rules");
+      }
+      if (!result.moderator && path != "/rules") {
+        return this.redirect(302, "/rules");
       }
-      return this.redirect(302, "/rules");
     }
     return result;
   }

+ 21 - 9
web/src/routes/rules/index.svelte

@@ -24,15 +24,15 @@
 
 <script lang="typescript">
   import { Converter } from "showdown";
-  import { onMount } from "svelte";
+  import { onMount, tick } from "svelte";
   import Icon from "svelte-awesome/components/Icon.svelte";
   import { faSpinner } from "@fortawesome/free-solid-svg-icons";
   import { fade } from "svelte/transition";
+  import type { VerifyRequest } from "./verify";
 
   export let rulesText: string = "";
   export let sitekey: string = "";
   export let verified!: boolean;
-  console.log(verified);
   let showVerify: boolean = false;
   const spinner = (faSpinner as unknown) as undefined;
   let htmlContent = new Converter().makeHtml(rulesText);
@@ -46,10 +46,24 @@
 
   let state: State = State.None;
   let message: string = "";
-  function onVerified(e: { key: string }) {
-    // console.log(e.key);
+  async function onVerified(e: { key: string }) {
     state = State.Verify;
-    // message = "wew";
+    const result = await fetch("/rules/verify", {
+      method: "post",
+      headers: {
+        Accept: "application/json",
+        "Content-Type": "application/json",
+      },
+      body: JSON.stringify({ captchaResponse: e.key } as VerifyRequest),
+    });
+    const opt = (await result.json()) as Option<unknown, { error: string }>;
+    if (!opt.ok) {
+      state = State.Error;
+      message = opt.error;
+    } else {
+      state = State.Message;
+      message = "Verification done! Welcome to the server!";
+    }
   }
 
   function onExpired() {
@@ -98,10 +112,8 @@
           them</label>
       </form>
       {#if showVerify}
-        <a
-          href="asd"
-          on:click|preventDefault={() => onVerified({ key: 'wew' })}>Verify test</a>
-        <!-- <h-captcha site-key={sitekey} dark on:verified={onVerified} on:expired={onExpired} on:error={onError} />   -->
+        <!-- <a href="asd" on:click|preventDefault={() => onVerified({ key: 'wew' })}>Verify test</a> -->
+        <h-captcha site-key={sitekey} dark on:verified={onVerified} on:expired={onExpired} on:error={onError} />  
       {/if}
       <div class="py-2 pointer-events-none select-none">
         {#if state == State.Verify}

+ 1 - 1
web/src/routes/rules/markdown-dark.css

@@ -52,7 +52,7 @@
         margin-left: 24px;
     }
     p, ul, ol {
-        font-size: 18px;
+        font-size: 16px;
         line-height: 24px;
         max-width: 540px;
     }

+ 0 - 2
web/src/routes/rules/verify.ts

@@ -1,6 +1,4 @@
 import { Request as ExpressRequest, Response as ExpressResponse } from "express";
-import { existsSync, promises } from "fs";
-import { join } from "path";
 import { ENVIRONMENT } from "src/utils/environment";
 import { Option, tryDo } from "@shared/common/async_utils";
 import { rpcClient } from "src/utils/rpc";