Skip to content

Commit

Permalink
Pass cookieToInitialState from server to client component
Browse files Browse the repository at this point in the history
  • Loading branch information
Royal-lobster committed Jan 2, 2025
1 parent e13a762 commit f409256
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-plums-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@everipedia/iq-login": patch
---

Made cookieToInitialState to be passed from server component
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ pnpm install @everipedia/iq-login [email protected] [email protected] @web3auth/modal @web3auth/e

## 🛠️ Setup

1. Add the package to your Tailwind CSS configuration:
1. Add environment variables:
```bash
## .env.local
NEXT_PUBLIC_CHAIN_ID=your_chain_id
NEXT_PUBLIC_WEB3_AUTH_CLIENT_ID=your_web3auth_client_id
```

Currently supported chains:
- Ethereum Mainnet (1)
- Polygon Mainnet (137)
- Fraxtal Mainnet (252)
- IQ Testnet (313_377)

2. Add the package to your Tailwind CSS configuration:

```ts
// tailwind.config.ts
Expand All @@ -30,29 +43,32 @@ const config: Config = {
export default config;
```

2. Wrap your application with the IqLoginProvider in your layout file:
3. Wrap your application with the IqLoginProvider in your layout file:

```tsx
// app/layout.tsx

import { IqLoginProvider } from "@everipedia/iq-login";
import { polygon } from 'wagmi/chains';
import { IqLoginProvider } from "@everipedia/iq-login/client";
import { getWagmiConfig } from "@everipedia/iq-login";
import { headers } from "next/headers";
import { cookieToInitialState } from "wagmi";

export default function RootLayout({
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
const cookie = headers().get("cookie") || "";
const initialStates = cookieToInitialState(
getWagmiConfig(),
(await headers()).get("cookie")
);

return (
<html lang="en">
<body>
<IqLoginProvider
chain={polygon} // Required: Specify the chain to use
web3AuthProjectId="YOUR_PROJECT_ID" // Required: Web3Auth Project ID
projectName="YOUR_PROJECT_NAME" // Required: Project name for storage
cookie={cookie}
initialStates={initialStates}
>
{children}
</IqLoginProvider>
Expand All @@ -62,7 +78,7 @@ export default function RootLayout({
}
```

3. Add login page to your application:
4. Add login page to your application:

```tsx
// app/login/page.tsx
Expand Down
10 changes: 4 additions & 6 deletions src/components/iq-login-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { structuralSharing } from "@wagmi/core/query";
import type React from "react";
import { createContext, useState } from "react";
import { cookieToInitialState, WagmiProvider } from "wagmi";
import { type State, WagmiProvider } from "wagmi";
import {
getWagmiConfig,
web3AuthInstance,
Expand All @@ -13,15 +13,15 @@ import { Web3AuthProvider } from "./web3-auth-provider";

interface IqLoginProviderProps {
children: React.ReactNode;
cookie?: string;
projectName: string;
initialState?: State;
}

export const ProjectContext = createContext<string>("");

export function IqLoginProvider({
children,
cookie,
initialState,
projectName,
}: IqLoginProviderProps) {
const [wagmiConfig] = useState(() => getWagmiConfig());
Expand All @@ -37,11 +37,9 @@ export function IqLoginProvider({
}),
);

const initialStates = cookieToInitialState(wagmiConfig, cookie);

return (
<ProjectContext.Provider value={projectName}>
<WagmiProvider config={wagmiConfig} initialState={initialStates}>
<WagmiProvider config={wagmiConfig} initialState={initialState}>
<QueryClientProvider client={queryClient}>
<Web3AuthProvider web3AuthInstance={web3AuthInstance}>
{children}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { getAuth } from "./lib/getAuth";
// Data
// ===============
export { iqTestnet } from "./lib/data/iq-testnet";
export { getWagmiConfig } from "./lib/integrations/wagmi.config";

// ===============
// Constants
Expand Down

0 comments on commit f409256

Please sign in to comment.