diff options
-rw-r--r-- | slowcord/login/src/index.ts | 17 |
1 files changed, 16 insertions, 1 deletions
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); |