@tryfinch/react-connect 2.8.1 → 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/.eslintrc.js +8 -1
- package/README.md +1 -1
- package/dist/index.d.ts +23 -22
- package/dist/index.d.ts.map +1 -0
- package/dist/index.es.js +103 -110
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +103 -110
- package/dist/index.js.map +1 -1
- package/example/package-lock.json +17917 -42258
- package/package.json +16 -45
- package/rollup.config.js +9 -38
- package/src/index.ts +177 -0
- package/tsconfig.json +15 -0
- package/.babelrc +0 -9
- package/src/index.d.ts +0 -25
- package/src/index.js +0 -97
package/package.json
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryfinch/react-connect",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Finch SDK for embedding Finch Connect in API React Single Page Applications (SPA)",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"finch",
|
|
7
|
+
"sdk",
|
|
8
|
+
"payroll",
|
|
9
|
+
"react",
|
|
10
|
+
"oauth"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://tryfinch.com/",
|
|
5
13
|
"author": "",
|
|
6
14
|
"license": "MIT",
|
|
7
|
-
"repository": "Finch-API/react-
|
|
15
|
+
"repository": "Finch-API/react-connect",
|
|
8
16
|
"main": "dist/index.js",
|
|
9
17
|
"types": "dist/index.d.ts",
|
|
10
18
|
"module": "dist/index.es.js",
|
|
@@ -14,9 +22,8 @@
|
|
|
14
22
|
"npm": ">=5"
|
|
15
23
|
},
|
|
16
24
|
"scripts": {
|
|
17
|
-
"test": "cross-env CI=1 react-scripts test --env=jsdom",
|
|
18
|
-
"test:watch": "react-scripts test --env=jsdom",
|
|
19
25
|
"build": "rollup -c",
|
|
26
|
+
"lint": "eslint --ext .js,.jsx,.ts,.tsx src",
|
|
20
27
|
"start": "rollup -c -w",
|
|
21
28
|
"prepare": "npm run build",
|
|
22
29
|
"predeploy": "cd example && npm install && npm run build"
|
|
@@ -25,54 +32,18 @@
|
|
|
25
32
|
"react": ">=16.9.0"
|
|
26
33
|
},
|
|
27
34
|
"devDependencies": {
|
|
28
|
-
"@babel/core": "^7.0.0",
|
|
29
|
-
"@babel/plugin-external-helpers": "^7.0.0",
|
|
30
|
-
"@babel/plugin-proposal-class-properties": "^7.0.0",
|
|
31
|
-
"@babel/plugin-proposal-decorators": "^7.0.0",
|
|
32
|
-
"@babel/plugin-proposal-do-expressions": "^7.0.0",
|
|
33
|
-
"@babel/plugin-proposal-export-default-from": "^7.0.0",
|
|
34
|
-
"@babel/plugin-proposal-export-namespace-from": "^7.0.0",
|
|
35
|
-
"@babel/plugin-proposal-function-bind": "^7.0.0",
|
|
36
|
-
"@babel/plugin-proposal-function-sent": "^7.0.0",
|
|
37
|
-
"@babel/plugin-proposal-json-strings": "^7.0.0",
|
|
38
|
-
"@babel/plugin-proposal-logical-assignment-operators": "^7.0.0",
|
|
39
|
-
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
|
|
40
|
-
"@babel/plugin-proposal-numeric-separator": "^7.0.0",
|
|
41
|
-
"@babel/plugin-proposal-optional-chaining": "^7.0.0",
|
|
42
|
-
"@babel/plugin-proposal-pipeline-operator": "^7.0.0",
|
|
43
|
-
"@babel/plugin-proposal-throw-expressions": "^7.0.0",
|
|
44
|
-
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
|
|
45
|
-
"@babel/plugin-syntax-import-meta": "^7.0.0",
|
|
46
|
-
"@babel/preset-env": "^7.0.0",
|
|
47
|
-
"@babel/preset-react": "^7.0.0",
|
|
48
35
|
"@rollup/plugin-replace": "^4.0.0",
|
|
49
|
-
"@
|
|
50
|
-
"
|
|
51
|
-
"
|
|
36
|
+
"@rollup/plugin-typescript": "^11.1.0",
|
|
37
|
+
"@types/react": "^16.14.40",
|
|
38
|
+
"@typescript-eslint/eslint-plugin": "^5.59.0",
|
|
39
|
+
"@typescript-eslint/parser": "^5.59.0",
|
|
52
40
|
"eslint": "^7.32.0",
|
|
53
41
|
"eslint-config-airbnb": "^18.2.0",
|
|
54
42
|
"eslint-config-prettier": "^6.11.0",
|
|
55
|
-
"eslint-config-standard": "^11.0.0",
|
|
56
|
-
"eslint-config-standard-react": "^6.0.0",
|
|
57
|
-
"eslint-plugin-import": "^2.22.0",
|
|
58
|
-
"eslint-plugin-jsx-a11y": "^6.3.1",
|
|
59
|
-
"eslint-plugin-node": "^7.0.1",
|
|
60
43
|
"eslint-plugin-prettier": "^3.1.4",
|
|
61
|
-
"eslint-plugin-promise": "^4.0.0",
|
|
62
|
-
"eslint-plugin-react": "^7.20.3",
|
|
63
|
-
"eslint-plugin-react-hooks": "^4.0.8",
|
|
64
|
-
"eslint-plugin-standard": "^3.1.0",
|
|
65
|
-
"gh-pages": "^2.0.1",
|
|
66
|
-
"prettier": "^2.0.5",
|
|
67
44
|
"react": "^16.9.0",
|
|
68
|
-
"react-scripts": "^5.0.1",
|
|
69
|
-
"react-test-renderer": "^16.9.0",
|
|
70
45
|
"rollup": "^2.75.7",
|
|
71
|
-
"rollup-plugin-babel": "^4.3.2",
|
|
72
|
-
"rollup-plugin-commonjs": "^9.2.0",
|
|
73
|
-
"rollup-plugin-dts": "^4.0.1",
|
|
74
|
-
"rollup-plugin-node-resolve": "^4.0.0",
|
|
75
46
|
"rollup-plugin-peer-deps-external": "^2.2.0",
|
|
76
|
-
"
|
|
47
|
+
"typescript": "^4.9.5"
|
|
77
48
|
}
|
|
78
49
|
}
|
package/rollup.config.js
CHANGED
|
@@ -1,49 +1,20 @@
|
|
|
1
|
-
import babel from 'rollup-plugin-babel';
|
|
2
|
-
import commonjs from 'rollup-plugin-commonjs';
|
|
3
|
-
import dts from 'rollup-plugin-dts';
|
|
4
1
|
import external from 'rollup-plugin-peer-deps-external';
|
|
5
|
-
import resolve from 'rollup-plugin-node-resolve';
|
|
6
|
-
import url from 'rollup-plugin-url';
|
|
7
2
|
import replace from '@rollup/plugin-replace';
|
|
3
|
+
import typescript from '@rollup/plugin-typescript';
|
|
8
4
|
|
|
9
5
|
import pkg from './package.json';
|
|
10
6
|
|
|
7
|
+
const plugins = [external(), replace({ SDK_VERSION: pkg.version }), typescript()];
|
|
8
|
+
|
|
11
9
|
export default [
|
|
12
10
|
{
|
|
13
|
-
input: 'src/index.
|
|
14
|
-
output: [
|
|
15
|
-
|
|
16
|
-
file: pkg.main,
|
|
17
|
-
format: 'cjs',
|
|
18
|
-
sourcemap: true,
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
file: pkg.module,
|
|
22
|
-
format: 'es',
|
|
23
|
-
sourcemap: true,
|
|
24
|
-
},
|
|
25
|
-
],
|
|
26
|
-
plugins: [
|
|
27
|
-
external(),
|
|
28
|
-
url({ exclude: ['**/*.svg'] }),
|
|
29
|
-
babel({
|
|
30
|
-
exclude: 'node_modules/**',
|
|
31
|
-
}),
|
|
32
|
-
resolve(),
|
|
33
|
-
commonjs(),
|
|
34
|
-
replace({
|
|
35
|
-
SDK_VERSION: JSON.stringify(pkg.version), // has to be stringified somehow
|
|
36
|
-
}),
|
|
37
|
-
],
|
|
11
|
+
input: 'src/index.ts',
|
|
12
|
+
output: [{ file: pkg.main, format: 'cjs', sourcemap: true }],
|
|
13
|
+
plugins,
|
|
38
14
|
},
|
|
39
15
|
{
|
|
40
|
-
input: 'src/index.
|
|
41
|
-
output: [
|
|
42
|
-
|
|
43
|
-
file: pkg.types,
|
|
44
|
-
format: 'es',
|
|
45
|
-
},
|
|
46
|
-
],
|
|
47
|
-
plugins: [dts()],
|
|
16
|
+
input: 'src/index.ts',
|
|
17
|
+
output: [{ file: pkg.module, format: 'es', sourcemap: true }],
|
|
18
|
+
plugins,
|
|
48
19
|
},
|
|
49
20
|
];
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
export type SuccessEvent = {
|
|
4
|
+
code: string;
|
|
5
|
+
state?: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type ErrorEvent = {
|
|
9
|
+
errorMessage: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type ConnectOptions = {
|
|
13
|
+
category: string | null;
|
|
14
|
+
clientId: string;
|
|
15
|
+
manual: boolean;
|
|
16
|
+
state: string | null;
|
|
17
|
+
onSuccess: (e: SuccessEvent) => void;
|
|
18
|
+
onError: (e: ErrorEvent) => void;
|
|
19
|
+
onClose: () => void;
|
|
20
|
+
payrollProvider: string | null;
|
|
21
|
+
products: string[];
|
|
22
|
+
sandbox: boolean;
|
|
23
|
+
zIndex: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type OpenFn = (overrides?: Partial<Pick<ConnectOptions, 'products'>>) => void;
|
|
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
|
+
|
|
45
|
+
interface FinchConnectPostMessage {
|
|
46
|
+
data: FinchConnectAuthMessage;
|
|
47
|
+
origin: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const BASE_FINCH_CONNECT_URI = 'https://connect.tryfinch.com';
|
|
51
|
+
const DEFAULT_FINCH_REDIRECT_URI = 'https://tryfinch.com';
|
|
52
|
+
const FINCH_CONNECT_IFRAME_ID = 'finch-connect-iframe';
|
|
53
|
+
const FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message';
|
|
54
|
+
|
|
55
|
+
const constructAuthUrl = ({
|
|
56
|
+
clientId,
|
|
57
|
+
payrollProvider,
|
|
58
|
+
category,
|
|
59
|
+
products,
|
|
60
|
+
manual,
|
|
61
|
+
sandbox,
|
|
62
|
+
state,
|
|
63
|
+
}: Partial<ConnectOptions>) => {
|
|
64
|
+
const authUrl = new URL(`${BASE_FINCH_CONNECT_URI}/authorize`);
|
|
65
|
+
if (clientId) authUrl.searchParams.append('client_id', clientId);
|
|
66
|
+
if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider);
|
|
67
|
+
if (category) authUrl.searchParams.append('category', category);
|
|
68
|
+
authUrl.searchParams.append('products', (products ?? []).join(' '));
|
|
69
|
+
authUrl.searchParams.append('app_type', 'spa');
|
|
70
|
+
authUrl.searchParams.append('redirect_uri', DEFAULT_FINCH_REDIRECT_URI);
|
|
71
|
+
authUrl.searchParams.append('mode', 'employer');
|
|
72
|
+
if (manual) authUrl.searchParams.append('manual', String(manual));
|
|
73
|
+
if (sandbox) authUrl.searchParams.append('sandbox', String(sandbox));
|
|
74
|
+
if (state) authUrl.searchParams.append('state', state);
|
|
75
|
+
// replace with actual SDK version by rollup
|
|
76
|
+
authUrl.searchParams.append('sdk_version', 'react-SDK_VERSION');
|
|
77
|
+
|
|
78
|
+
return authUrl.href;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const noop = () => {
|
|
82
|
+
// intentionally empty
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const DEFAULT_OPTIONS: Omit<ConnectOptions, 'clientId'> = {
|
|
86
|
+
category: null,
|
|
87
|
+
manual: false,
|
|
88
|
+
onSuccess: noop,
|
|
89
|
+
onError: noop,
|
|
90
|
+
onClose: noop,
|
|
91
|
+
payrollProvider: null,
|
|
92
|
+
products: [],
|
|
93
|
+
sandbox: false,
|
|
94
|
+
state: null,
|
|
95
|
+
zIndex: 999,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const useFinchConnect = (options: Partial<ConnectOptions>): { open: OpenFn } => {
|
|
99
|
+
if (!options.clientId) throw new Error('must specify clientId in options for useFinchConnect');
|
|
100
|
+
|
|
101
|
+
const combinedOptions: ConnectOptions = {
|
|
102
|
+
clientId: '',
|
|
103
|
+
...DEFAULT_OPTIONS,
|
|
104
|
+
...options,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const open: OpenFn = (overrides?: Partial<Pick<ConnectOptions, 'products'>>) => {
|
|
108
|
+
const openOptions: ConnectOptions = {
|
|
109
|
+
...combinedOptions,
|
|
110
|
+
...overrides,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
if (!document.getElementById(FINCH_CONNECT_IFRAME_ID)) {
|
|
114
|
+
const iframe = document.createElement('iframe');
|
|
115
|
+
iframe.src = constructAuthUrl(openOptions);
|
|
116
|
+
iframe.frameBorder = '0';
|
|
117
|
+
iframe.id = FINCH_CONNECT_IFRAME_ID;
|
|
118
|
+
iframe.style.position = 'fixed';
|
|
119
|
+
iframe.style.zIndex = openOptions.zIndex.toString();
|
|
120
|
+
iframe.style.height = '100%';
|
|
121
|
+
iframe.style.width = '100%';
|
|
122
|
+
iframe.style.top = '0';
|
|
123
|
+
iframe.style.backgroundColor = 'none transparent';
|
|
124
|
+
iframe.style.border = 'none';
|
|
125
|
+
document.body.prepend(iframe);
|
|
126
|
+
document.body.style.overflow = 'hidden';
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const close = () => {
|
|
131
|
+
const frameToRemove = document.getElementById(FINCH_CONNECT_IFRAME_ID);
|
|
132
|
+
if (frameToRemove) {
|
|
133
|
+
frameToRemove.parentNode?.removeChild(frameToRemove);
|
|
134
|
+
document.body.style.overflow = 'inherit';
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
function handleFinchAuth(event: FinchConnectPostMessage) {
|
|
140
|
+
if (!event.data) return;
|
|
141
|
+
if (event.data.name !== FINCH_AUTH_MESSAGE_NAME) return;
|
|
142
|
+
if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI)) return;
|
|
143
|
+
|
|
144
|
+
close();
|
|
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
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
window.addEventListener('message', handleFinchAuth);
|
|
171
|
+
return () => window.removeEventListener('message', handleFinchAuth);
|
|
172
|
+
}, [combinedOptions.onClose, combinedOptions.onError, combinedOptions.onSuccess]);
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
open,
|
|
176
|
+
};
|
|
177
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2016",
|
|
4
|
+
"module": "esnext",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"declarationMap": true,
|
|
7
|
+
"sourceMap": true,
|
|
8
|
+
"outDir": "./",
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"strict": true
|
|
12
|
+
},
|
|
13
|
+
"include": ["src/**/*"],
|
|
14
|
+
"exclude": ["node_modules", "example/**/*"]
|
|
15
|
+
}
|
package/.babelrc
DELETED
package/src/index.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
declare module 'react-finch-connect' {
|
|
2
|
-
export type SuccessEvent = {
|
|
3
|
-
code: string;
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
export type ErrorEvent = {
|
|
7
|
-
errorMessage: string;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export type ConnectOptions = {
|
|
11
|
-
clientId: string;
|
|
12
|
-
products?: string[];
|
|
13
|
-
mode?: string;
|
|
14
|
-
manual?: boolean;
|
|
15
|
-
payrollProvider?: string;
|
|
16
|
-
sandbox?: boolean;
|
|
17
|
-
category?: string;
|
|
18
|
-
onSuccess?: (e: SuccessEvent) => void;
|
|
19
|
-
onError?: (e: ErrorEvent) => void;
|
|
20
|
-
onClose?: () => void;
|
|
21
|
-
zIndex?: bigint | string;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export function useFinchConnect(opts: ConnectOptions): { open: () => void };
|
|
25
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { useEffect } from 'react';
|
|
2
|
-
|
|
3
|
-
const BASE_FINCH_CONNECT_URI = 'https://connect.tryfinch.com';
|
|
4
|
-
const DEFAULT_FINCH_REDIRECT_URI = 'https://tryfinch.com';
|
|
5
|
-
const FINCH_CONNECT_IFRAME_ID = 'finch-connect-iframe';
|
|
6
|
-
const FINCH_AUTH_MESSAGE_NAME = 'finch-auth-message';
|
|
7
|
-
|
|
8
|
-
const noop = () => {};
|
|
9
|
-
|
|
10
|
-
// eslint-disable-next-line import/prefer-default-export
|
|
11
|
-
export const useFinchConnect = (options = {}) => {
|
|
12
|
-
const {
|
|
13
|
-
clientId,
|
|
14
|
-
products = [],
|
|
15
|
-
mode = 'employer',
|
|
16
|
-
category,
|
|
17
|
-
manual = false,
|
|
18
|
-
payrollProvider = null,
|
|
19
|
-
sandbox = false,
|
|
20
|
-
onSuccess = noop,
|
|
21
|
-
onError = noop,
|
|
22
|
-
onClose = noop,
|
|
23
|
-
zIndex = 999,
|
|
24
|
-
} = options;
|
|
25
|
-
|
|
26
|
-
const _constructAuthUrl = (clientId, products) => {
|
|
27
|
-
const authUrl = new URL(`${BASE_FINCH_CONNECT_URI}/authorize`);
|
|
28
|
-
|
|
29
|
-
if (clientId) authUrl.searchParams.append('client_id', clientId);
|
|
30
|
-
if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider);
|
|
31
|
-
if (category) authUrl.searchParams.append('category', category);
|
|
32
|
-
authUrl.searchParams.append('products', products.join(' '));
|
|
33
|
-
authUrl.searchParams.append('app_type', 'spa');
|
|
34
|
-
authUrl.searchParams.append('redirect_uri', DEFAULT_FINCH_REDIRECT_URI);
|
|
35
|
-
authUrl.searchParams.append('mode', mode);
|
|
36
|
-
if (manual) authUrl.searchParams.append('manual', manual);
|
|
37
|
-
if (sandbox) authUrl.searchParams.append('sandbox', sandbox);
|
|
38
|
-
/* global SDK_VERSION */
|
|
39
|
-
if (SDK_VERSION) authUrl.searchParams.append('sdk_version', `react-${SDK_VERSION}`);
|
|
40
|
-
|
|
41
|
-
return authUrl.href;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const open = () => {
|
|
45
|
-
if (document.getElementById(FINCH_CONNECT_IFRAME_ID)) {
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const iframe = document.createElement('iframe');
|
|
50
|
-
iframe.src = _constructAuthUrl(clientId, products);
|
|
51
|
-
iframe.frameBorder = '0';
|
|
52
|
-
iframe.id = FINCH_CONNECT_IFRAME_ID;
|
|
53
|
-
iframe.style.position = 'fixed';
|
|
54
|
-
iframe.style.zIndex = zIndex.toString();
|
|
55
|
-
iframe.style.height = '100%';
|
|
56
|
-
iframe.style.width = '100%';
|
|
57
|
-
iframe.style.top = '0';
|
|
58
|
-
iframe.style.backgroundColor = 'none transparent';
|
|
59
|
-
iframe.style.border = 'none';
|
|
60
|
-
document.body.prepend(iframe);
|
|
61
|
-
document.body.style.overflow = 'hidden';
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const close = () => {
|
|
65
|
-
const frameToRemove = document.getElementById(FINCH_CONNECT_IFRAME_ID);
|
|
66
|
-
if (frameToRemove) {
|
|
67
|
-
frameToRemove.parentNode.removeChild(frameToRemove);
|
|
68
|
-
document.body.style.overflow = 'inherit';
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
useEffect(() => {
|
|
73
|
-
function handleFinchAuth(event) {
|
|
74
|
-
const handleFinchAuthSuccess = (code) => onSuccess({ code });
|
|
75
|
-
const handleFinchAuthError = (error) => onError({ errorMessage: error });
|
|
76
|
-
const handleFinchAuthClose = () => onClose();
|
|
77
|
-
|
|
78
|
-
if (!event.data) return;
|
|
79
|
-
if (event.data.name !== FINCH_AUTH_MESSAGE_NAME) return;
|
|
80
|
-
if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI)) return;
|
|
81
|
-
|
|
82
|
-
const { code, error, closed } = event.data;
|
|
83
|
-
|
|
84
|
-
close();
|
|
85
|
-
if (code) handleFinchAuthSuccess(code);
|
|
86
|
-
else if (error) handleFinchAuthError(error);
|
|
87
|
-
else if (closed) handleFinchAuthClose();
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
window.addEventListener('message', handleFinchAuth);
|
|
91
|
-
return () => window.removeEventListener('message', handleFinchAuth);
|
|
92
|
-
}, [onClose, onError, onSuccess]);
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
open,
|
|
96
|
-
};
|
|
97
|
-
};
|