Skip to content

Commit

Permalink
Implement automatic Safe login via custom hook
Browse files Browse the repository at this point in the history
  • Loading branch information
snoopy1412 committed May 25, 2024
1 parent e8eefd7 commit 8388d55
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts

/.vscode
/.vscode
certificates
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev --turbo",
"dev": "next dev --turbo --experimental-https",
"build": "next build",
"start": "next start",
"lint": "next lint"
Expand Down
2 changes: 2 additions & 0 deletions src/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import Image from 'next/image';
import Link from 'next/link';

import ConnectButton from '@/components/connect-button';
import { useAppAutoConnect } from '@/hooks/useAppAutoConnect';

const Header = () => {
useAppAutoConnect();
return (
<header className="h-12 w-full md:h-[3.5rem]">
<div className="mx-auto flex h-full w-full items-center justify-between px-8">
Expand Down
34 changes: 34 additions & 0 deletions src/hooks/useAppAutoConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useConnect, useReconnect } from 'wagmi';
import { useEffect } from 'react';
import { Connector } from 'wagmi';

interface AppConnector extends Connector {
rkDetails?: {
installed: boolean;
isRainbowKitConnector: boolean;
};
}

function useAppAutoConnect() {
const { connect, connectors } = useConnect();
const { reconnect } = useReconnect();

useEffect(() => {
if (window.self !== window.top) {
const appConnector = connectors.find(
(connector: AppConnector) =>
connector.id === 'safe' &&
connector.rkDetails?.installed &&
connector.rkDetails?.isRainbowKitConnector
) as AppConnector;

if (appConnector) {
connect({ connector: appConnector });
return;
}
}
reconnect();
}, [connect, connectors, reconnect]);
}

export { useAppAutoConnect };
2 changes: 1 addition & 1 deletion src/providers/web3-app-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const queryClient = new QueryClient();

export function Web3AppProvider({ children }: React.PropsWithChildren<{}>) {
return (
<WagmiProvider config={config}>
<WagmiProvider config={config} reconnectOnMount={false}>
<QueryClientProvider client={queryClient}>
<RainbowKitProvider>{children}</RainbowKitProvider>
</QueryClientProvider>
Expand Down

0 comments on commit 8388d55

Please sign in to comment.