Skip to content

Commit

Permalink
SOV-2787: access to notification service (#2574)
Browse files Browse the repository at this point in the history
fix: access to notification service
  • Loading branch information
creed-victor authored Sep 29, 2023
1 parent a8ad150 commit ec93f36
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -162,29 +162,32 @@ const EmailNotificationSettingsDialogComponent: React.FC<IEmailNotificationSetti
return;
}

const storedToken = getStoredToken(account);
if (storedToken && validateJwt(storedToken)) {
setNotificationToken(storedToken);
setNotificationWallet(account);
return;
}

const timestamp = new Date();
const message = `Login to Sovryn on: ${timestamp}`;

const { data: alreadyUser } = await axios.get(
`${userEndpoint}isUser/${account}`,
);

return walletService
.signMessage(message)
.then(signedMessage =>
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);
}
}),
)
Expand Down Expand Up @@ -258,37 +261,36 @@ const EmailNotificationSettingsDialogComponent: React.FC<IEmailNotificationSetti
return;
}

const userId = parseJwt(notificationToken)?.sub;

if (!userId) {
if (!validateJwt(notificationToken)) {
getToken();
return;
}

setLoading(true);

const promise = axios.get(`${userEndpoint}${userId}`, {
const promise = axios.get(userEndpoint, {
headers: {
Authorization: 'bearer ' + notificationToken,
},
});

handleUserDataResponse(promise);
}, [handleUserDataResponse, account, notificationToken]);
}, [account, notificationToken, handleUserDataResponse, getToken]);

const updateUser = useCallback(() => {
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,
},
Expand All @@ -297,7 +299,7 @@ const EmailNotificationSettingsDialogComponent: React.FC<IEmailNotificationSetti
handleUserDelete(promise);
} else {
const promise = axios.put(
`${userEndpoint}${account}`,
userEndpoint,
{
walletAddress: account,
email: email || undefined,
Expand All @@ -314,11 +316,12 @@ const EmailNotificationSettingsDialogComponent: React.FC<IEmailNotificationSetti
}
}, [
account,
email,
handleUserDataResponse,
notificationToken,
subscriptions,
email,
getToken,
handleUserDelete,
subscriptions,
handleUserDataResponse,
]);

useEffect(() => {
Expand Down Expand Up @@ -423,3 +426,11 @@ export const EmailNotificationSettingsDialog: React.FC<IEmailNotificationSetting
/>
</EmailNotificationSettingsContextProvider>
);

function getStoredToken(account: string) {
return localStorage.getItem(`notification-token-${account}`);
}

function storeToken(account: string, token: string) {
localStorage.setItem(`notification-token-${account}`, token);
}
6 changes: 6 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5196,10 +5196,10 @@
node-fetch "^2.6.6"
web3 "^1.6.0"

"@sovryn/[email protected].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/[email protected].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"
Expand Down

0 comments on commit ec93f36

Please sign in to comment.