UNPKG

5.44 kBPlain TextView Raw
1import React, {ReactNode} from 'react';
2
3
4import uuidv4 from 'uuid/v4';
5
6import {IComponent} from "../types/component";
7import Types, { IInfrastructure } from "../types";
8
9import createMiddleware, { isMiddleware } from '../middleware/middleware-component';
10import createWebApp, { isWebApp } from '../webapp/webapp-component';
11
12import { getChildrenArray, findComponentRecursively } from '../libs';
13import cookiesMiddleware from 'universal-cookie-express';
14
15export const IDENTITY_INSTANCE_TYPE = "IdentityComponent";
16
17export const getBrowserId = (req, key=IDENTITY_KEY) => {
18 const browserId = req.universalCookies.get(key);
19
20 if (browserId !== undefined) {
21 return browserId;
22
23 } else {
24 const newId = uuidv4();
25 req.universalCookies.set(key, newId);
26 return newId;
27 }
28}
29
30
31
32export const IDENTITY_KEY ="IC_IDENTITY_KEY";
33
34/**
35 * Specifies all the properties that a Authentication-Component must have
36 */
37export interface IIdentityArgs {
38
39
40}
41
42
43/**
44 * specifies the properties that an Identity-Component has during runtime
45 */
46export interface IIdentityProps {
47
48 /**
49 * Function that allows the identity to store the user-data
50 * filled by the DataLayer
51 *
52 * @storeData: a function that storesData
53 */
54 setStoreData: (
55 storeData: (pkEntity: string, pkVal: any, skEntity: string, skVal: any, jsonData: any) => void
56 ) => void
57
58 storeData?: (pkEntity: string, pkVal: any, skEntity: string, skVal: any, jsonData: any) => void,
59
60 setGetData: (
61 storeData: (pkEntity: string, pkVal: any, skEntity: string, skVal: any) => any
62 ) => void
63
64 getData?: (pkEntity: string, pkVal: any, skEntity: string, skVal: any) => any
65}
66
67
68/**
69 * The Identity-Component uses cookies to uniquely identify a browser
70 *
71 * @param props
72 */
73export default (props: IIdentityArgs | any) => {
74
75 //console.log ("Identity: ",props );
76
77 const componentProps: IInfrastructure & IComponent = {
78 infrastructureType: Types.INFRASTRUCTURE_TYPE_COMPONENT,
79 instanceType: IDENTITY_INSTANCE_TYPE,
80 instanceId: undefined, // authentications cannot be found programmatically?!
81 };
82
83
84 const identityProps: IIdentityProps = {
85 setStoreData: (storeData: (pkEntity: string, pkVal: any, skEntity: string, skVal: any, jsonData: any) => void) => {
86 //console.log("setStoreData: ",storeData)
87 props.storeData = storeData;
88 },
89
90 setGetData: (getData: (pkEntity: string, pkVal: any, skEntity: string, skVal: any) => void) => {
91 //console.log("setStoreData: ",storeData)
92 props.getData = getData;
93 }
94 }
95
96 // if the child needs to store data that belongs to the user, provide a function to do so!
97 findComponentRecursively(props.children, (child) => child.setStoreIdentityData !== undefined).forEach( child => {
98
99 child.setStoreIdentityData(
100
101 async function (request: any, secondaryKey: string, val: any, jsonData: any) {
102 //console.log("identity: storeData: ", props);
103 return await props.storeData(
104 IDENTITY_KEY, //pkEntity: string,
105 getBrowserId(request, IDENTITY_KEY), //pkVal: any,
106 secondaryKey, //skEntity: string,
107 val, //skVal: any,
108 jsonData //: any
109 )
110 }
111 );
112
113 child.setGetIdentityData(
114
115 async function (request: any, matchBrowserIdentity: boolean, secondaryKey: string, val: any) {
116 //console.log("identity: storeData: ", props);
117 return await props.getData(
118 IDENTITY_KEY, //pkEntity: string,
119 matchBrowserIdentity ? getBrowserId(request, IDENTITY_KEY) : undefined, //pkVal: any,
120 secondaryKey, //skEntity: string,
121 val //skVal: any,
122 )
123 }
124 );
125
126 });
127
128
129 /**
130 * The Identity requires cookies to store an uuid
131 */
132 const mappedChildren = {
133 // we provide the middlewares that we require
134 children: [
135
136 // we need to use cookies in order to verify whether a user is logged in
137 createMiddleware({ callback: cookiesMiddleware() }),
138
139
140 // here we provide all interested children with the identity - on server side only!
141 // but for the browser, we provide the cookie
142 createMiddleware({ callback: (req, res, next) => {
143
144 //console.log("this it the identity-mw")
145 findComponentRecursively(props.children, (child) => child.setIdentity !== undefined).forEach( child => {
146 child.setIdentity(getBrowserId(req, IDENTITY_KEY));
147 });
148
149 return next();
150 }})
151
152 ].concat(props.children)
153 };
154
155 //console.log("mapped children: ", mappedChildren.children.filter(c=> isWebApp(c)).map(c=> c.routes.map(r=>r.middlewares)));
156
157 //console.log("identity children: ", findComponentRecursively(props.children, isWebApp));
158 //console.log("identity mapped children: ", findComponentRecursively(mappedChildren.children, isWebApp));
159
160 return Object.assign(props, componentProps, identityProps, mappedChildren);
161
162
163};
164
165export const isIdentity = (component) => {
166
167 return component !== undefined &&
168 component.instanceType === IDENTITY_INSTANCE_TYPE;
169};
\No newline at end of file