From b4a769e15fb18d854af56c24e39b0b72fff273a0 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 27 Aug 2022 12:51:32 -0700 Subject: Support biometric authentication --- modern/src/common/components/NativeInterface.js | 33 ++++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'modern/src/common/components/NativeInterface.js') diff --git a/modern/src/common/components/NativeInterface.js b/modern/src/common/components/NativeInterface.js index 4e6ad4fe..d43b678c 100644 --- a/modern/src/common/components/NativeInterface.js +++ b/modern/src/common/components/NativeInterface.js @@ -14,37 +14,40 @@ export const nativePostMessage = (message) => { } }; -const listeners = new Set(); +export const handleLoginTokenListeners = new Set(); +window.handleLoginToken = (token) => { + handleLoginTokenListeners.forEach((listener) => listener(token)); +}; + +const updateNotificationTokenListeners = new Set(); window.updateNotificationToken = (token) => { - listeners.forEach((listener) => listener(token)); + updateNotificationTokenListeners.forEach((listener) => listener(token)); }; const NativeInterface = () => { const dispatch = useDispatch(); const user = useSelector((state) => state.session.user); - const [token, setToken] = useState(null); + const [notificationToken, setNotificationToken] = useState(null); useEffect(() => { - const listener = (token) => setToken(token); - listeners.add(listener); - return () => { - listeners.delete(listener); - }; - }, [setToken]); + const listener = (token) => setNotificationToken(token); + updateNotificationTokenListeners.add(listener); + return () => updateNotificationTokenListeners.delete(listener); + }, [setNotificationToken]); useEffectAsync(async () => { - if (user && token) { - window.localStorage.setItem('notificationToken', token); - setToken(null); + if (user && notificationToken) { + window.localStorage.setItem('notificationToken', notificationToken); + setNotificationToken(null); const tokens = user.attributes.notificationTokens?.split(',') || []; - if (!tokens.includes(token)) { + if (!tokens.includes(notificationToken)) { const updatedUser = { ...user, attributes: { ...user.attributes, - notificationTokens: [...tokens.slice(-2), token].join(','), + notificationTokens: [...tokens.slice(-2), notificationToken].join(','), }, }; @@ -61,7 +64,7 @@ const NativeInterface = () => { } } } - }, [user, token, setToken]); + }, [user, notificationToken, setNotificationToken]); return null; }; -- cgit v1.2.3