Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SIWE sign out #6

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stale-coats-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@valorem-labs-inc/react-hooks': patch
---

fix: SIWE provider sign out
3 changes: 3 additions & 0 deletions src/context/SIWEProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const siweQueryProps = {
refetchOnWindowFocus: false,
refetchOnMount: false,
refetchOnReconnect: false,
keepPreviousData: false,
cacheTime: 0,
staleTime: 1,
};

export function SIWEProvider({ onSignIn, onSignOut, children }: SIWEProps) {
Expand Down
49 changes: 41 additions & 8 deletions src/utils/siwe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,47 @@ export const getSIWEConfig = ({
},
async signOut() {
logger.debug('Signing out...');
await signOutQuery.refetch();
await nonceQuery.refetch();
try {
await signOutQuery.refetch();
queryClient.setQueryData(['valorem.trade.v1.Auth', 'Nonce'], undefined);
queryClient.setQueryData(
['valorem.trade.v1.Auth', 'Session'],
undefined,
);
queryClient.setQueryData(
['valorem.trade.v1.Auth', 'Authenticate'],
undefined,
);
queryClient.setQueryData(['valorem.trade.v1.Auth', 'signed-out'], true);
logger.info('Signed out');
return true;
} catch (error) {
logger.error('Error signing out');
return false;
}
},
async getSession() {
logger.debug('Getting session...');
await new Promise((resolve) => {
setTimeout(resolve, 750);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nickadamson can we reduce this to 250ms, should cover global round trip ping and set, and feel "real time" to the user.

});
if (
queryClient.getQueryData(['valorem.trade.v1.Auth', 'signed-out']) ===
true
) {
logger.debug('User is signed out');
queryClient.setQueryData(
['valorem.trade.v1.Auth', 'signed-out'],
false,
);
return null;
}
queryClient.setQueryData(['valorem.trade.v1.Auth', 'Nonce'], undefined);
queryClient.setQueryData(['valorem.trade.v1.Auth', 'Session'], undefined);
queryClient.setQueryData(
['valorem.trade.v1.Auth', 'Authenticate'],
undefined,
);
logger.info('Signed out');
return true;
},
async getSession() {
logger.debug('Getting session...');

// check auth endpoint to ensure session is valid
const { data: authData } = await authenticateQuery.refetch({});
Expand All @@ -90,6 +118,7 @@ export const getSIWEConfig = ({
const authorizedAddress = fromH160ToAddress(authData);
if (authorizedAddress.toLowerCase() !== address?.toLowerCase()) {
logger.error('Authorized address does not match connected address');
return null;
}
// get session data
const { data: sessionData } = await sessionQuery.refetch();
Expand All @@ -98,8 +127,12 @@ export const getSIWEConfig = ({
return null;
}
const sessionAddress = fromH160ToAddress(sessionData.address);
if (sessionAddress.toLowerCase() === address?.toLowerCase()) {
if (sessionAddress.toLowerCase() === address.toLowerCase()) {
logger.debug('Session is valid');
queryClient.setQueryData(
['valorem.trade.v1.Auth', 'signed-out'],
false,
);
return {
address: sessionAddress,
chainId: Number(fromH256(sessionData.chainId).toString()),
Expand Down