From f88a13ef528f105b09583988cbd0453a10b6a0be Mon Sep 17 00:00:00 2001 From: Sammy-T Date: Wed, 3 Jul 2024 04:51:22 -0400 Subject: [PATCH] Delete current token before install --- cms/src/lib/backends/github.js | 26 ++++++++++++++++++++++++-- package.json | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/cms/src/lib/backends/github.js b/cms/src/lib/backends/github.js index b7b6f7f..ecf170e 100644 --- a/cms/src/lib/backends/github.js +++ b/cms/src/lib/backends/github.js @@ -65,6 +65,8 @@ async function init() { const apiRoot = backendCfg.api_root ?? window.location.origin; const authEndpoint = backendCfg.auth_endpoint ?? '/api/github/oauth/token'; + const authTokenUrl = `${apiRoot}${authEndpoint}`; + const data = { code }; /** @type {RequestInit} */ @@ -77,8 +79,7 @@ async function init() { }; // Exchange code for token - const resp = await fetch(`${apiRoot}${authEndpoint}`, opts); - + const resp = await fetch(authTokenUrl, opts); const respJson = await resp.json(); if(!resp.ok) throw new Error(respJson.error ?? 'Token error'); @@ -97,6 +98,7 @@ async function init() { const appSlug = backendCfg.app_name.replaceAll(' ', '-').toLowerCase(); if(total <= 0) { + await deleteToken(authTokenUrl, authentication.token); installApp(appSlug); return; } @@ -105,6 +107,7 @@ async function init() { const installed = installations.find(installation => installation.app_slug === appSlug); if(!installed) { + await deleteToken(authTokenUrl, authentication.token); installApp(appSlug); return; } @@ -123,6 +126,25 @@ async function init() { return github; } +/** + * A helper to delete an auth token. + * @param {string} authTokenUrl + * @param {string} token + */ +async function deleteToken(authTokenUrl, token) { + /** @type {RequestInit} */ + const opts = { + method: 'delete', + headers: { + 'Authorization': `token ${token}` + } + }; + + const resp = await fetch(authTokenUrl, opts); + + if(!resp.ok) throw new Error('Delete token error'); +} + /** * A helper to redirect to the GitHub install page corresponding to the * specified app slug. diff --git a/package.json b/package.json index e0c0e60..8b7e040 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "snow-cms", - "version": "1.0.4", + "version": "1.1.0", "description": "A configurable CMS built with Svelte.", "keywords": [], "author": "Sammy-T",