diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts
new file mode 100644
index 0000000..b1d4d9b
--- /dev/null
+++ b/src/routes/+page.server.ts
@@ -0,0 +1,38 @@
+import { building } from "$app/environment";
+import { DISCORD_USER_TOKEN } from "$env/static/private";
+
+let memberCount = 0;
+let roleMembers: { [key: string]: number } = {
+ "1214817156420862012": 50,
+};
+async function setMemberCount() {
+ console.log("Fetching member count");
+ const promises = [
+ fetch("https://discord.com/api/v9/invites/suyu?with_counts=true&with_expiration=true"),
+ DISCORD_USER_TOKEN
+ ? fetch("https://discord.com/api/v9/guilds/1214371687114477618/roles/member-counts", {
+ headers: {
+ Authorization: DISCORD_USER_TOKEN,
+ },
+ })
+ : Promise.resolve({ json: () => roleMembers }),
+ ];
+ const [res, roles] = await Promise.all(promises);
+ const jsonPromises = [res.json(), roles.json()];
+ const [resJson, rolesJson] = await Promise.all(jsonPromises);
+ memberCount = resJson.approximate_member_count;
+ if (DISCORD_USER_TOKEN) roleMembers = rolesJson;
+ console.log("Member count:", memberCount);
+}
+
+if (!building) {
+ await setMemberCount();
+ setInterval(setMemberCount, 1000 * 60 * 10);
+}
+
+export async function load(opts) {
+ return {
+ memberCount,
+ roleMembers,
+ };
+}
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte
index 4ccc7c2..97f9b4c 100644
--- a/src/routes/+page.svelte
+++ b/src/routes/+page.svelte
@@ -1,18 +1,12 @@
-
- a
+
+
+
+
+ suyu Online Services
+
+
+ Your token should be kept private. If you believe it has been compromised, please
+ contact us immediately.
+
+
+
+
diff --git a/src/routes/jwt/internal/+server.ts b/src/routes/jwt/internal/+server.ts
index 7294ef7..ba144e7 100644
--- a/src/routes/jwt/internal/+server.ts
+++ b/src/routes/jwt/internal/+server.ts
@@ -5,7 +5,9 @@ import jwt from "jsonwebtoken";
export async function POST({ request }) {
const userKey = `${request.headers.get("x-username")}:${request.headers.get("x-token")}`;
const user = await useAuth(userKey);
- const token = jwt.sign({ ...user }, Buffer.from(PRIVATE_KEY), { algorithm: "RS256" });
+ const token = jwt.sign({ ...user, apiKey: userKey }, Buffer.from(PRIVATE_KEY), {
+ algorithm: "RS256",
+ });
return new Response(token, {
headers: {
"content-type": "text/html",
diff --git a/src/routes/lobby/+server.ts b/src/routes/lobby/+server.ts
index ae7a03c..dd223c5 100644
--- a/src/routes/lobby/+server.ts
+++ b/src/routes/lobby/+server.ts
@@ -3,6 +3,7 @@ import { userRepo } from "$lib/server/repo/index.js";
import { SuyuUser } from "$lib/server/schema";
import { PUBLIC_KEY } from "$lib/server/secrets/secrets.json";
import { json } from "$lib/server/util";
+import { useAuth } from "$lib/util/api/index.js";
import type { IJwtData } from "$types/auth.js";
import type { IRoom, LobbyResponse } from "$types/rooms";
import jwt from "jsonwebtoken";
@@ -37,11 +38,11 @@ export async function POST({ request, getClientAddress }) {
return new Response(null, { status: 400 });
}
}
- const token = request.headers.get("authorization")?.replace("Bearer ", "");
+ const token = request.headers.get("authorization");
if (!token) return new Response(null, { status: 401 });
// TODO: jwt utils which type and validate automatically
- const data = jwt.verify(token, Buffer.from(PUBLIC_KEY), { algorithms: ["RS256"] }) as IJwtData;
- const user = await userRepo.findOne({ where: { id: data.id } });
+ const user = await useAuth(token);
+ console.log(user);
if (!user) return new Response(null, { status: 401 });
const room = RoomManager.createRoom({
name: body.name,
diff --git a/src/routes/profile/+server.ts b/src/routes/profile/+server.ts
index 8d73e29..a5c82c2 100644
--- a/src/routes/profile/+server.ts
+++ b/src/routes/profile/+server.ts
@@ -3,6 +3,7 @@ import { useAuth } from "$lib/util/api/index.js";
export async function GET({ request }) {
const user = await useAuth(request.headers.get("authorization") || "");
+ console.log(user);
if (!user) return new Response(null, { status: 401 });
return json({
username: user.username,
diff --git a/src/routes/signup/+page.svelte b/src/routes/signup/+page.svelte
index bc5e8f5..a13947d 100644
--- a/src/routes/signup/+page.svelte
+++ b/src/routes/signup/+page.svelte
@@ -5,6 +5,11 @@
import { PUBLIC_SITE_KEY } from "$env/static/public";
import { SuyuAPI } from "$lib/client/api";
import type { PageData } from "./$types";
+ import type { Writable } from "svelte/store";
+ import { getContext } from "svelte";
+
+ const token = getContext
>("token");
+ if ($token) goto("/account");
let usernameInput = "";
let emailInput = "";
@@ -28,6 +33,7 @@
}
// set "token" cookie
document.cookie = `token=${res.token}; path=/; max-age=31536000; samesite=strict`;
+ $token = res.token;
goto("/account");
}
@@ -57,7 +63,9 @@
-
+
+
+
diff --git a/src/types/auth.d.ts b/src/types/auth.d.ts
index 1b44c3d..4fb2681 100644
--- a/src/types/auth.d.ts
+++ b/src/types/auth.d.ts
@@ -5,4 +5,5 @@ export interface IJwtData {
avatarUrl: string;
roles: string;
iat: number;
+ apiKey: string;
}
diff --git a/vite.config.ts b/vite.config.ts
index 1ccbe54..5df527b 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -1,7 +1,14 @@
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
-import { ViteImageOptimizer } from "vite-plugin-image-optimizer";
+import { imagetools } from "vite-imagetools";
export default defineConfig({
- plugins: [ViteImageOptimizer(), sveltekit()],
+ plugins: [
+ imagetools({
+ defaultDirectives: new URLSearchParams({
+ format: "webp",
+ }),
+ }),
+ sveltekit(),
+ ],
});