use discord avatar as fallback

This commit is contained in:
Julian 2023-04-25 21:23:55 -04:00
parent 9c65c76026
commit 234518a62e
Signed by untrusted user: NotNite
GPG key ID: BD91A5402CCEB08A
3 changed files with 31 additions and 1 deletions

View file

@ -1,6 +1,7 @@
import * as ldap from "@/ldap";
import prisma from "@/prisma";
import { getUserFromRequest } from "@/auth";
import { getDiscordAvatar } from "@/app/oauth/discord/oauth";
type RequestBody = {
username: string;
@ -67,6 +68,16 @@ export async function POST(request: Request) {
}
}
const discordAuth = await prisma.discordAuth.findFirst({
where: {
userId: user.id
}
});
if (discordAuth !== null && avatarBuf === undefined) {
avatarBuf = await getDiscordAvatar(discordAuth.accessToken);
}
const users = await ldap.getUsers();
for (const user of users) {
if (user.id.toLowerCase() === username.toLowerCase()) {

View file

@ -10,6 +10,7 @@ export type DiscordAccessTokenResponse = {
export type DiscordUserResponse = {
id: string;
avatar: string;
};
export type DiscordGuildResponse = {
@ -39,3 +40,18 @@ export async function getDiscordGuilds(token: string) {
const res: DiscordGuildResponse[] = await req.json();
return res.map((guild) => guild.id);
}
export async function getDiscordAvatar(token: string) {
const req = await fetch("https://discord.com/api/users/@me", {
headers: {
Authorization: `Bearer ${token}`
}
});
const res: DiscordUserResponse = await req.json();
const file = `https://cdn.discordapp.com/avatars/${res.id}/${res.avatar}.png`;
const avatarReq = await fetch(file);
const avatarBuffer = await avatarReq.arrayBuffer();
return Buffer.from(avatarBuffer);
}

View file

@ -184,7 +184,10 @@ export default function RegisterForm({ ticket }: { ticket: string }) {
/>
<Input
hint="This image will automatically be used as your avatar with supported services. Optional - maximum 1 MB."
hint={
"This image will automatically be used as your avatar with supported services - maximum 1 MB. " +
"Will use the avatar of the service you signed up with if not provided."
}
type="file"
name="avatar"
label="Avatar"