This section installs SSX as a dependency and implements Sign-In with Ethereum
A completed version of this part can be found in the example repository (01_sign_in).
In this step, we will see how to connect SSX with your application and use the UserAuthorization module to identify your user. In this quickstart, we will show how to authenticate your user on both the client and server.
To start, you need to install SSX on both your frontend and backend:
Connecting SSX to the Backend
To use the SSX library on the backend, you must install @spruceid/ssx-server. Furthermore, we must create the routes required by the authorization module (nonce, sign in, and sign out) for our Next app. To add those, use the following commands:
npmi@spruceid/ssx-server
You can create the folders and files by yourself or by running the bash command below:
Then, add an environment variable into the my-app/.env.local file:
SSX_SIGNING_KEY=anythingyouwanthere
Add a helper file to our code so that we don't have to instantiate a new SSX Server object directly every time. Add the following to my-app/app/api/_ssx.ts file:
Now, create an API route for generating a random nonce. This is used to identify the session and prevent replay attacks. Add the following to my-app/app/api/ssx-nonce/route.ts file:
Add a sign-in API route. This route will do all necessary checks in the SIWE message, ensuring it's valid. All checks occur inside the ssx.login() function. Add the following to my-app/app/api/ssx-login/route.ts file:
To use the SSX library on the frontend you must install @spruceid/ssx. Furthermore, we need to create a component to create the authorization module logic. To add it, use the following commands:
npmi@spruceid/ssx
You can create the folders and files by yourself or by running the bash command below:
mkdircomponentstouchcomponents/SSXComponent.tsx
Let's update some CSS by adding the following to my-app/app/globals.css file:
This SSX configuration assumes you are running the Next.js project using the default port (3000). If you are running in a different port, change the providers.server.host in the SSX config.