store session in host page's localStorage

#123 #148
This commit is contained in:
Jeremy Danyow 2020-11-30 20:51:33 -08:00
parent 7ab9cd94d9
commit 3c78385354
No known key found for this signature in database
GPG Key ID: 50404A1CEB6B5250
4 changed files with 21 additions and 12 deletions

View File

@ -2,10 +2,11 @@ import { param, deparam } from './deparam';
import { ResizeMessage } from './measure'; import { ResizeMessage } from './measure';
import { preferredThemeId, preferredTheme } from './preferred-theme'; 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 params = deparam(location.search.substr(1));
const token = params.utterances; const session = params.utterances;
if (token) { if (session) {
localStorage.setItem('utterances-session', session);
delete params.utterances; delete params.utterances;
let search = param(params); let search = param(params);
if (search.length) { if (search.length) {
@ -46,7 +47,7 @@ if (len > 1000) {
} }
const ogtitleMeta = document.querySelector(`meta[property='og:title'],meta[name='og:title']`) as HTMLMetaElement; const ogtitleMeta = document.querySelector(`meta[property='og:title'],meta[name='og:title']`) as HTMLMetaElement;
attrs['og:title'] = ogtitleMeta ? ogtitleMeta.content : ''; 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 // create the standard utterances styles and insert them at the beginning of the
// <head> for easy overriding. // <head> for easy overriding.

View File

@ -1,5 +1,6 @@
import { UTTERANCES_API } from './utterances-api'; import { UTTERANCES_API } from './utterances-api';
import { param } from './deparam'; import { param } from './deparam';
import { pageAttributes } from './page-attributes';
export const token = { value: null as null | string }; export const token = { value: null as null | string };
@ -12,8 +13,19 @@ export async function loadToken(): Promise<string | null> {
if (token.value) { if (token.value) {
return token.value; return token.value;
} }
if (!pageAttributes.session) {
return null;
}
const url = `${UTTERANCES_API}/token`; 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) { if (response.ok) {
const t = await response.json(); const t = await response.json();
token.value = t; token.value = t;

View File

@ -1,6 +1,5 @@
import { deparam } from './deparam'; import { deparam } from './deparam';
import repoRegex from './repo-regex'; import repoRegex from './repo-regex';
import { token } from './oauth';
function readPageAttributes() { function readPageAttributes() {
const params = deparam(location.search.substr(1)); const params = deparam(location.search.substr(1));
@ -42,10 +41,6 @@ function readPageAttributes() {
throw new Error(`Invalid repo: "${params.repo}"`); throw new Error(`Invalid repo: "${params.repo}"`);
} }
if (params.token) {
token.value = params.token;
}
return { return {
owner: matches[1], owner: matches[1],
repo: matches[2], repo: matches[2],
@ -56,7 +51,8 @@ function readPageAttributes() {
title: params.title, title: params.title,
description: params.description, description: params.description,
label: params.label, label: params.label,
theme: params.theme || 'github-light' theme: params.theme || 'github-light',
session: params.session
}; };
} }

View File

@ -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'; export const UTTERANCES_API = 'https://api.utteranc.es';