126 lines
3.4 KiB
TypeScript
126 lines
3.4 KiB
TypeScript
import Fastify from 'fastify'
|
|
import {PrismaClient} from '@prisma/client'
|
|
import fastifyMultipart from "@fastify/multipart";
|
|
import fastifyOauth2 from "@fastify/oauth2";
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
import dotenv from 'dotenv';
|
|
import {fileURLToPath} from 'url';
|
|
import {checkadmin, checkAuthenticatedDiscordToken} from "./middleware.ts";
|
|
import {getUser} from "./authHelper.ts";
|
|
|
|
dotenv.config();
|
|
|
|
|
|
if (!process.env.ADMINS) {
|
|
console.error("no admin account found");
|
|
process.exit(1);
|
|
}
|
|
|
|
if (!process.env.ORIGIN) {
|
|
console.error("no origin found");
|
|
process.exit(1);
|
|
}
|
|
|
|
const FILESIZE = process.env.FILESIZE_LIMIT
|
|
if (!FILESIZE) {
|
|
console.error("no file size detected");
|
|
process.exit(1);
|
|
} else if (isNaN(Number(FILESIZE)) && FILESIZE.trim() !== '') {
|
|
console.error("file size cannot be parsed.");
|
|
process.exit(1);
|
|
}
|
|
|
|
const PORT = process.env.PORT
|
|
if (!PORT) {
|
|
console.error("no port detected");
|
|
process.exit(1);
|
|
} else if (isNaN(Number(PORT)) && FILESIZE.trim() !== '') {
|
|
console.error("port cannot be parsed.");
|
|
process.exit(1);
|
|
}
|
|
|
|
|
|
export const __filename = fileURLToPath(import.meta.url);
|
|
export const __dirname = path.dirname(__filename);
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
|
|
const fastify = Fastify({logger: true})
|
|
|
|
|
|
fastify.register(fastifyMultipart)
|
|
fastify.register(fastifyOauth2, {
|
|
name: 'discordOAuth2',
|
|
credentials: {
|
|
client: {
|
|
id: process.env.DISCORD_CLIENT,
|
|
secret: process.env.DISCORD_SECRET,
|
|
},
|
|
auth: fastifyOauth2.DISCORD_CONFIGURATION
|
|
},
|
|
scope: ["identify"],
|
|
startRedirectPath: '/login',
|
|
callbackUri: req => `${process.env.ORIGIN}/login/callback`,
|
|
})
|
|
|
|
fastify.get('/login/callback', async function (request, reply) {
|
|
// @ts-ignore
|
|
const {token} = await this.discordOAuth2?.getAccessTokenFromAuthorizationCodeFlow(request)
|
|
//this is funny
|
|
const discordAccount = await getUser(token.access_token)
|
|
const test = await prisma.whitelistedUsers.findUnique({
|
|
where: {discordId: discordAccount.id},
|
|
});
|
|
|
|
console.log(token)
|
|
|
|
if (!test) {
|
|
return reply.status(401).send("Not Authorized");
|
|
}
|
|
|
|
// @ts-ignore
|
|
const existingUser = await prisma.user.findUnique({
|
|
where: {id: discordAccount.id},
|
|
});
|
|
console.log(existingUser)
|
|
// @ts-ignore
|
|
const {token: refreshToken} = await this.discordOAuth2?.getNewAccessTokenUsingRefreshToken(token)
|
|
if (!existingUser) {
|
|
await prisma.user.create({
|
|
data: {
|
|
id: discordAccount.id,
|
|
discordToken: refreshToken.access_token,
|
|
},
|
|
});
|
|
|
|
} else {
|
|
prisma.user.update({
|
|
where: {id: discordAccount.id},
|
|
data: {
|
|
discordToken: refreshToken.access_token,
|
|
},
|
|
})
|
|
}
|
|
reply.setCookie('access_token', refreshToken.discordToken);
|
|
return reply.send({access_token: refreshToken.access_token});
|
|
});
|
|
|
|
for (const file of fs.readdirSync(path.resolve(__dirname, "endpoints"))) {
|
|
const endpoint = await import(path.resolve(__dirname, "endpoints", file));
|
|
endpoint.default(fastify, prisma);
|
|
}
|
|
|
|
fastify.get('/', async (request, reply) => {
|
|
return reply.status(200).send('ok')
|
|
})
|
|
|
|
|
|
fastify.listen({port: Number(PORT)}, (err, address) => {
|
|
if (err) {
|
|
fastify.log.error(err)
|
|
process.exit(1)
|
|
}
|
|
fastify.log.info(`Server running at ${address}`)
|
|
})
|