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 { 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
// <head> for easy overriding.

View File

@ -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<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;

View File

@ -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
};
}

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';