From dbc68d9c872f55c397427385d60c105b64cc94f2 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Fri, 8 Jul 2022 00:51:15 +1000 Subject: [PATCH] Oauth login now copies avatar --- slowcord/login/src/index.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/slowcord/login/src/index.ts b/slowcord/login/src/index.ts index 45fbea7d..68234e83 100644 --- a/slowcord/login/src/index.ts +++ b/slowcord/login/src/index.ts @@ -1,7 +1,8 @@ import "dotenv/config"; import express, { Request, Response } from "express"; import cookieParser from "cookie-parser"; -import { initDatabase, generateToken, User, Config } from "@fosscord/util"; +import * as util from "@fosscord/util"; +const { initDatabase, generateToken, User, Config, handleFile } = util; import path from "path"; import fetch from "node-fetch"; @@ -25,6 +26,13 @@ setInterval(() => { requestsThisSecond = 0; }, 1000); +const toDataURL = async (url: string) => { + const response = await fetch(url); + const blob = await response.blob(); + const buffer = Buffer.from(await blob.text()); + return `data:${blob.type};base64,${buffer.toString("base64")}`; +} + class Discord { static getAccessToken = async (req: Request, res: Response) => { const { code } = req.query; @@ -71,6 +79,7 @@ class Discord { id: json.id, email: json.email, username: json.username, + avatar_url: json.avatar ? `https://cdn.discordapp.com/avatars/${json.id}/${json.avatar}?size=2048` : null, }; }; } @@ -115,6 +124,12 @@ app.get("/oauth/:type", async (req, res) => { username: details.username, req }); + + if (details.avatar_url) { + const avatar = await handleFile(`/avatars/${user.id}`, await toDataURL(details.avatar_url) as string); + user.avatar = avatar; + await user.save(); + } } const token = await generateToken(user.id);