Additional cleanup & Modrinth OAuth support

This commit is contained in:
oitsjustjose 2025-05-20 12:45:01 -05:00
commit 338a1a5791
3 changed files with 17 additions and 7 deletions

View file

@ -47,14 +47,15 @@ export default async (req: Request, res: Response) => {
body: new URLSearchParams({ body: new URLSearchParams({
code: req.body.clientOauthCode, code: req.body.clientOauthCode,
client_id: process.env.MODRINTH_OAUTH_CLIENT_ID, client_id: process.env.MODRINTH_OAUTH_CLIENT_ID,
redirect_uri: "https://ctm.oitsjustjose.com/", redirect_uri: req.body.redirectUri,
grant_type: "authorization_code", grant_type: "authorization_code",
}), }),
} }
); );
if (!response.ok) { if (!response.ok) {
return res.status(400).send(response); const body = await response.json();
return res.status(400).send(body);
} }
const body = (await response.json()) as OAuthTokenResponse; const body = (await response.json()) as OAuthTokenResponse;

View file

@ -22,7 +22,8 @@ export default () => {
/* /*
Begins the Modrinth OAuth flow, creating a URL with a callback that has the Begins the Modrinth OAuth flow, creating a URL with a callback that has the
data we need in the query params and will return back with the client's data we need in the query params and will return back with the client's
oauth code that will be needed back on the backend oauth code that will be needed back on the backend. When the OAuth call
succeeds, the callback will trigger the useEffect below to handle it.
*/ */
const startOauthFlow = (evt) => { const startOauthFlow = (evt) => {
evt && evt.preventDefault(); evt && evt.preventDefault();
@ -33,14 +34,16 @@ export default () => {
useEffect(() => { // Handle the Modrinth OAuth callback useEffect(() => { // Handle the Modrinth OAuth callback
const internalCreateJob = async (formDataIn) => { const internalCreateJob = async (formDataIn) => {
if (!formDataIn.code.length) return;
if (!formDataIn.curseforgeSlug.length) return; if (!formDataIn.curseforgeSlug.length) return;
if (!formDataIn.modrinthId.length) return; if (!formDataIn.modrinthId.length) return;
if (!formDataIn.code.length) return; if (!formDataIn.redirectUri.length) return;
const body = { const body = {
clientOauthCode: formDataIn.code,
curseforgeSlug: formDataIn.curseforgeSlug, curseforgeSlug: formDataIn.curseforgeSlug,
modrinthId: formDataIn.modrinthId, modrinthId: formDataIn.modrinthId,
clientOauthCode: formDataIn.code, redirectUri: formDataIn.redirectUri,
}; };
const resp = await fetch('/api/v1/jobs', { const resp = await fetch('/api/v1/jobs', {

View file

@ -7,7 +7,6 @@ const MODRINTH_CLIENT_ID = '50zijYLk';
* @returns {string} The callback URI * @returns {string} The callback URI
*/ */
export const CreateModrinthUri = (curseforgeSlug, modrinthId) => { export const CreateModrinthUri = (curseforgeSlug, modrinthId) => {
console.log(process.env);
const uri = new URL(`${window.location.origin}${window.location.pathname}`); const uri = new URL(`${window.location.origin}${window.location.pathname}`);
uri.searchParams.set('curseforgeSlug', curseforgeSlug); uri.searchParams.set('curseforgeSlug', curseforgeSlug);
uri.searchParams.set('modrinthId', modrinthId); uri.searchParams.set('modrinthId', modrinthId);
@ -21,5 +20,12 @@ export const ModrinthOauthCallbackHandler = () => {
const params = new URLSearchParams(window.location.search); const params = new URLSearchParams(window.location.search);
if (!params.has('code')) return null; if (!params.has('code')) return null;
const ret = Object.fromEntries(params); const ret = Object.fromEntries(params);
return Object.keys(ret).length ? ret : null;
if (!Object.keys(ret).length) return null;
const redirectUri = new URL(`${window.location.origin}${window.location.pathname}`);
redirectUri.searchParams.set('curseforgeSlug', params.get('curseforgeSlug'));
redirectUri.searchParams.set('modrinthId', params.get('modrinthId'));
return { ...ret, redirectUri: redirectUri.toString() };
}; };