Refactor sign out and auth change listener hooks
In the use-sign-out.ts file, the use of query client has been removed and error handling has been improved during sign-out operations. Meanwhile, in the use-auth-change-listener.ts file, an unnecessary usage of query client and router has been removed. Event handling logic has also been simplified by using window.location.reload() instead of router.refresh() on user sign-out.
This commit is contained in:
@@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
import { usePathname, useRouter } from 'next/navigation';
|
import { usePathname } from 'next/navigation';
|
||||||
|
|
||||||
import { useQueryClient } from '@tanstack/react-query';
|
|
||||||
|
|
||||||
import { useSupabase } from './use-supabase';
|
import { useSupabase } from './use-supabase';
|
||||||
|
|
||||||
@@ -28,12 +26,10 @@ export function useAuthChangeListener({
|
|||||||
}) {
|
}) {
|
||||||
const client = useSupabase();
|
const client = useSupabase();
|
||||||
const pathName = usePathname();
|
const pathName = usePathname();
|
||||||
const router = useRouter();
|
|
||||||
const queryClient = useQueryClient();
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// keep this running for the whole session unless the component was unmounted
|
// keep this running for the whole session unless the component was unmounted
|
||||||
const listener = client.auth.onAuthStateChange(async (event, user) => {
|
const listener = client.auth.onAuthStateChange((event, user) => {
|
||||||
// log user out if user is falsy
|
// log user out if user is falsy
|
||||||
// and if the current path is a private route
|
// and if the current path is a private route
|
||||||
const shouldRedirectUser =
|
const shouldRedirectUser =
|
||||||
@@ -48,22 +44,13 @@ export function useAuthChangeListener({
|
|||||||
|
|
||||||
// revalidate user session when user signs in or out
|
// revalidate user session when user signs in or out
|
||||||
if (event === 'SIGNED_OUT') {
|
if (event === 'SIGNED_OUT') {
|
||||||
await queryClient.invalidateQueries();
|
window.location.reload();
|
||||||
|
|
||||||
return router.refresh();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// destroy listener on un-mounts
|
// destroy listener on un-mounts
|
||||||
return () => listener.data.subscription.unsubscribe();
|
return () => listener.data.subscription.unsubscribe();
|
||||||
}, [
|
}, [client.auth, pathName, appHomePath, privatePathPrefixes]);
|
||||||
client.auth,
|
|
||||||
router,
|
|
||||||
pathName,
|
|
||||||
appHomePath,
|
|
||||||
privatePathPrefixes,
|
|
||||||
queryClient,
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
import { useMutation } from '@tanstack/react-query';
|
||||||
|
|
||||||
import { useSupabase } from './use-supabase';
|
import { useSupabase } from './use-supabase';
|
||||||
|
|
||||||
export function useSignOut() {
|
export function useSignOut() {
|
||||||
const client = useSupabase();
|
const client = useSupabase();
|
||||||
const queryClient = useQueryClient();
|
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: async () => {
|
mutationFn: () => {
|
||||||
await client.auth.signOut();
|
return client.auth.signOut();
|
||||||
await queryClient.invalidateQueries();
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user