Skip to content

Commit

Permalink
Merge pull request #5 from MiraiSubject/module-user-eligibility
Browse files Browse the repository at this point in the history
Move eligibility check into the configuration module
  • Loading branch information
MiraiSubject authored Apr 1, 2023
2 parents c2551cf + e8f336e commit 9ee4cb8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/typecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 3 additions & 17 deletions apps/webstack/src/routes/auth/osu/callback/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"dev": "dotenv -- turbo dev",
"lint": "turbo run lint",
"build": "turbo run build",
"check": "turbo run check",
"format": "prettier --write \"**/*.{ts,tsx,md}\""
},
"packageManager": "pnpm@7.29.0",
Expand Down
17 changes: 17 additions & 0 deletions packages/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { OsuUser } from "../../apps/webstack/src/lib/OsuUser";
import type { ITournamentConfig } from "./config.interface";
import { DateTime } from "luxon";

export const config: ITournamentConfig = {
"host": "Example Host",
Expand All @@ -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;
}
6 changes: 5 additions & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 6 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9ee4cb8

Please sign in to comment.