Hello WebApp
In this guide we will show you how to quickly spin up a frontend where users can login using their wallets and interact with a contract.
If you already have an application and want to integrate NEAR into it, we recommend you to first go through this guide and then check our documentation on integrating NEAR to a frontend
Create NEAR App
If you already have Node.js installed, simply run:
npx create-near-app@latest
Use the interactive menu to set up:
A Web App.NextJs (Classic).
While you can use our app with any package manager, we recommend you to skip the installation step and manually install the dependencies using pnpm i.
Once the folder is ready - and all dependencies installed - you can start the development server using pnpm.
pnpm dev
Visit http://localhost:3000 in your browser to view the dApp. Note that since the dApp uses NextJS the app might take longer to load the pages on first visit.
The app is not starting?
Make sure you are using node >= v18, you can easily switch versions using nvm use 18
Landing Page
Once the app starts you will see the landing page, rendering a navigation bar that allows users to login using their NEAR wallet. You can then navigate to the docs or the Near Integration page (which we will do).
Landing page of Hello NEAR Gateway
Go ahead and sign in with your NEAR account. If you don't have one, you can create one on the fly.
Under the Hood
Next.js uses a template system, where each page is a React component.
Our app's template is defined at ./src/pages/_app.js. It does two things:
- Initializes a wallet selector, and stores it in context so other components can access it later.
- Renders the navigation menu and the page's content.
Loading...
When initializing the wallet you can choose to create a function call access key for a specific contract for the application to use. This allows the application to sign non-payable methods on behalf of the user so they are not required to manually sign each transaction.
const wallet = new Wallet({
createAccessKeyFor: HelloNearContract,
networkId: NetworkId,
});
This example additionally includes the option to login with Metamask and other EVM wallets. Further information on how to add EVM wallets to your application can be found in the Ethereum Wallets on NEAR documentation.
What is the wallet selector?
The wallet selector is a modal that allows users to select their preferred Near wallet to login. Our application creates a new instance of the wallet selector then stores it in the apps context so it can be accessed by other components.
Navigation Bar & Login
The navigation bar implements buttons to login and logout users with their Near wallet.
The code for the navigation bar can be found at ./src/components/navigation.js. The login and logout buttons are implemented by using the signIn and signOut methods from the wallet selector previously initialized:
Loading...