Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Update Next.js auth documentation with new Auth setup and payload generation details for smart accounts #488

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/app/connect/auth/frameworks/next/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ const clientId = process.env.NEXT_PUBLIC_THIRDWEB_CLIENT_ID!; // this will be us
const secretKey = process.env.THIRDWEB_SECRET_KEY!; // this will be used on the server-side

export const client = createThirdwebClient(
secretKey
? { secretKey }
: {
clientId,
},
secretKey ? { secretKey } : { clientId },
);
```

Expand Down Expand Up @@ -160,12 +156,13 @@ In your project's components directory, create a `LoginButton.tsx` file:
// LoginButton.tsx
"use client";

import { useActiveAccount } from "thirdweb/react";
import { useActiveAccount, useActiveWalletChain } from "thirdweb/react";
import { generatePayload, login } from "@/actions/auth"; // we'll add this file in the next section
import { signLoginPayload } from "thirdweb/auth";

export const LoginButton = () => {
const account = useActiveAccount();
const chain = useActiveWalletChain();
const { connect, isConnecting, error } = useConnect();

async function handleClick() {
Expand All @@ -181,7 +178,10 @@ export const LoginButton = () => {
activeAccount = account;
}
// Step 1: fetch the payload from the server
const payload = await generatePayload({ address: account.address });
const payload = await generatePayload({
address: account.address,
chainId: chain.id,
});
// Step 2: Sign the payload
const signatureResult = await signLoginPayload({ account, payload });
// Step 3: Send the signature to the server for verification
Expand Down Expand Up @@ -239,6 +239,7 @@ if (!privateKey) {
const thirdwebAuth = createAuth({
domain: process.env.NEXT_PUBLIC_THIRDWEB_AUTH_DOMAIN || "",
adminAccount: privateKeyAccount({ client, privateKey }),
client: client,
});

export const generatePayload = thirdwebAuth.generatePayload;
Expand Down
13 changes: 11 additions & 2 deletions src/app/connect/auth/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ clientId: '1234567890', // get yours at https://thirdweb.com/dashboard/settings/

const thirdwebAuth = createAuth({
domain: 'localhost:3000',
client
client,
adminAccount: privateKeyToAccount({ client, privateKey }),
});

export default function App() {
Expand Down Expand Up @@ -113,11 +114,15 @@ const client = createThirdwebClient({

const auth = createAuth({
domain: "localhost:3000",
adminAccount: privateKeyToAccount({ client, privateKey }),
client,
});

// 1. generate a login payload on the server
const loginPayload = await auth.generatePayload({ address: "0x123..." });
const loginPayload = await auth.generatePayload({
address: "0x123...",
chain: 1,
});

// 2. sign the login payload on the client
const signature = await auth.signPayload({
Expand Down Expand Up @@ -149,6 +154,10 @@ const { valid, parsedJWT } = await auth.verifyJWT({ jwt });
Production](/connect/auth/deploying-to-production).
</Callout>

### Auth with Smart Accounts (Account Abstraction)

When using Auth with a smart account, you **must** specify a client (on `createAuth`) and a chain ID (on `generatePayload`). The smart account is deployed on a specific chain and the payload must reflect that, and the client is needed to call the wallet contract to verify the signature.

</Tabs>

## Templates
Expand Down
Loading