Quickstart
Start building awesome apps in under 5 minutes
Step 1. Install Protocol Auth in your NextjS app:
Step 2. Create a project at https://studio.pxyz.dev
Once created, you will see environment variables for your project on the dashboard.
Step 3. Add the environment variables to your .env
file:
Step 4. Project setup (app router)
Add the following code to lib/auth.ts:
Whenever you need to determine whether or not a user is authenticated, you can use the auth
function. If the user is not authenticated, the function will return null
. If the user is authenticated, the function will return the verified JWT object.
Add an api route to handle the callback from the hosted accounts page. Create a new file called app/api/auth/callback.ts
. Add the following code:
Step 5. Protect pages (app router)
Create a new page in your project that you want to secure. For example, create a new file called app/onboard/page.tsx
. Add the following code:
Step 6. Protect api routes (app router)
Create a new file called app/api/protected/route.ts
. Add the following code:
Step 7. Protecting server actions
You can also protect server actions by calling the auth
function. For example, if your actions are in lib/actions.ts add the following code:
Step 8. Checking if a user is authenticated in middleware
You can also check if a user is authenticated in middleware. For example, if you want to check if a user is authenticated in a middleware, add the following code:
If you only need to check if a user is authenticated, you can use the getToken
function. For example:
Step 9. Passing user data to client components
You can easily pass user data to client components from the server. For example, create a new component in components/user-button.tsx
. Add the following code:
And then call it from your protected page: