Modrinth OAuth support
This commit is contained in:
parent
b72301bea2
commit
52e28067d8
10 changed files with 125 additions and 66 deletions
41
frontend/src/shared/index.js
Normal file
41
frontend/src/shared/index.js
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
const OAUTH_KEY = 'modrinth-oauth-token';
|
||||
|
||||
/**
|
||||
* Creates a modrinth URI with a given URL encoded callback
|
||||
* @param {string | null} curseforgeSlug
|
||||
* @param {string | null} modrinthId
|
||||
* @returns {string} The callback URI
|
||||
*/
|
||||
const CreateModrinthUri = (curseforgeSlug, modrinthId) => {
|
||||
const uri = new URL(`${window.location.origin}${window.location.pathname}`);
|
||||
curseforgeSlug && curseforgeSlug.length && uri.searchParams.set('curseforgeSlug', curseforgeSlug);
|
||||
modrinthId && curseforgeSlug.length && uri.searchParams.set('modrinthId', modrinthId);
|
||||
return `https://modrinth.com/auth/authorize?client_id=50zijYLk&redirect_uri=${encodeURIComponent(uri.toString())}&scope=PROJECT_WRITE+VERSION_CREATE`;
|
||||
};
|
||||
|
||||
export const GetModrinthOauth = () => window.localStorage.getItem(OAUTH_KEY);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string | null} curseforgeSlug
|
||||
* @param {string | null} modrinthId
|
||||
* @returns
|
||||
*/
|
||||
export const DoModrinthOauth = (curseforgeSlug, modrinthId) => {
|
||||
const uri = CreateModrinthUri(curseforgeSlug, modrinthId);
|
||||
window.location.href = uri;
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles Modrinth's callback after OAuth is completed
|
||||
*/
|
||||
export const ModrinthOauthCallbackHandler = () => {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
if (!params.has('code')) return null;
|
||||
|
||||
window.localStorage.setItem(OAUTH_KEY, params.get('code'));
|
||||
params.delete('code');
|
||||
|
||||
const ret = Object.fromEntries(params);
|
||||
return Object.keys(ret).length ? ret : null;
|
||||
};
|
||||
Loading…
Reference in a new issue