0.1.3 • Published 2 years ago
@marcotommoro/react-ethereum-auth-hook v0.1.3
*** DEVELOPMENT IN PROGRESS ***
*** Please contact me for bugs ***
Next / React Ethereum Auth Hook
Implements metamask functions:
- connect wallet
- get address
- change network
Implements web3-auth function from library
- get signed token
Install
npm i @marcotommoro/react-ethereum-auth-hook
Add to project
- add EthereumContextProvider to
_app.tsx
: | param | type | default | :---: | :---: | :---: | |autoCheckCorrectNetwork
auto check that the network is always the same as the one specified in the .env (or 0x1 by default) | boolean | true| | | |
import { EthereumContextProvider } from "@marcotommoro/react-ethereum-auth-hook";
function MyApp({ Component, pageProps }: AppProps) {
return (
<EthereumContextProvider autoCheckCorrectNetwork={true}>
...
<Component {...pageProps} />
...
</EthereumContextProvider>
);
}
useEthereum in a react/next component:
const { address, currentNetwork, isMetamaskInstalled, isNetworkCorrect, connect, correctNetwork, getToken, } = useEthereum();
(optional) set env
NEXT_PUBLIC_NETWORK_ID
with your testnet id. Default Ethereum mainnet0x1
. Check networks id on Metamask documentation
Instructions
key | value |
---|---|
address | Current metamask address selected. |
currentNetwork | Current network selected. |
isMetamaskInstalled | Check if window.ehtereum is present. |
isNetworkCorrect | Check if is the correct network. If process.env.NEXT_PUBLIC_NETWORK_ID is not present, the default network is Ethereum mainnet ('0x1 ') |
connect | Ask user to connect your beautiful react app with metamask extension. |
correctNetwork | Ask user permission to switch to the right network. |
getToken | Ask user to sign a transaction and get a signed token to use in a backend server to validate the user is really him/her/it. |