From 5bc3e396ea59e4881b420c850dcc146a39c49d07 Mon Sep 17 00:00:00 2001 From: Shivam Date: Sat, 1 Apr 2023 23:40:07 +0200 Subject: [PATCH 1/3] Move eligibility check into the configuration module --- .../src/routes/auth/osu/callback/+server.ts | 20 +++---------------- packages/config/config.ts | 17 ++++++++++++++++ packages/config/package.json | 6 +++++- pnpm-lock.yaml | 7 ++++++- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/apps/webstack/src/routes/auth/osu/callback/+server.ts b/apps/webstack/src/routes/auth/osu/callback/+server.ts index 8bfe4ee..2a20e52 100644 --- a/apps/webstack/src/routes/auth/osu/callback/+server.ts +++ b/apps/webstack/src/routes/auth/osu/callback/+server.ts @@ -3,6 +3,7 @@ import { env as pubEnv } from '$env/dynamic/public'; import { redirect } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { DateTime } from "luxon"; +import { isUserEligible } from 'config'; import type { OsuUser } from '$lib/OsuUser'; async function getOAuthTokens(code: string) { @@ -48,32 +49,17 @@ async function getUserData(tokens: { } } -/** - * @description This function is used to check if the user is eligible for the verified role(s) specfied in your config. - * The default is to check if the user is older than 6 months. - * You can modify the function to check for other things, such as if the user has a certain amount of playtime, rank or pp. - * @param userData The osu! profile of the user. - * @returns A boolean indicating if the user is eligible for the linked role. - * -*/ -function isUserEligible(userData: OsuUser): boolean { - const joinDate = DateTime.fromISO(userData.join_date); - const nowMinus6Months = DateTime.now().minus({ months: 6 }); - - return nowMinus6Months > joinDate; -} - // Write cookie for the state which will be used to compare later for the linked role stuff. export const GET = (async ({ url, locals }) => { try { const code = url.searchParams.get('code'); if (!code) throw new Error('No code provided'); const tokens = await getOAuthTokens(code); - const meData = await getUserData(tokens); + const meData = await getUserData(tokens) as OsuUser; await locals.session.set({ osu: { - id: meData.id, + id: meData.id.toString(), username: meData.username, joinDate: DateTime.fromISO(meData.join_date) } diff --git a/packages/config/config.ts b/packages/config/config.ts index ebf6bb3..e50cc9d 100644 --- a/packages/config/config.ts +++ b/packages/config/config.ts @@ -1,4 +1,6 @@ +import type { OsuUser } from "$lib/OsuUser"; import type { ITournamentConfig } from "./config.interface"; +import { DateTime } from "luxon"; export const config: ITournamentConfig = { "host": "Example Host", @@ -15,3 +17,18 @@ export const config: ITournamentConfig = { ] } } + +/** + * @description This function is used to check if the user is eligible for the verified role(s) specfied in your config. + * The default is to check if the user is older than 6 months. + * You can modify the function to check for other things, such as if the user has a certain amount of playtime, rank or pp. + * @param userData The osu! profile of the user. + * @returns A boolean indicating if the user is eligible for the linked role. + * +*/ +export function isUserEligible(userData: OsuUser): boolean { + const joinDate = DateTime.fromISO(userData.join_date); + const nowMinus6Months = DateTime.now().minus({ months: 6 }); + + return nowMinus6Months > joinDate; +} \ No newline at end of file diff --git a/packages/config/package.json b/packages/config/package.json index 0b2d2f6..f643397 100644 --- a/packages/config/package.json +++ b/packages/config/package.json @@ -1,5 +1,9 @@ { "name": "config", "version": "0.0.0", - "main": "index.ts" + "main": "index.ts", + "dependencies": { + "@types/luxon": "^3.2.0", + "luxon": "^3.3.0" + } } \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cd56776..9232940 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -80,7 +80,12 @@ importers: vitest: 0.25.8 packages/config: - specifiers: {} + specifiers: + '@types/luxon': ^3.2.0 + luxon: ^3.3.0 + dependencies: + '@types/luxon': 3.2.0 + luxon: 3.3.0 packages: From 75810cf4a8c0c4abecd1be124da12a6fb1d558f8 Mon Sep 17 00:00:00 2001 From: Shivam Date: Sun, 2 Apr 2023 00:10:27 +0200 Subject: [PATCH 2/3] Add turbo to the install procedure in the workflow --- .github/workflows/typecheck.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/typecheck.yml b/.github/workflows/typecheck.yml index 7e5d4de..5de85d3 100644 --- a/.github/workflows/typecheck.yml +++ b/.github/workflows/typecheck.yml @@ -19,6 +19,6 @@ jobs: node-version: ${{ matrix.node-version }} cache: 'pnpm' - name: Install dependencies - run: pnpm install + run: pnpm add --global turbo && pnpm install - name: Typecheck the webservice run: pnpm check From e8f336e1b701f099588f6ad401c918b68dbe2c5b Mon Sep 17 00:00:00 2001 From: Shivam Date: Sun, 2 Apr 2023 00:13:07 +0200 Subject: [PATCH 3/3] Correct package type --- packages/config/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/config/config.ts b/packages/config/config.ts index e50cc9d..ede7423 100644 --- a/packages/config/config.ts +++ b/packages/config/config.ts @@ -1,4 +1,4 @@ -import type { OsuUser } from "$lib/OsuUser"; +import type { OsuUser } from "../../apps/webstack/src/lib/OsuUser"; import type { ITournamentConfig } from "./config.interface"; import { DateTime } from "luxon";