|
| 1 | +/** |
| 2 | + * Copyright 2023 Shift Crypto AG |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { ChangeEvent, Dispatch } from 'react'; |
| 18 | +import { useTranslation } from 'react-i18next'; |
| 19 | +import { Toggle } from '../../../../components/toggle/toggle'; |
| 20 | +import { SettingsItem } from '../settingsItem/settingsItem'; |
| 21 | +import { TBackendConfig, TConfig } from '../../advanced-settings'; |
| 22 | +import { setConfig } from '../../../../utils/config'; |
| 23 | +import { TAuthEventObject, subscribeAuth, forceAuth } from '../../../../api/backend'; |
| 24 | +import { runningInAndroid } from '../../../../utils/env'; |
| 25 | + |
| 26 | +type TProps = { |
| 27 | + backendConfig?: TBackendConfig; |
| 28 | + onChangeConfig: Dispatch<TConfig>; |
| 29 | +} |
| 30 | + |
| 31 | +export const EnableAuthSetting = ({ backendConfig, onChangeConfig }: TProps) => { |
| 32 | + const { t } = useTranslation(); |
| 33 | + |
| 34 | + const handleToggleAuth = async (e: ChangeEvent<HTMLInputElement>) => { |
| 35 | + // Before updating the config we need the user to authenticate. |
| 36 | + // The forceAuth is needed to force the backend to execute the |
| 37 | + // authentication even if the auth config is disabled. |
| 38 | + const unsubscribe = subscribeAuth((data: TAuthEventObject) => { |
| 39 | + if (data.typ === 'auth-ok') { |
| 40 | + updateConfig(!e.target.checked); |
| 41 | + unsubscribe(); |
| 42 | + } |
| 43 | + }); |
| 44 | + forceAuth(); |
| 45 | + }; |
| 46 | + |
| 47 | + const updateConfig = async (auth: boolean) => { |
| 48 | + const config = await setConfig({ |
| 49 | + backend: { authentication: auth }, |
| 50 | + }) as TConfig; |
| 51 | + onChangeConfig(config); |
| 52 | + }; |
| 53 | + |
| 54 | + if (!runningInAndroid()) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <SettingsItem |
| 60 | + settingName={t('newSettings.advancedSettings.authentication.title')} |
| 61 | + secondaryText={t('newSettings.advancedSettings.authentication.description')} |
| 62 | + extraComponent={ |
| 63 | + backendConfig !== undefined ? |
| 64 | + <Toggle |
| 65 | + checked={backendConfig?.authentication || false} |
| 66 | + onChange={handleToggleAuth} |
| 67 | + /> |
| 68 | + : |
| 69 | + null |
| 70 | + } |
| 71 | + /> |
| 72 | + ); |
| 73 | +}; |
0 commit comments