@tryfinch/react-connect 3.0.0 → 3.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryfinch/react-connect",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Finch SDK for embedding Finch Connect in API React Single Page Applications (SPA)",
5
5
  "keywords": [
6
6
  "finch",
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ import { useEffect } from 'react';
2
2
 
3
3
  export type SuccessEvent = {
4
4
  code: string;
5
+ state?: string;
5
6
  };
6
7
 
7
8
  export type ErrorEvent = {
@@ -12,6 +13,7 @@ export type ConnectOptions = {
12
13
  category: string | null;
13
14
  clientId: string;
14
15
  manual: boolean;
16
+ state: string | null;
15
17
  onSuccess: (e: SuccessEvent) => void;
16
18
  onError: (e: ErrorEvent) => void;
17
19
  onClose: () => void;
@@ -23,13 +25,25 @@ export type ConnectOptions = {
23
25
 
24
26
  type OpenFn = (overrides?: Partial<Pick<ConnectOptions, 'products'>>) => void;
25
27
 
28
+ const POST_MESSAGE_NAME = 'finch-auth-message' as const;
29
+
30
+ type FinchConnectAuthMessage = { name: typeof POST_MESSAGE_NAME } & (
31
+ | {
32
+ kind: 'closed';
33
+ }
34
+ | {
35
+ kind: 'success';
36
+ code: string;
37
+ state?: string;
38
+ }
39
+ | {
40
+ kind: 'error';
41
+ error: string;
42
+ }
43
+ );
44
+
26
45
  interface FinchConnectPostMessage {
27
- data: {
28
- name: string;
29
- code?: string;
30
- error?: string;
31
- closed?: boolean;
32
- };
46
+ data: FinchConnectAuthMessage;
33
47
  origin: string;
34
48
  }
35
49
 
@@ -45,9 +59,9 @@ const constructAuthUrl = ({
45
59
  products,
46
60
  manual,
47
61
  sandbox,
62
+ state,
48
63
  }: Partial<ConnectOptions>) => {
49
64
  const authUrl = new URL(`${BASE_FINCH_CONNECT_URI}/authorize`);
50
-
51
65
  if (clientId) authUrl.searchParams.append('client_id', clientId);
52
66
  if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider);
53
67
  if (category) authUrl.searchParams.append('category', category);
@@ -57,6 +71,7 @@ const constructAuthUrl = ({
57
71
  authUrl.searchParams.append('mode', 'employer');
58
72
  if (manual) authUrl.searchParams.append('manual', String(manual));
59
73
  if (sandbox) authUrl.searchParams.append('sandbox', String(sandbox));
74
+ if (state) authUrl.searchParams.append('state', state);
60
75
  // replace with actual SDK version by rollup
61
76
  authUrl.searchParams.append('sdk_version', 'react-SDK_VERSION');
62
77
 
@@ -76,6 +91,7 @@ const DEFAULT_OPTIONS: Omit<ConnectOptions, 'clientId'> = {
76
91
  payrollProvider: null,
77
92
  products: [],
78
93
  sandbox: false,
94
+ state: null,
79
95
  zIndex: 999,
80
96
  };
81
97
 
@@ -121,21 +137,34 @@ export const useFinchConnect = (options: Partial<ConnectOptions>): { open: OpenF
121
137
 
122
138
  useEffect(() => {
123
139
  function handleFinchAuth(event: FinchConnectPostMessage) {
124
- const handleFinchAuthSuccess = (code: string) => combinedOptions.onSuccess({ code });
125
- const handleFinchAuthError = (error: string) =>
126
- combinedOptions.onError({ errorMessage: error });
127
- const handleFinchAuthClose = () => combinedOptions.onClose();
128
-
129
140
  if (!event.data) return;
130
141
  if (event.data.name !== FINCH_AUTH_MESSAGE_NAME) return;
131
142
  if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI)) return;
132
143
 
133
- const { code, error, closed } = event.data;
134
-
135
144
  close();
136
- if (code) handleFinchAuthSuccess(code);
137
- else if (error) handleFinchAuthError(error);
138
- else if (closed) handleFinchAuthClose();
145
+
146
+ switch (event.data.kind) {
147
+ case 'closed':
148
+ combinedOptions.onClose();
149
+ break;
150
+ case 'error':
151
+ combinedOptions.onError({ errorMessage: event.data.error });
152
+ break;
153
+ case 'success':
154
+ combinedOptions.onSuccess({
155
+ code: event.data.code,
156
+ state: event.data.state,
157
+ });
158
+ break;
159
+ default: {
160
+ // This case should never happen, if it does it should be reported to us
161
+ combinedOptions.onError({
162
+ errorMessage: `Report to developers@tryfinch.com: unable to handle window.postMessage for: ${JSON.stringify(
163
+ event.data
164
+ )}`,
165
+ });
166
+ }
167
+ }
139
168
  }
140
169
 
141
170
  window.addEventListener('message', handleFinchAuth);