Skip to content

Commit

Permalink
refactor: allow multiple domain names
Browse files Browse the repository at this point in the history
  • Loading branch information
amatiasq committed Aug 4, 2022
1 parent fa94bf9 commit 72cca62
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/3-github/GHRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { POST } from '../1-core/http';
import { COMMIT_ENDPOINT } from '../config.json';
import { ghCommitEndpoint } from './gh-utils';
import { GithubToken } from './GithubAuth';
import { GithubGraphQlApi } from './GithubGraphQlApi';
import { GithubRestApi, MediaType } from './GithubRestApi';
Expand Down Expand Up @@ -160,7 +160,7 @@ export class GHRepository {

this.commiting = true;

return POST<void>(COMMIT_ENDPOINT, body, { keepalive: isUrgent }).finally(
return POST<void>(ghCommitEndpoint, body, { keepalive: isUrgent }).finally(
() => (this.commiting = false),
);
}
Expand Down
33 changes: 32 additions & 1 deletion src/3-github/gh-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { getQueryParameter } from '../0-dom/getQueryParameter';
import { Note } from '../2-entities/Note';
import {
AUTH_ENDPOINT,
CLIENT_ID_DEV,
CLIENT_ID_PROD,
COMMIT_ENDPOINT,
GH_SCOPE,
VALID_ORIGINS,
} from '../config.json';
import { GithubToken } from './GithubAuth';
import { GithubUsername } from './models/GHApiUser';

const GH_API = 'https://api.github.com';

export const ghRepository = getQueryParameter('repo', 'pensieve-data');
Expand All @@ -20,3 +27,27 @@ export function ghAuthHeaders(token: GithubToken) {
export function ghPublicPage(username: GithubUsername, note: Note) {
return `https://github.com/${username}/${ghRepository}/blob/main/note/${note.id}`;
}

const isLocalHost = location.hostname === 'localhost';

export const appOrigin = getOrigin();
export const ghScope = GH_SCOPE;
export const ghClientId = isLocalHost ? CLIENT_ID_DEV : CLIENT_ID_PROD;

const endpointOrigin = isLocalHost ? VALID_ORIGINS[0] : appOrigin;

export const ghAuthEndpoint = `${endpointOrigin}${AUTH_ENDPOINT}`;
export const ghCommitEndpoint = `${endpointOrigin}${COMMIT_ENDPOINT}`;

function getOrigin() {
const { origin } = location;

if (
!isLocalHost ||
!VALID_ORIGINS.some((x: string) => origin.startsWith(x))
) {
throw new Error(`Invalid origin: ${origin}`);
}

return origin;
}
23 changes: 10 additions & 13 deletions src/5-app/useGithubAuth.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { ClientStorage } from '@amatiasq/client-storage';
import { useState } from 'react';
import { parseParams } from '../1-core/url';
import {
appOrigin,
ghAuthEndpoint,
ghClientId,
ghScope,
} from '../3-github/gh-utils';
import { GithubAuth, GithubToken } from '../3-github/GithubAuth';
import { GithubUsers } from '../3-github/GithubUsers';
import { GithubUsername } from '../3-github/models/GHApiUser';
import {
APP_ROOT,
AUTH_ENDPOINT,
CLIENT_ID_DEV,
CLIENT_ID_PROD,
GH_SCOPE,
} from '../config.json';

const isLocalHost = location.hostname === 'localhost';

const auth = new GithubAuth({
scope: GH_SCOPE,
endpoint: AUTH_ENDPOINT,
clientId: isLocalHost ? CLIENT_ID_DEV : CLIENT_ID_PROD,
redirectUri: isLocalHost ? location.origin : APP_ROOT,
scope: ghScope,
endpoint: ghAuthEndpoint,
clientId: ghClientId,
redirectUri: appOrigin,
});

const user = new ClientStorage<GithubUsername | null>('notes.gh-user', {
Expand Down
8 changes: 4 additions & 4 deletions src/config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"GH_SCOPE": "repo gist",
"GH_API": "https://api.github.com",
"AUTH_ENDPOINT": "https://pensieve.amatiasq.com/auth",
"COMMIT_ENDPOINT": "https://pensieve.amatiasq.com/commit",
"APP_ROOT": "https://pensieve.amatiasq.com/",
"AUTH_ENDPOINT": "/auth",
"COMMIT_ENDPOINT": "/commit",
"CLIENT_ID_DEV": "c55d2c46c215f9a4c3cb",
"CLIENT_ID_PROD": "09580e62d66243b6b09d"
"CLIENT_ID_PROD": "09580e62d66243b6b09d",
"VALID_ORIGINS": ["https://pensieve.amatiasq.com"]
}
4 changes: 2 additions & 2 deletions src/manifest.webmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"display": "standalone",
"background_color": "#1c1c1c",
"theme_color": "#1c1c1c",
"start_url": "https://pensieve.amatiasq.com/",
"scope": "https://pensieve.amatiasq.com/",
"start_url": "../",
"scope": "/",
"icons": [
{
"src": "/assets/manifest-icon-192.png",
Expand Down

0 comments on commit 72cca62

Please sign in to comment.