add api route for querying user information

This commit is contained in:
Julian 2023-05-10 18:29:11 -04:00
parent 33e680a43f
commit 4ce2931348
Signed by: NotNite
GPG Key ID: BD91A5402CCEB08A
2 changed files with 42 additions and 0 deletions

1
environment.d.ts vendored
View File

@ -27,6 +27,7 @@ declare global {
GITHUB_ORG: string;
BASE_DOMAIN: string;
API_TOKEN?: string;
}
}
}

View File

@ -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
})
);
}