From 4ce293134845accabc3ca5663acf4596635ef6fc Mon Sep 17 00:00:00 2001 From: NotNite Date: Wed, 10 May 2023 18:29:11 -0400 Subject: [PATCH] add api route for querying user information --- environment.d.ts | 1 + src/app/api/user/[username]/route.ts | 41 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/app/api/user/[username]/route.ts diff --git a/environment.d.ts b/environment.d.ts index 000a0b4..c575c8b 100644 --- a/environment.d.ts +++ b/environment.d.ts @@ -27,6 +27,7 @@ declare global { GITHUB_ORG: string; BASE_DOMAIN: string; + API_TOKEN?: string; } } } diff --git a/src/app/api/user/[username]/route.ts b/src/app/api/user/[username]/route.ts new file mode 100644 index 0000000..136fc93 --- /dev/null +++ b/src/app/api/user/[username]/route.ts @@ -0,0 +1,41 @@ +import { NextRequest } from "next/server"; +import prisma from "@/prisma"; +import * as ldap from "@/ldap"; + +export const dynamic = "force-dynamic"; + +export async function GET( + request: NextRequest, + { params }: { params: { username: string } } +) { + const { username } = params; + + if ( + process.env.API_TOKEN == null || + process.env.API_TOKEN !== request.headers.get("Authorization") + ) { + return new Response(null, { status: 401 }); + } + + const user = await prisma.user.findUnique({ + where: { username: username as string } + }); + + if (user == null) { + return new Response(null, { status: 404 }); + } + + const ldapUser = await ldap.getUserInfo(user); + if (ldapUser == null) { + return new Response(null, { status: 404 }); + } + + return new Response( + JSON.stringify({ + ...ldapUser, + avatar: ldapUser.avatar ?? null, + discordId: ldapUser.discordId ?? null, + githubId: ldapUser.githubId ?? null + }) + ); +}