diff --git a/src/app/components/EmailNotificationSettingsDialog/EmailNotificationSettingsDialog.tsx b/src/app/components/EmailNotificationSettingsDialog/EmailNotificationSettingsDialog.tsx index 53801c021..6761d8016 100644 --- a/src/app/components/EmailNotificationSettingsDialog/EmailNotificationSettingsDialog.tsx +++ b/src/app/components/EmailNotificationSettingsDialog/EmailNotificationSettingsDialog.tsx @@ -7,7 +7,7 @@ import { FormGroup } from 'app/components/Form/FormGroup'; import { DialogButton } from 'app/components/Form/DialogButton'; import axios from 'axios'; import { useAccount } from 'app/hooks/useAccount'; -import { parseJwt, validateEmail } from 'utils/helpers'; +import { validateEmail, validateJwt } from 'utils/helpers'; import { walletService } from '@sovryn/react-wallet'; import { currentChainId, @@ -162,29 +162,32 @@ const EmailNotificationSettingsDialogComponent: React.FC axios - .post(`${userEndpoint}${alreadyUser ? 'auth' : 'register'}`, { + .post(`${userEndpoint}auth`, { signedMessage, message, walletAddress: account, - ...(alreadyUser - ? '' - : { subscriptions: defaultSubscriptionsArray }), + // create default subscriptions for new users + subscriptions: defaultSubscriptionsArray, }) .then(res => { if (res.data?.token) { setNotificationToken(res.data.token); setNotificationWallet(account); + storeToken(account, res.data.token); } }), ) @@ -258,37 +261,36 @@ const EmailNotificationSettingsDialogComponent: React.FC { if (!account || !notificationToken) { return; } - const userId = parseJwt(notificationToken)?.sub; - if (!userId) { + if (!validateJwt(notificationToken)) { + getToken(); return; } setLoading(true); if (email.length === 0) { - const promise = axios.delete(`${userEndpoint}${userId}`, { + const promise = axios.delete(userEndpoint, { headers: { Authorization: 'bearer ' + notificationToken, }, @@ -297,7 +299,7 @@ const EmailNotificationSettingsDialogComponent: React.FC { @@ -423,3 +426,11 @@ export const EmailNotificationSettingsDialog: React.FC ); + +function getStoredToken(account: string) { + return localStorage.getItem(`notification-token-${account}`); +} + +function storeToken(account: string, token: string) { + localStorage.setItem(`notification-token-${account}`, token); +} diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index fd7c1cb1e..aba5af395 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -271,6 +271,12 @@ export const parseJwt = (token: string) => { return JSON.parse(jsonPayload); }; +export const validateJwt = (token: string) => { + const parsedToken = parseJwt(token); + const now = Date.now() / 1000; + return parsedToken?.exp > now; +}; + export const calculateAssetValue = ( asset: Asset, amount: string, diff --git a/yarn.lock b/yarn.lock index 86d800525..f0e56683b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5196,10 +5196,10 @@ node-fetch "^2.6.6" web3 "^1.6.0" -"@sovryn/react-wallet@2.2.20": - version "2.2.20" - resolved "https://registry.yarnpkg.com/@sovryn/react-wallet/-/react-wallet-2.2.20.tgz#80e80d1b8907d7f38261b52e91b28bb52bff8caf" - integrity sha512-EN6jR8vnwrxe8dvtrQ5FBD/4Rdk/HEze0U9JwnXZojJCG5+YzQbPW5sdeS86213eLMwIieWJKPQRBHzhkLnLGA== +"@sovryn/react-wallet@2.2.21": + version "2.2.21" + resolved "https://registry.yarnpkg.com/@sovryn/react-wallet/-/react-wallet-2.2.21.tgz#0bea3bcb5f43678a6b086b8691f0944eae5bbda1" + integrity sha512-gD5SErk0HKEgAIj37EjsdC+cRNt7vidAQ48wSH6Nzm0Dj2GR3NSRYEh7xy0OvWOoCLpRxyaSJ/WbFauMURSdCg== dependencies: "@blueprintjs/core" "^3.44.2" "@hookstate/core" "^3.0.6"