diff options
author | mat <27899617+mat-1@users.noreply.github.com> | 2021-05-27 16:56:50 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-27 16:56:50 -0500 |
commit | c9097dc2d0749fa63fd731423bc87a5452f0444d (patch) | |
tree | 50afe6b99cf3ed6bd7369e804316c05c7407b5ca /src/index.ts | |
parent | a3662fb5888bd031b27cc81028ba86b0271eb442 (diff) | |
download | skyblock-api-c9097dc2d0749fa63fd731423bc87a5452f0444d.tar.gz skyblock-api-c9097dc2d0749fa63fd731423bc87a5452f0444d.tar.bz2 skyblock-api-c9097dc2d0749fa63fd731423bc87a5452f0444d.zip |
Profile customization (#45)
* add basic discord auth
* add uuid dependency
* add lastUpdated to sessions
* add route to get session from id
* add accounts collection
* update build
* add gm rank
* add `customization` url parameter
* add customization parameter to /player/:user
* add route to get info from discord id
* remove a console.log
* Update database.js
* Update package.json
* fix tests
Diffstat (limited to 'src/index.ts')
-rw-r--r-- | src/index.ts | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/src/index.ts b/src/index.ts index 1652428..3d6bf6f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,15 @@ -import { fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots } from './database' +import { createSession, fetchAccount, fetchAccountFromDiscord, fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots, fetchSession, updateAccount } from './database' import { fetchMemberProfile, fetchUser } from './hypixel' import rateLimit from 'express-rate-limit' import * as constants from './constants' +import * as discord from './discord' import express from 'express' const app = express() export const debug = false +const mainSiteUrl = 'http://localhost:8081' // 200 requests over 5 minutes const limiter = rateLimit({ @@ -22,6 +24,7 @@ const limiter = rateLimit({ }) app.use(limiter) +app.use(express.json()) app.use((req, res, next) => { res.setHeader('Access-Control-Allow-Origin', '*') next() @@ -35,14 +38,21 @@ app.get('/player/:user', async(req, res) => { res.json( await fetchUser( { user: req.params.user }, - [req.query.basic as string === 'true' ? undefined : 'profiles', 'player'] + [req.query.basic as string === 'true' ? undefined : 'profiles', 'player'], + req.query.customization as string === 'true' ) ) }) +app.get('/discord/:id', async(req, res) => { + res.json( + await fetchAccountFromDiscord(req.params.id) + ) +}) + app.get('/player/:user/:profile', async(req, res) => { res.json( - await fetchMemberProfile(req.params.user, req.params.profile) + await fetchMemberProfile(req.params.user, req.params.profile, req.query.customization as string === 'true') ) }) @@ -75,6 +85,45 @@ app.get('/constants', async(req, res) => { ) }) +app.post('/accounts/createsession', async(req, res) => { + try { + const { code } = req.body + const { access_token: accessToken, refresh_token: refreshToken } = await discord.exchangeCode(`${mainSiteUrl}/loggedin`, code) + if (!accessToken) + // access token is invalid :( + return res.json({ ok: false }) + const userData = await discord.getUser(accessToken) + const sessionId = await createSession(refreshToken, userData) + res.json({ ok: true, session_id: sessionId }) + } catch (err) { + res.json({ ok: false }) + } +}) + +app.post('/accounts/session', async(req, res) => { + try { + const { uuid } = req.body + const session = await fetchSession(uuid) + const account = await fetchAccountFromDiscord(session.discord_user.id) + res.json({ session, account }) + } catch (err) { + console.error(err) + res.json({ ok: false }) + } +}) + + +app.post('/accounts/update', async(req, res) => { + // it checks against the key, so it's kind of secure + if (req.headers.key !== process.env.key) return console.log('bad key!') + try { + await updateAccount(req.body.discordId, req.body) + res.json({ ok: true }) + } catch (err) { + console.error(err) + res.json({ ok: false }) + } +}) // only run the server if it's not doing tests if (!globalThis.isTest) |