declare var require: any; import * as React from 'react'; import {IC_USER_ID, IC_WEB_TOKEN} from "../authentication/auth-middleware"; import ExecutionEnvironment from 'exenv'; import Cookies from 'universal-cookie'; import {getBasename} from "../libs/iso-libs"; // create empty context as default const UserContext = React.createContext({}); export function getUserId(request) { if (request) { return new Cookies(request.headers.cookie).get(IC_USER_ID) } else if (ExecutionEnvironment.canUseDOM) { return new Cookies().get(IC_USER_ID) } return undefined; } export function getWebToken(request) { if (request) { return new Cookies(request.headers.cookie).get(IC_WEB_TOKEN) } else if (ExecutionEnvironment.canUseDOM) { return new Cookies().get(IC_WEB_TOKEN) } return undefined; } interface IAttachUserProps { request: any // passed by `withRequest` } /** * This HOC attaches the User-Id to a webapp, may be undefined! */ const AttachUser: React.SFC = (props) => { //console.log("attaching user: ", getUserId(undefined)); return {props.children} }; /** * @param Component * @returns {function(any): any} */ export function withUser(Component) { return function WrapperComponent(props) { return ( {(context: any) => { return }} ); }; } export function userLogout(pathUrl: string | undefined ) { const path = require('path'); const cookies = new Cookies(); cookies.remove(IC_USER_ID, { path: '/' }); cookies.remove(IC_WEB_TOKEN, { path: '/' }); if (pathUrl !== undefined) { window.location.href = path.join(getBasename(), pathUrl); } else { window.location.reload() } } export default require("infrastructure-components").withRequest(AttachUser);