Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Implement automatic Safe login via custom hook #17

Merged
merged 1 commit into from
May 25, 2024
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
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
Loading