mirror of
https://github.com/PeWu/topola-viewer.git
synced 2026-03-18 13:33:46 +00:00
WikiTree: store logged in user name in a cookie
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
import * as queryString from 'query-string';
|
import * as queryString from 'query-string';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import Cookies from 'js-cookie';
|
|
||||||
import debounce from 'debounce';
|
import debounce from 'debounce';
|
||||||
import md5 from 'md5';
|
import md5 from 'md5';
|
||||||
import {analyticsEvent} from './analytics';
|
import {analyticsEvent} from './analytics';
|
||||||
import {buildSearchIndex, SearchIndex} from './search_index';
|
import {buildSearchIndex, SearchIndex} from './search_index';
|
||||||
import {displaySearchResult} from './search_util';
|
import {displaySearchResult} from './search_util';
|
||||||
import {FormattedMessage, intlShape} from 'react-intl';
|
import {FormattedMessage, intlShape} from 'react-intl';
|
||||||
|
import {getLoggedInUserName} from './wikitree';
|
||||||
import {IndiInfo, JsonGedcomData} from 'topola';
|
import {IndiInfo, JsonGedcomData} from 'topola';
|
||||||
import {Link} from 'react-router-dom';
|
import {Link} from 'react-router-dom';
|
||||||
import {RouteComponentProps} from 'react-router-dom';
|
import {RouteComponentProps} from 'react-router-dom';
|
||||||
@@ -293,12 +293,11 @@ export class TopBar extends React.Component<
|
|||||||
}
|
}
|
||||||
|
|
||||||
private checkWikiTreeLoginState() {
|
private checkWikiTreeLoginState() {
|
||||||
const wikiTreeLoginState =
|
const wikiTreeLoginUsername = getLoggedInUserName();
|
||||||
Cookies.get('wikidb_wtb_UserID') !== undefined
|
const wikiTreeLoginState = wikiTreeLoginUsername
|
||||||
? WikiTreeLoginState.LOGGED_IN
|
? WikiTreeLoginState.LOGGED_IN
|
||||||
: WikiTreeLoginState.NOT_LOGGED_IN;
|
: WikiTreeLoginState.NOT_LOGGED_IN;
|
||||||
if (this.state.wikiTreeLoginState !== wikiTreeLoginState) {
|
if (this.state.wikiTreeLoginState !== wikiTreeLoginState) {
|
||||||
const wikiTreeLoginUsername = Cookies.get('wikidb_wtb_UserName');
|
|
||||||
this.setState(
|
this.setState(
|
||||||
Object.assign({}, this.state, {
|
Object.assign({}, this.state, {
|
||||||
wikiTreeLoginState,
|
wikiTreeLoginState,
|
||||||
@@ -650,7 +649,7 @@ export class TopBar extends React.Component<
|
|||||||
src={WIKITREE_LOGO_URL}
|
src={WIKITREE_LOGO_URL}
|
||||||
alt="WikiTree logo"
|
alt="WikiTree logo"
|
||||||
className="menu-icon"
|
className="menu-icon"
|
||||||
/>
|
/>
|
||||||
<FormattedMessage
|
<FormattedMessage
|
||||||
id="menu.select_wikitree_id"
|
id="menu.select_wikitree_id"
|
||||||
defaultMessage="Select WikiTree ID"
|
defaultMessage="Select WikiTree ID"
|
||||||
@@ -828,7 +827,7 @@ export class TopBar extends React.Component<
|
|||||||
src={WIKITREE_LOGO_URL}
|
src={WIKITREE_LOGO_URL}
|
||||||
alt="WikiTree logo"
|
alt="WikiTree logo"
|
||||||
className="menu-icon"
|
className="menu-icon"
|
||||||
/>
|
/>
|
||||||
<FormattedMessage
|
<FormattedMessage
|
||||||
id="menu.wikitree_login"
|
id="menu.wikitree_login"
|
||||||
defaultMessage="Log in to WikiTree"
|
defaultMessage="Log in to WikiTree"
|
||||||
@@ -878,7 +877,7 @@ export class TopBar extends React.Component<
|
|||||||
src={WIKITREE_LOGO_URL}
|
src={WIKITREE_LOGO_URL}
|
||||||
alt="WikiTree logo"
|
alt="WikiTree logo"
|
||||||
className="menu-icon"
|
className="menu-icon"
|
||||||
/>
|
/>
|
||||||
<FormattedMessage
|
<FormattedMessage
|
||||||
id="menu.wikitree_logged_in"
|
id="menu.wikitree_logged_in"
|
||||||
defaultMessage="Logged in"
|
defaultMessage="Logged in"
|
||||||
|
|||||||
@@ -3,6 +3,12 @@ import {Date, JsonFam, JsonIndi, DateOrRange} from 'topola';
|
|||||||
import {GedcomData, TopolaData, normalizeGedcom} from './gedcom_util';
|
import {GedcomData, TopolaData, normalizeGedcom} from './gedcom_util';
|
||||||
import {GedcomEntry} from 'parse-gedcom';
|
import {GedcomEntry} from 'parse-gedcom';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cookie where the logged in user name is stored. This cookie is shared
|
||||||
|
* between apps hosted on apps.wikitree.com.
|
||||||
|
*/
|
||||||
|
const USER_NAME_COOKIE = 'wikidb_wtb_UserName';
|
||||||
|
|
||||||
/** WikiTree API getAncestors request. */
|
/** WikiTree API getAncestors request. */
|
||||||
interface GetAncestorsRequest {
|
interface GetAncestorsRequest {
|
||||||
action: 'getAncestors';
|
action: 'getAncestors';
|
||||||
@@ -11,19 +17,29 @@ interface GetAncestorsRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** WikiTree API getRelatives request. */
|
/** WikiTree API getRelatives request. */
|
||||||
interface GetRelatives {
|
interface GetRelativesRequest {
|
||||||
action: 'getRelatives';
|
action: 'getRelatives';
|
||||||
keys: string;
|
keys: string;
|
||||||
getChildren?: true;
|
getChildren?: true;
|
||||||
getSpouses?: true;
|
getSpouses?: true;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ClientLogin {
|
/** WikiTree API clientLogin request. */
|
||||||
|
interface ClientLoginRequest {
|
||||||
action: 'clientLogin';
|
action: 'clientLogin';
|
||||||
authcode: string;
|
authcode: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
type WikiTreeRequest = GetAncestorsRequest | GetRelatives | ClientLogin;
|
/** WikiTree API clientLogin response. */
|
||||||
|
interface ClientLoginResponse {
|
||||||
|
result: string;
|
||||||
|
username: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
type WikiTreeRequest =
|
||||||
|
| GetAncestorsRequest
|
||||||
|
| GetRelativesRequest
|
||||||
|
| ClientLoginRequest;
|
||||||
|
|
||||||
/** Person structure returned from WikiTree API. */
|
/** Person structure returned from WikiTree API. */
|
||||||
interface Person {
|
interface Person {
|
||||||
@@ -154,7 +170,9 @@ async function getRelatives(keys: string[], handleCors: boolean) {
|
|||||||
return result.concat(fetchedResults);
|
return result.concat(fetchedResults);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function clientLogin(authcode: string) {
|
export async function clientLogin(
|
||||||
|
authcode: string,
|
||||||
|
): Promise<ClientLoginResponse> {
|
||||||
const response = await wikiTreeGet(
|
const response = await wikiTreeGet(
|
||||||
{
|
{
|
||||||
action: 'clientLogin',
|
action: 'clientLogin',
|
||||||
@@ -165,6 +183,18 @@ export async function clientLogin(authcode: string) {
|
|||||||
return response.clientLogin;
|
return response.clientLogin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returnes the logged in user name or undefined if not logged in.
|
||||||
|
*
|
||||||
|
* This is not an authoritative answer. The result of this function relies on
|
||||||
|
* the cookies set on the apps.wikitree.com domain under which this application
|
||||||
|
* is hosted. The authoritative source of login information is in cookies set on
|
||||||
|
* the api.wikitree.com domain.
|
||||||
|
*/
|
||||||
|
export function getLoggedInUserName(): string | undefined {
|
||||||
|
return Cookies.get(USER_NAME_COOKIE);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads data from WikiTree to populate an hourglass chart starting from the
|
* Loads data from WikiTree to populate an hourglass chart starting from the
|
||||||
* given person ID.
|
* given person ID.
|
||||||
@@ -176,10 +206,11 @@ export async function loadWikiTree(
|
|||||||
// Work around CORS if not in apps.wikitree.com domain.
|
// Work around CORS if not in apps.wikitree.com domain.
|
||||||
const handleCors = window.location.hostname !== 'apps.wikitree.com';
|
const handleCors = window.location.hostname !== 'apps.wikitree.com';
|
||||||
|
|
||||||
if (!handleCors && !Cookies.get('wikidb_wtb_UserID') && authcode) {
|
if (!handleCors && !getLoggedInUserName() && authcode) {
|
||||||
const loginResult = await clientLogin(authcode);
|
const loginResult = await clientLogin(authcode);
|
||||||
if (loginResult.result === 'Success') {
|
if (loginResult.result === 'Success') {
|
||||||
sessionStorage.clear();
|
sessionStorage.clear();
|
||||||
|
Cookies.set(USER_NAME_COOKIE, loginResult.username);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user