More Cleanup & Fixes
This commit is contained in:
parent
1b21b0e3cf
commit
6ad5f703db
6 changed files with 77 additions and 78 deletions
|
|
@ -3,6 +3,12 @@ import Job from "../../models/Job";
|
|||
import Status from "../../status";
|
||||
import { Fernet } from "fernet-nodejs";
|
||||
|
||||
type OAuthTokenResponse = {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
expires_in: number;
|
||||
};
|
||||
|
||||
const getNextQueuePlace = async () => {
|
||||
const allActiveJobs = await Job.find({ status: Status.ENQUEUED }).sort({
|
||||
queuePlace: "desc",
|
||||
|
|
@ -16,7 +22,7 @@ const getNextQueuePlace = async () => {
|
|||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
if (
|
||||
!req.body.oauthToken ||
|
||||
!req.body.clientOauthCode ||
|
||||
!req.body.curseforgeSlug ||
|
||||
!req.body.modrinthId
|
||||
) {
|
||||
|
|
@ -25,15 +31,37 @@ export default async (req: Request, res: Response) => {
|
|||
.send(
|
||||
[
|
||||
"Missing a required parameter in the JSON body",
|
||||
"Required params: oauthToken: string, curseforgeSlug: string, modrinthId: string",
|
||||
"Required params: clientOauthCode: string, curseforgeSlug: string, modrinthId: string",
|
||||
].join(" ")
|
||||
);
|
||||
}
|
||||
|
||||
const fern = new Fernet(process.env.SECRET);
|
||||
const response = await fetch(
|
||||
"https://api.modrinth.com/_internal/oauth/token",
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Authorization: process.env.MODRINTH_OAUTH_CLIENT_SECRET,
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
code: req.body.clientOauthCode,
|
||||
client_id: process.env.MODRINTH_OAUTH_CLIENT_ID,
|
||||
redirect_uri: "https://ctm.oitsjustjose.com/",
|
||||
grant_type: "authorization_code",
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
return res.status(400).send(response);
|
||||
}
|
||||
|
||||
const body = (await response.json()) as OAuthTokenResponse;
|
||||
|
||||
const fern = new Fernet(process.env.SECRET);
|
||||
const newJob = new Job({
|
||||
oauthToken: fern.encrypt(req.body.oauthToken),
|
||||
oauthToken: fern.encrypt(body.access_token),
|
||||
curseforgeSlug: req.body.curseforgeSlug,
|
||||
modrinthId: req.body.modrinthId,
|
||||
queuePlace: await getNextQueuePlace(),
|
||||
|
|
|
|||
Loading…
Reference in a new issue