@myxtra/authentication-green 1.16.0 → 2.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -118,3 +118,68 @@ type Config = {
118
118
  position?: 'left' | 'right';
119
119
  };
120
120
  ```
121
+
122
+ ## Events & window object
123
+
124
+ The authentication component will emit events and add properties to the window object. For backwards
125
+ compatibility with the orange toolkit, the window properties are the same.
126
+
127
+ ### Window object
128
+
129
+ Everything is available under the `window.xtra` object. If the user is authenticated, the component
130
+ will also add the following properties to the window object:
131
+
132
+ ```ts
133
+ type XTRA = {
134
+ user: {
135
+ firstName: () => string;
136
+ lastName: () => string;
137
+ dob: () => number;
138
+ mob: () => number;
139
+ yob: () => number;
140
+ gender: () => 'M' | 'F';
141
+ phone: () => string;
142
+ streetName: () => string,
143
+ houseNr: () => string,
144
+ box: () => string,
145
+ zip: () => string,
146
+ city: () => string,
147
+ country: () => string,
148
+ email: () => string,
149
+ cbh: () => string,
150
+ state: () => 'Authenticated',
151
+ }
152
+ ```
153
+
154
+ The authenticated event is sent through
155
+ [window.postMessage](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage), below you
156
+ can find an example of how to listen for the event:
157
+
158
+ ```ts
159
+ type XtraAuthenticationMessageEventData = {
160
+ source: 'xtra-authentication';
161
+ isAuthenticated?: boolean;
162
+ address?: Address;
163
+ };
164
+
165
+ const onMessage = (message: MessageEvent<XtraAuthenticationMessageEventData>) => {
166
+ // Ensure the event wasn't sent from another domain
167
+ if (event.origin !== new URL(window.location.href).origin) {
168
+ return;
169
+ }
170
+
171
+ // Ensure the event wasn't sent from another source
172
+ if (!event.data.source === 'xtra-authenticaiton') {
173
+ return;
174
+ }
175
+
176
+ if (!event.data.isAuthenticated) {
177
+ // TODO: add logic in case the user is not authenticated
178
+ return;
179
+ }
180
+
181
+ // TODO: add logic when the user is authenticated (window.xtra is set here)
182
+ };
183
+
184
+ window.addEventListener('message', onMessage);
185
+ ```