1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-06 02:42:37 +01:00

Oauth login now copies avatar

This commit is contained in:
Madeline 2022-07-08 00:51:15 +10:00
parent e06891d291
commit dbc68d9c87
No known key found for this signature in database
GPG Key ID: 1958E017C36F2E47

View File

@ -1,7 +1,8 @@
import "dotenv/config"; import "dotenv/config";
import express, { Request, Response } from "express"; import express, { Request, Response } from "express";
import cookieParser from "cookie-parser"; 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 path from "path";
import fetch from "node-fetch"; import fetch from "node-fetch";
@ -25,6 +26,13 @@ setInterval(() => {
requestsThisSecond = 0; requestsThisSecond = 0;
}, 1000); }, 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 { class Discord {
static getAccessToken = async (req: Request, res: Response) => { static getAccessToken = async (req: Request, res: Response) => {
const { code } = req.query; const { code } = req.query;
@ -71,6 +79,7 @@ class Discord {
id: json.id, id: json.id,
email: json.email, email: json.email,
username: json.username, 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, username: details.username,
req 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); const token = await generateToken(user.id);