1 |
|
2 | declare var require: any;
|
3 | import React, { useEffect } from 'react';
|
4 | //import Cookies from 'universal-cookie';
|
5 | //import {IC_USER_ID} from "../authentication/auth-middleware";
|
6 |
|
7 |
|
8 | //import ExecutionEnvironment from 'exenv';
|
9 | //import {withRequest} from "../components/attach-request";
|
10 |
|
11 | /*
|
12 | export const isLoggedIn = (identityKey) => {
|
13 | const val = new Cookies().get(identityKey);
|
14 | //console.log("forceLogin: ",val);
|
15 | return val !== undefined;
|
16 | }*/
|
17 |
|
18 | // create a context with the default value: empty
|
19 | //const LoggedInContext = React.createContext(undefined);
|
20 |
|
21 |
|
22 | interface ForceLoginProps {
|
23 | userId: string | undefined,
|
24 | children: any,
|
25 | //request? : any // passed by `withRequest`
|
26 | //identityKey: string // the primaryKey-property of the applicable <Identity />-Component
|
27 | }
|
28 | /**
|
29 | * implements [[RequireServerRenderingSpec]]
|
30 | *
|
31 | * the ForceLogin Component is a HOC that passes the userid to its child components
|
32 | * the forceLogin gets the cookie value regardless on whether it runs on the server or in the browser
|
33 | * see: https://www.npmjs.com/package/universal-cookie
|
34 | *
|
35 | * see hocs with context: https://itnext.io/combining-hocs-with-the-new-reacts-context-api-9d3617dccf0b
|
36 | *
|
37 | * how to check whether running on server or in browser: https://www.npmjs.com/package/exenv
|
38 | */
|
39 | const ForceLogin = (props: ForceLoginProps) => {
|
40 |
|
41 |
|
42 | /**
|
43 | * When we run in the browser: if not logged in, then make the page reload from the server to
|
44 | * make the login-middlewares apply
|
45 | */
|
46 | useEffect(()=> {
|
47 | //console.log("ForceLogin-Component: ", this.props.identityKey)
|
48 |
|
49 | if (props.userId == undefined) {
|
50 | window.location.reload();
|
51 | }
|
52 |
|
53 | });
|
54 |
|
55 |
|
56 | //console.log("ForceLogin: request ->", this.props.request)
|
57 |
|
58 | // we provide the information which user is logged in
|
59 | return <div>{props.children}</div>
|
60 |
|
61 | }
|
62 |
|
63 | import {withUser} from "./attach-user";
|
64 |
|
65 |
|
66 | /**
|
67 | * we MUST NOT IMPORT CONTEXTs directly, but require them at time of use generally from Infrastructure-Components
|
68 | * because this then resolves to node_modules
|
69 | */
|
70 | export default withUser(ForceLogin);
|
71 |
|
72 |
|
73 |
|
74 |
|