Skip to content

Commit 3c78385

Browse files
committed
store session in host page's localStorage
#123 #148
1 parent 7ab9cd9 commit 3c78385

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

src/client.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { param, deparam } from './deparam';
22
import { ResizeMessage } from './measure';
33
import { preferredThemeId, preferredTheme } from './preferred-theme';
44

5-
// slice access token from query string
5+
// slice session from query string
66
const params = deparam(location.search.substr(1));
7-
const token = params.utterances;
8-
if (token) {
7+
const session = params.utterances;
8+
if (session) {
9+
localStorage.setItem('utterances-session', session);
910
delete params.utterances;
1011
let search = param(params);
1112
if (search.length) {
@@ -46,7 +47,7 @@ if (len > 1000) {
4647
}
4748
const ogtitleMeta = document.querySelector(`meta[property='og:title'],meta[name='og:title']`) as HTMLMetaElement;
4849
attrs['og:title'] = ogtitleMeta ? ogtitleMeta.content : '';
49-
attrs.token = token;
50+
attrs.session = session || localStorage.getItem('utterances-session') || '';
5051

5152
// create the standard utterances styles and insert them at the beginning of the
5253
// <head> for easy overriding.

src/oauth.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { UTTERANCES_API } from './utterances-api';
22
import { param } from './deparam';
3+
import { pageAttributes } from './page-attributes';
34

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

@@ -12,8 +13,19 @@ export async function loadToken(): Promise<string | null> {
1213
if (token.value) {
1314
return token.value;
1415
}
16+
if (!pageAttributes.session) {
17+
return null;
18+
}
1519
const url = `${UTTERANCES_API}/token`;
16-
const response = await fetch(url, { method: 'POST', mode: 'cors', credentials: 'include' });
20+
const response = await fetch(url, {
21+
method: 'POST',
22+
mode: 'cors',
23+
credentials: 'include',
24+
headers: {
25+
'content-type': 'application/json'
26+
},
27+
body: JSON.stringify(pageAttributes.session)
28+
});
1729
if (response.ok) {
1830
const t = await response.json();
1931
token.value = t;

src/page-attributes.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { deparam } from './deparam';
22
import repoRegex from './repo-regex';
3-
import { token } from './oauth';
43

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

45-
if (params.token) {
46-
token.value = params.token;
47-
}
48-
4944
return {
5045
owner: matches[1],
5146
repo: matches[2],
@@ -56,7 +51,8 @@ function readPageAttributes() {
5651
title: params.title,
5752
description: params.description,
5853
label: params.label,
59-
theme: params.theme || 'github-light'
54+
theme: params.theme || 'github-light',
55+
session: params.session
6056
};
6157
}
6258

src/utterances-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// export const UTTERANCES_API = 'http://localhost:5000';
1+
// export const UTTERANCES_API = 'http://localhost:7000';
22
export const UTTERANCES_API = 'https://api.utteranc.es';

0 commit comments

Comments
 (0)