SIWN: React
In this guide, we’ll take a look at how to implement sign-in with neynar in a React app. For this guide, I am going to be using next.js but the same would work for CRA, remix, or anything based on react!
For this guide, we’ll go over:
- Setting up auth using the Neynar React SDK
- Using the signer to create casts
Before we begin, you can access the complete source code for this guide on GitHub.
Let’s get started!
Creating the app
Setting up the project
Create a new next.js app using the following command:
You can choose the configuration based on your personal preference, I am using this config for the guide:
Once the app is created, install the packages that we are going to need for the command:
Install peer dependencies for @neynar/react
Once the dependencies are installed you can open it in your favourite and we can start working on adding sign-in with neynar!
Adding the sign-in button
Head over to the layout.tsx
file and wrap your app in a NeynarContextProvider
like this:
We are passing some settings here like clientId
, defaultTheme
and eventsCallbacks
.
clientId
: This is going to be the client ID you get from your neynar, add it to your.env.local
file asNEXT_PUBLIC_NEYNAR_CLIENT_ID
.
Make sure to add localhost to the authorized origins
defaultTheme
: default theme lets you change the theme of your sign-in button, currently, we have only light mode but dark mode is going to be live soon.eventsCallbacks
: This allows you to perform certain actions when the user signs out or auth is successful.
I’ve also added a styles import from the neynar react package here which is needed for the styles of the sign-in button.
Finally, let’s add the sign-in button in the page.tsx
file like this:
If you head over to your app you’ll be able to see a sign-in button on the screen. Go ahead and try signing in!
Now that our sign-in button is working let’s use the signer we get to publish casts!
Using the signer UUID to publish casts
In the page.tsx
file access the user data using the useNeynarContext
hook like this:
The user object contains various information like the username, fid, display name, pfp url, signer uuid, etc.
Then, we can use this user object to check whether the user has signed in and display a screen conditionally like this:
Now, let’s add a text area and a cast button here like this:
This will give you something like this:
But as you can see we have used a handlePublishCast
function but we have not yet created it. So, let’s create that and also add the text
useState that we are using in the textarea:
This creates an api call to a /api/cast
route with the signer uuid and text.
Finally, we need to create the api route which will create casts. Create a new file api/cast/route.ts
in the app
folder and add the following:
Make sure to set the NEYNAR_API_KEY variable in your .env.local file
Now, you can go ahead and try making a cast from the website after connecting your Farcaster account!
Conclusion
This guide taught us how to add sign-in with neynar to a React app, check out the GitHub repository if you want to look at the full code.
Lastly, make sure to share what you built with us on Farcaster by tagging @neynar and if you have any questions, reach out to us on warpcast or Telegram!