Skip to content

Commit

Permalink
store session in host page's localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
jdanyow committed Dec 1, 2020
1 parent 7ab9cd9 commit 3c78385
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { param, deparam } from './deparam';
import { ResizeMessage } from './measure';
import { preferredThemeId, preferredTheme } from './preferred-theme';

// slice access token from query string
// slice session from query string
const params = deparam(location.search.substr(1));
const token = params.utterances;
if (token) {
const session = params.utterances;
if (session) {
localStorage.setItem('utterances-session', session);
delete params.utterances;
let search = param(params);
if (search.length) {
Expand Down Expand Up @@ -46,7 +47,7 @@ if (len > 1000) {
}
const ogtitleMeta = document.querySelector(`meta[property='og:title'],meta[name='og:title']`) as HTMLMetaElement;
attrs['og:title'] = ogtitleMeta ? ogtitleMeta.content : '';
attrs.token = token;
attrs.session = session || localStorage.getItem('utterances-session') || '';

// create the standard utterances styles and insert them at the beginning of the
// <head> for easy overriding.
Expand Down
14 changes: 13 additions & 1 deletion src/oauth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UTTERANCES_API } from './utterances-api';
import { param } from './deparam';
import { pageAttributes } from './page-attributes';

export const token = { value: null as null | string };

Expand All @@ -12,8 +13,19 @@ export async function loadToken(): Promise<string | null> {
if (token.value) {
return token.value;
}
if (!pageAttributes.session) {
return null;
}
const url = `${UTTERANCES_API}/token`;
const response = await fetch(url, { method: 'POST', mode: 'cors', credentials: 'include' });
const response = await fetch(url, {
method: 'POST',
mode: 'cors',
credentials: 'include',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(pageAttributes.session)
});
if (response.ok) {
const t = await response.json();
token.value = t;
Expand Down
8 changes: 2 additions & 6 deletions src/page-attributes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { deparam } from './deparam';
import repoRegex from './repo-regex';
import { token } from './oauth';

function readPageAttributes() {
const params = deparam(location.search.substr(1));
Expand Down Expand Up @@ -42,10 +41,6 @@ function readPageAttributes() {
throw new Error(`Invalid repo: "${params.repo}"`);
}

if (params.token) {
token.value = params.token;
}

return {
owner: matches[1],
repo: matches[2],
Expand All @@ -56,7 +51,8 @@ function readPageAttributes() {
title: params.title,
description: params.description,
label: params.label,
theme: params.theme || 'github-light'
theme: params.theme || 'github-light',
session: params.session
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/utterances-api.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// export const UTTERANCES_API = 'http://localhost:5000';
// export const UTTERANCES_API = 'http://localhost:7000';
export const UTTERANCES_API = 'https://api.utteranc.es';

0 comments on commit 3c78385

Please sign in to comment.