diff options
| author | mat <github@matdoes.dev> | 2022-12-15 14:59:56 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-12-15 14:59:56 -0600 |
| commit | 6e723aadf6de45a79b4ef64d288ea275628232c5 (patch) | |
| tree | c27bcc33aefa9212baf6f3d9c2eb1af88d6c5105 /src/routes/verify | |
| parent | 89bf3d31e36ad3bdfd45461ee6fb69a4c791f848 (diff) | |
| download | skyblock-stats-6e723aadf6de45a79b4ef64d288ea275628232c5.tar.gz skyblock-stats-6e723aadf6de45a79b4ef64d288ea275628232c5.tar.bz2 skyblock-stats-6e723aadf6de45a79b4ef64d288ea275628232c5.zip | |
start updating to sveltekit v1
Diffstat (limited to 'src/routes/verify')
| -rw-r--r-- | src/routes/verify/+page.server.ts | 75 | ||||
| -rw-r--r-- | src/routes/verify/+page.svelte | 66 |
2 files changed, 141 insertions, 0 deletions
diff --git a/src/routes/verify/+page.server.ts b/src/routes/verify/+page.server.ts new file mode 100644 index 0000000..3321164 --- /dev/null +++ b/src/routes/verify/+page.server.ts @@ -0,0 +1,75 @@ +import { fetchApi } from '$lib/api' +import type { AccountSchema, CleanUser, SessionSchema } from '$lib/APITypes' +import type { RequestHandler } from '@sveltejs/kit' +import env from '$lib/env' + + +function redirect(status: number, location: string) { + return { + status, + headers: { + location, + }, + } +} + +export const post: RequestHandler = async ({ request, locals, platform }) => { + const key = env(platform).SKYBLOCK_STATS_API_KEY + if (!key) { + return redirect(303, `/verify?error=NO_KEY`) + } + if (locals.sid === undefined) { + return redirect(303, '/login') + } + + const form = await request.formData() + + // username or uuid + const playerIdentifier = form.get('ign') + if (!playerIdentifier) { + return redirect(303, `/verify?error=NO_IGN`) + } + + const playerResponse: CleanUser = await fetchApi(`player/${playerIdentifier}`, fetch).then(res => res.json()) + const sessionResponse: { session: SessionSchema | null, account: AccountSchema | null } = await fetchApi(`accounts/session`, fetch, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + uuid: locals.sid, + }), + }).then(r => r.json()) + + if (!sessionResponse.session) + return redirect(303, '/login') + + const hypixelDiscordName = playerResponse.player?.socials.discord + + if (!hypixelDiscordName) + return redirect(303, `/verify?error=NOT_LINKED`) + + const discordUser = sessionResponse.session.discord_user + const actualDiscordName = discordUser.name + // some people link themselves as <id>#<discrim> instead of <name>#<discrim> + const actualDiscordIdDiscrim = `${discordUser.id}#${discordUser.name.split('#')[1]}` + + if (!(hypixelDiscordName === actualDiscordName || hypixelDiscordName === actualDiscordIdDiscrim)) + return redirect(303, `/verify?error=WRONG_NAME¤t=${encodeURIComponent(hypixelDiscordName)}&correct=${encodeURIComponent(actualDiscordName)}`) + + const updatedAccount: AccountSchema = { + discordId: sessionResponse.session.discord_user.id, + minecraftUuid: playerResponse.player?.uuid + } + + await fetchApi(`accounts/update`, fetch, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + key: key + }, + body: JSON.stringify(updatedAccount), + }).then(r => r.json()) + + return redirect(303, '/profile') +}
\ No newline at end of file diff --git a/src/routes/verify/+page.svelte b/src/routes/verify/+page.svelte new file mode 100644 index 0000000..20de403 --- /dev/null +++ b/src/routes/verify/+page.svelte @@ -0,0 +1,66 @@ +<script lang="ts" context="module"> + import type { Load } from '@sveltejs/kit' + export const load: Load = async ({ session, url }) => { + if (session.sid === undefined) { + return { redirect: '/login', status: 303 } + } + return { + props: { + errorCode: url.searchParams.get('error'), + current: url.searchParams.get('current'), + correct: url.searchParams.get('correct'), + }, + } + } +</script> + +<script lang="ts"> + import Emoji from '$lib/Emoji.svelte' + import Head from '$lib/Head.svelte' + import Header from '$lib/Header.svelte' + + export let errorCode: string | null + export let current: string | null + export let correct: string | null + + const errorCodes = { + NO_IGN: 'Please enter a valid Minecraft username.', + NOT_LINKED: + 'Please link your Discord in Hypixel by doing /profile -> Social media -> Discord. If you just changed it, wait a few minutes and try again.', + WRONG_NAME: `You're linked to ${current} on Hypixel. Please change this to ${correct} by doing /profile -> Social media -> Discord. If you just changed it, wait a few minutes and try again.`, + NO_KEY: + "This instance of skyblock-stats doesn't have a skyblock-api key set. Please contact the owner of the website if you believe this to be a mistake.", + } +</script> + +<Head title="Verify Account" /> +<Header /> + +<main> + <h1>Verify Minecraft account</h1> + <p>Please enter your Minecraft username to verify that this is your account.</p> + <p> + This will check with the Hypixel API that your Discord username matches the Discord name set in + your Hypixel settings. + </p> + {#if errorCode && errorCode in errorCodes} + <div class="error"> + <Emoji value="🚫" /> + {errorCodes[errorCode]} + </div> + {/if} + <form method="post" action="/verify"> + <input placeholder="Username or UUID" name="ign" required /> + <input type="submit" value="Enter" /> + </form> +</main> + +<style> + p { + margin: 0; + } + .error { + font-weight: bold; + margin: 1em 0; + } +</style> |
