Skip to content

Commit

Permalink
remove query invalidation, add user id to query key
Browse files Browse the repository at this point in the history
  • Loading branch information
kahkeng committed Jul 15, 2023
1 parent cabc3c0 commit d1f85f4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
11 changes: 8 additions & 3 deletions src/api/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useQuery } from 'react-query';
import { fetchChats, fetchShareSettings } from '@/api/fetches';
import { useSession } from 'next-auth/react';

export interface ChatSession {
id: string;
Expand All @@ -9,7 +10,9 @@ export interface Chats {
sessions: ChatSession[] | undefined;
}
export const useQueryChats = () => {
const { data: chats, ...rest } = useQuery<Chats>(['chats'], async () => fetchChats());
const { data: sessionData } = useSession();
const userId = sessionData?.user?.name || '';
const { data: chats, ...rest } = useQuery<Chats>(['chats', userId], async () => fetchChats());
return { chats, ...rest };
};

Expand All @@ -18,8 +21,10 @@ export interface ChatShareSettings {
canEdit?: boolean;
}
export const useQueryShareSettings = (sessionId: string) => {
const { data, ...rest } = useQuery(['shareSettings', sessionId], async () =>
const { data: sessionData } = useSession();
const userId = sessionData?.user?.name || '';
const { data: settings, ...rest } = useQuery<ChatShareSettings>(['shareSettings', sessionId, userId], async () =>
fetchShareSettings(sessionId)
);
return { settings: data as ChatShareSettings, ...rest };
return { settings, ...rest };
};
10 changes: 0 additions & 10 deletions src/contexts/ConnectionWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ const ConnectionWrapper = ({ children, useSiwe = true }: any) => {
return nonce;
};

const invalidateQueries = () => {
// set timeout to allow cookie response to first be set prior to refetching
setTimeout(() => {
queryClient.invalidateQueries({ queryKey: ['chats'] });
queryClient.invalidateQueries({ queryKey: ['shareSettings'] });
}, 1000);
};

const getSigninCallback = async (message: string, signature: string) => {
const backendUrl = getBackendApiUrl();
const result = await axios.post(
Expand All @@ -94,14 +86,12 @@ const ConnectionWrapper = ({ children, useSiwe = true }: any) => {
},
{ withCredentials: true }
);
invalidateQueries();
return !!result.data;
};

const getSignoutCallback = async () => {
const backendUrl = getBackendApiUrl();
await axios.post(`${backendUrl}/logout`, {}, { withCredentials: true });
invalidateQueries();
};

const CustomAvatar: AvatarComponent = ({
Expand Down
11 changes: 6 additions & 5 deletions src/utils/rainbowSIWEmod/RainbowKitSiweNextAuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ export function RainbowKitSiweNextAuthProvider({
const { address: account } = useAccount();

const signoutSequence = async () => {
// signout on frontend first, so that we don't end up in situation
// where frontend is signed in but backend is signed out, which will
// be confusing to the user
await signOut({ redirect: false });
// signout on backend first, to ensure session cookies get cleared
// prior to any frontend hooks firing
if (getSignoutCallback) {
await getSignoutCallback();
}
await signOut({ redirect: false });
};

/* force logout if account changes */
Expand Down Expand Up @@ -99,7 +98,9 @@ export function RainbowKitSiweNextAuthProvider({
verify: async ({ message, signature }) => {
const messageJson = JSON.stringify(message);
// signin on backend first, so that any issues there will not lead to
// an inconsistent signin state with frontend
// an inconsistent signin state with frontend. also, this ensures
// session cookies are set by backend prior to any frontend hooks
// firing
if (getSigninCallback) {
const result = await getSigninCallback(messageJson, signature);
if (!result) {
Expand Down

0 comments on commit d1f85f4

Please sign in to comment.