Skip to content

Commit

Permalink
Adding an option to enable auto-logout when the wallet is disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
liarco committed Mar 27, 2024
1 parent 38b1743 commit 2f609b7
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/components/Web3RouteGuard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import { useEffect, PropsWithChildren, ReactNode } from 'react';
import { useEffect, PropsWithChildren, ReactNode, useCallback } from 'react';
import { useAccountEffect } from 'wagmi';

import { useWeb3AuthToken } from '../hooks/useWeb3AuthToken';

Expand All @@ -8,21 +9,33 @@ type Web3RouteGuardProps = {
loginSearchParams?: string[][];
token: Parameters<typeof useWeb3AuthToken>[0];
fallback?: ReactNode | undefined;
logoutOnDisconnect?: boolean;
};

export const Web3RouteGuard = ({
loginRoute,
loginSearchParams = [],
token: tokenOptions,
fallback,
logoutOnDisconnect = false,
children,
}: PropsWithChildren<Web3RouteGuardProps>) => {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
const { isVerifying, isAuthenticated } = useWeb3AuthToken(tokenOptions);
const { isVerifying, isAuthenticated, clear } = useWeb3AuthToken(tokenOptions);
const currentUrl = `${pathname}?${searchParams}`;

useAccountEffect({
onDisconnect: useCallback(() => {
if (!logoutOnDisconnect) {
return;
}

clear();
}, [logoutOnDisconnect, clear]),
});

useEffect(() => {
if (isAuthenticated || isVerifying) {
return;
Expand Down

0 comments on commit 2f609b7

Please sign in to comment.