Skip to content

Commit

Permalink
Merge pull request #5 from Sammy-T/backend
Browse files Browse the repository at this point in the history
Delete current token before install
  • Loading branch information
Sammy-T authored Jul 3, 2024
2 parents 131da5b + f88a13e commit 4c5b3d1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions cms/src/lib/backends/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand All @@ -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');
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 4c5b3d1

Please sign in to comment.