-
Notifications
You must be signed in to change notification settings - Fork 74
/
index.tsx
138 lines (126 loc) · 3.69 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* eslint-disable camelcase */
/**
*
* LoginSocialGoogle
*
*/
import React, { memo, useCallback, useEffect, useRef, useState } from 'react';
import { objectType, IResolveParams } from '../';
interface Props {
scope?: string;
client_id: string;
className?: string;
redirect_uri?: string;
children?: React.ReactNode;
onLoginStart?: () => void;
onReject: (reject: string | objectType) => void;
onResolve: ({ provider, data }: IResolveParams) => void;
}
const JS_SRC =
'https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js';
const SCRIPT_ID = 'apple-login';
const _window = window as any;
const LoginSocialApple = ({
client_id,
scope = 'name email',
className = '',
onLoginStart,
onReject,
onResolve,
redirect_uri = '/',
children,
}: // fetch_basic_profile = true,
Props) => {
const scriptNodeRef = useRef<HTMLScriptElement>(null!);
const [isSdkLoaded, setIsSdkLoaded] = useState(false);
useEffect(() => {
!isSdkLoaded && load();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isSdkLoaded]);
useEffect(
() => () => {
if (scriptNodeRef.current) scriptNodeRef.current.remove();
},
[],
);
const checkIsExistsSDKScript = useCallback(() => {
return !!document.getElementById(SCRIPT_ID);
}, []);
const insertScript = useCallback(
(
d: HTMLDocument,
s: string = 'script',
id: string,
jsSrc: string,
cb: () => void,
) => {
const appleScriptTag: any = d.createElement(s);
appleScriptTag.id = id;
appleScriptTag.src = jsSrc;
appleScriptTag.async = true;
appleScriptTag.defer = true;
const scriptNode = document.getElementsByTagName('script')![0];
scriptNodeRef.current = appleScriptTag;
scriptNode &&
scriptNode.parentNode &&
scriptNode.parentNode.insertBefore(appleScriptTag, scriptNode);
appleScriptTag.onload = cb;
},
[],
);
const handleResponse = useCallback(
(res: objectType) => {
if (res?.access_token) {
onResolve({
provider: 'apple',
data: res,
});
} else {
const data: objectType = res;
onResolve({
provider: 'apple',
data,
});
}
},
[onResolve],
);
const load = useCallback(() => {
if (checkIsExistsSDKScript()) {
setIsSdkLoaded(true);
} else {
insertScript(document, 'script', SCRIPT_ID, JS_SRC, () => {
const options = {
clientId: client_id, // This is the service ID we created.
scope, // To tell apple we want the user name and emails fields in the response it sends us.
redirectURI: redirect_uri, // As registered along with our service ID
state: 'origin:web', // Any string of your choice that you may use for some logic. It's optional and you may omit it.
usePopup: true, // Important if we want to capture the data apple sends on the client side.
};
_window.AppleID.auth.init(options);
setIsSdkLoaded(true);
});
}
}, [scope, client_id, redirect_uri, insertScript, checkIsExistsSDKScript]);
const loginApple = useCallback(async () => {
if (!isSdkLoaded) return;
if (!_window.AppleID) {
load();
onReject("Apple SDK isn't loaded!");
} else {
onLoginStart && onLoginStart();
try {
const response = await _window.AppleID.auth.signIn();
handleResponse(response);
} catch (err) {
onReject({ err });
}
}
}, [handleResponse, isSdkLoaded, load, onLoginStart, onReject]);
return (
<div className={className} onClick={loginApple}>
{children}
</div>
);
};
export default memo(LoginSocialApple);