aboutsummaryrefslogtreecommitdiff
path: root/src/routes/verify/+page.server.ts
blob: bd3ea7fbbf3429e32b798bd29bbcff590e67453f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { fetchApi } from '$lib/api'
import type { AccountSchema, CleanUser, SessionSchema } from '$lib/APITypes'
import { redirect, type RequestHandler, type ServerLoad } from '@sveltejs/kit'
import env from '$lib/env'
import type { Actions } from './$types'


export const actions: Actions = {
	default: async ({ platform, locals, request }) => {
		const key = env(platform).SKYBLOCK_STATS_API_KEY
		if (!key) {
			throw redirect(303, `/verify?error=NO_KEY`)
		}
		if (locals.sid === undefined) {
			throw redirect(303, '/login')
		}

		const form = await request.formData()

		// username or uuid
		const playerIdentifier = form.get('ign')
		if (!playerIdentifier) {
			throw 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)
			throw redirect(303, '/login')

		const hypixelDiscordName = playerResponse.player?.socials.discord

		if (!hypixelDiscordName)
			throw 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))
			throw redirect(303, `/verify?error=WRONG_NAME&current=${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())

		throw redirect(303, '/profile')
	}
}

export const load = (({ locals }) => {
	return {
		sid: locals.sid
	}
}) satisfies ServerLoad