|
| 1 | +import { Accounts } from 'meteor/accounts-base'; |
| 2 | +import React, { FC, useCallback, useEffect, useLayoutEffect, useRef } from 'react'; |
| 3 | + |
| 4 | +import { useAbsoluteUrl } from '../../../contexts/ServerContext'; |
| 5 | +import { useSetting } from '../../../contexts/SettingsContext'; |
| 6 | + |
| 7 | +export const AppleOauthButton: FC = () => { |
| 8 | + const enabled = useSetting('Accounts_OAuth_Apple'); |
| 9 | + const absoluteUrl = useAbsoluteUrl(); |
| 10 | + const appleClientID = useSetting('Accounts_OAuth_Apple_id'); |
| 11 | + |
| 12 | + const redirectURI = absoluteUrl('_oauth/apple'); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + const success = (data: any): void => { |
| 16 | + const { authorization, user } = data.detail; |
| 17 | + |
| 18 | + Accounts.callLoginMethod({ |
| 19 | + methodArguments: [ |
| 20 | + { |
| 21 | + serviceName: 'apple', |
| 22 | + identityToken: authorization.id_token, |
| 23 | + ...(user && { |
| 24 | + fullName: { |
| 25 | + givenName: user.name.firstName, |
| 26 | + familyName: user.name.lastName, |
| 27 | + }, |
| 28 | + email: user.email, |
| 29 | + }), |
| 30 | + }, |
| 31 | + ], |
| 32 | + userCallback: console.log, |
| 33 | + }); |
| 34 | + }; |
| 35 | + |
| 36 | + const error = (error: any): void => { |
| 37 | + // handle error. |
| 38 | + console.error(error); |
| 39 | + }; |
| 40 | + document.addEventListener('AppleIDSignInOnSuccess', success); |
| 41 | + // Listen for authorization failures |
| 42 | + document.addEventListener('AppleIDSignInOnFailure', error); |
| 43 | + |
| 44 | + return (): void => { |
| 45 | + document.removeEventListener('AppleIDSignInOnSuccess', success); |
| 46 | + document.removeEventListener('AppleIDSignInOnFailure', error); |
| 47 | + }; |
| 48 | + }, []); |
| 49 | + |
| 50 | + const scriptLoadedHandler = useCallback(() => { |
| 51 | + if (!enabled) { |
| 52 | + return; |
| 53 | + } |
| 54 | + (window as any).AppleID.auth.init({ |
| 55 | + clientId: appleClientID, |
| 56 | + scope: 'name email', |
| 57 | + redirectURI, |
| 58 | + usePopup: true, |
| 59 | + }); |
| 60 | + }, [enabled, appleClientID, redirectURI]); |
| 61 | + |
| 62 | + const ref = useRef<HTMLScriptElement>(); |
| 63 | + |
| 64 | + useEffect(() => { |
| 65 | + if ((window as any).AppleID) { |
| 66 | + scriptLoadedHandler(); |
| 67 | + return; |
| 68 | + } |
| 69 | + if (!ref.current) { |
| 70 | + return; |
| 71 | + } |
| 72 | + ref.current.onload = scriptLoadedHandler; |
| 73 | + }, [scriptLoadedHandler]); |
| 74 | + |
| 75 | + useLayoutEffect(() => { |
| 76 | + const script = document.createElement('script'); |
| 77 | + script.src = 'https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js'; |
| 78 | + script.async = true; |
| 79 | + ref.current = script; |
| 80 | + document.body.appendChild(script); |
| 81 | + return (): void => { |
| 82 | + document.body.removeChild(script); |
| 83 | + }; |
| 84 | + }, []); |
| 85 | + |
| 86 | + if (!enabled) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + return <div id='appleid-signin' data-height='40px'></div>; |
| 91 | +}; |
| 92 | + |
| 93 | +export default AppleOauthButton; |
0 commit comments