diff options
| author | mat <github@matdoes.dev> | 2022-03-16 13:12:48 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-03-16 13:12:48 -0500 |
| commit | e6be291bd6787199f644b263c28c6d2c8b9b1d74 (patch) | |
| tree | 7f9671882fdc24498a63460dfc7618d78b615b63 /src/routes | |
| parent | f4e0719c4c9b3d4ba9be725700d47e6d229928f4 (diff) | |
| download | skyblock-stats-e6be291bd6787199f644b263c28c6d2c8b9b1d74.tar.gz skyblock-stats-e6be291bd6787199f644b263c28c6d2c8b9b1d74.tar.bz2 skyblock-stats-e6be291bd6787199f644b263c28c6d2c8b9b1d74.zip | |
Fix styling issues and add /login route
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/loggedin.ts | 36 | ||||
| -rw-r--r-- | src/routes/login.ts | 35 | ||||
| -rw-r--r-- | src/routes/player/[player]/[profile].svelte | 15 |
3 files changed, 80 insertions, 6 deletions
diff --git a/src/routes/loggedin.ts b/src/routes/loggedin.ts new file mode 100644 index 0000000..eb7236d --- /dev/null +++ b/src/routes/loggedin.ts @@ -0,0 +1,36 @@ +import { API_URL } from '$lib/api' +import type { RequestHandler } from '@sveltejs/kit' +import type { JSONValue } from '@sveltejs/kit/types/internal' + +export const get: RequestHandler = async ({ params }) => { + const code = params.code + const response = await fetch(`${API_URL}accounts/createsession`, { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ + code + }), + }).then(res => { + if (res.status !== 200) + throw new Error(res.statusText) + return res.json() + }) + console.log(response) + if (response.ok) { + return { + status: 303, + headers: { + location: '/verify', + 'Set-Cookie': `sid=${response.session_id}; Max-Age=31536000000; Path=/; HttpOnly; SameSite=Strict` + } + } + } + return { + status: 303, + headers: { + location: '/login', + } + } +} diff --git a/src/routes/login.ts b/src/routes/login.ts new file mode 100644 index 0000000..03ca5ee --- /dev/null +++ b/src/routes/login.ts @@ -0,0 +1,35 @@ +import type { RequestHandler } from '@sveltejs/kit' + +const DISCORD_CLIENT_ID = process.env.DISCORD_CLIENT_ID +if (!DISCORD_CLIENT_ID) + console.warn('DISCORD_CLIENT_ID is not set as an environment variable. This is required for logging in with Discord to work.') + +export const get: RequestHandler = async ({ request }) => { + const host = request.headers.get('host') + if (!DISCORD_CLIENT_ID) + return { + status: 500, + headers: { + 'content-type': 'text/plain', + }, + body: 'DISCORD_CLIENT_ID is not set as an environment variable. Please contact the owner of the website if this was expected to work.', + } + + if (!host) + return { + status: 400, + headers: { + 'content-type': 'text/plain', + }, + body: 'Host header is required.', + } + + const protocol = request.url.startsWith('https') ? 'https' : 'http' + + return { + status: 303, + headers: { + location: `https://discord.com/oauth2/authorize?client_id=${DISCORD_CLIENT_ID}&redirect_uri=${protocol}://${host}%2Floggedin&response_type=code&scope=identify` + } + } +} diff --git a/src/routes/player/[player]/[profile].svelte b/src/routes/player/[player]/[profile].svelte index b763b7f..b87741c 100644 --- a/src/routes/player/[player]/[profile].svelte +++ b/src/routes/player/[player]/[profile].svelte @@ -143,12 +143,14 @@ {/if} {#if data.member.stats} {#each categories as category} - <section> - <Collapsible id={category}> - <h2 slot="title">{cleanId(category)}</h2> - <StatList stats={data.member.stats.filter(s => s.category === category)} /> - </Collapsible> - </section> + {#if data.member.stats?.find(s => s.category === category)} + <section> + <Collapsible id={category}> + <h2 slot="title">{cleanId(category)}</h2> + <StatList stats={data.member.stats.filter(s => s.category === category)} /> + </Collapsible> + </section> + {/if} {/each} {/if} <section> @@ -207,6 +209,7 @@ section { margin-bottom: 0.5em; + max-width: fit-content; } @media only screen and (max-width: 1040px) { |
