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:
parent
e06891d291
commit
dbc68d9c87
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user