This quick start guide will walk you through how to quickly get setup with the Passport SDK with Clerky account, register and authenticate users, and start signing messages and transactions. You can skip to the Complete Setup, and then follow the section step-by-step where each code snippet will be explained. You can also quickly pull the completed repository here https://github.com/0xpass/passport-clerk-quickstart and follow along.
Project setup
First we'll setup a Next.js project, with tailwind, we can do this with the following command, and walk through the configuration wizard, making sure to chose the app-router, and tailwind.
npxcreate-next-app@latest
Setup and Dependencies
Firstly, we'll ensure we have all the requires dependencies. So we're going to install the Passport SDK @0xpass/passport to interact with Passport protocol, and @clerk/nextjs for third party authentication. We'll also install some helper packages @0xpass/key-signer to handle our signature management for delegated registration and authentication.
You should grab your own NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY, and CLERK_SECRET_KEY from your Clerk account.
For Developer Key (PRIVATE_KEY), follow Generating DOA Keys for generating your own keys. We'll refer to the private key generated in this step as PRIVATE_KEY environment variable in the example below.
Backend APIs
Firstly let's setup backend API for creating an account in passport protocol upon successful authentication via Clerk.
Authentication Flow:
Signing Flow:
This is not a part of backend, but you need a page that takes the callback from clerk, and redirects to /api/user-callback. In app/auth/callback we'll have the following snippet so that frontend calls the backend endpoint with user id and email address upon successful sign-in.
We'll create a file at:app/api/user-callback/route.ts Which will create a Passport instance with KeySigner with your private key. PRIVATE_KEY environment variable can point to a file path or the actual private key. Note that the private key should only be handled in your backend, and can't be exposed publicly.
After authenticating with Clerk, you can directly interact with Passport with the username (email from Clerk) and your developer keys.
Now we'll implement get-accountendpioint at app/api/get-account/route.ts. For retrieving your user's email address you can use currentUser() function which will grab user information from the session established with Clerk.
First we'll need to make sure we wrap our application inside the ClerkProvider . We can do this by creating a new file providers.tsx inside the src directory, and populating it as below.
Now with all of this setup we can setup our UI too, so the overall setup looks as below. In the final part of our setup, we've created the UI layer to interact with the above functions. We have buttons to register and authenticate, which dynamically interact with the state, to show loading states, as well as authenticated states, and finally when a message is signed, the user is also able to see the signed message signature.
Please refer to https://github.com/0xpass/passport-clerk-quickstart for the complete setup.
You should be able to clone, update environment variable and run the example in less than a minute.