From 3c78385354af3e755ed1af58c2023fc145068ed8 Mon Sep 17 00:00:00 2001 From: Jeremy Danyow Date: Mon, 30 Nov 2020 20:51:33 -0800 Subject: [PATCH] store session in host page's localStorage #123 #148 --- src/client.ts | 9 +++++---- src/oauth.ts | 14 +++++++++++++- src/page-attributes.ts | 8 ++------ src/utterances-api.ts | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/client.ts b/src/client.ts index b2d6efcb..db080c62 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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) { @@ -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 // for easy overriding. diff --git a/src/oauth.ts b/src/oauth.ts index 19b2fa2d..21aa8a4f 100644 --- a/src/oauth.ts +++ b/src/oauth.ts @@ -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 }; @@ -12,8 +13,19 @@ export async function loadToken(): Promise { 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; diff --git a/src/page-attributes.ts b/src/page-attributes.ts index 3823b40c..5a210dd2 100644 --- a/src/page-attributes.ts +++ b/src/page-attributes.ts @@ -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)); @@ -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], @@ -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 }; } diff --git a/src/utterances-api.ts b/src/utterances-api.ts index 7367c78d..969be33e 100644 --- a/src/utterances-api.ts +++ b/src/utterances-api.ts @@ -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';