-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SDK] Feature: Add SiweOptions for useConnectModal (#6317)
Co-authored-by: Joaquim Verges <[email protected]>
- Loading branch information
1 parent
cf202c0
commit a19c0c9
Showing
8 changed files
with
210 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"thirdweb": minor | ||
--- | ||
|
||
Add SiweOptions in useConnectModal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"use client"; | ||
|
||
import { | ||
generatePayload, | ||
isLoggedIn, | ||
login, | ||
logout, | ||
} from "@/app/connect/auth/server/actions/auth"; | ||
import { | ||
type SiweAuthOptions, | ||
useActiveWallet, | ||
useConnectModal, | ||
useSiweAuth, | ||
} from "thirdweb/react"; | ||
import { Button } from "../ui/button"; | ||
|
||
const auth: SiweAuthOptions = { | ||
isLoggedIn: (address) => isLoggedIn(address), | ||
doLogin: (params) => login(params), | ||
getLoginPayload: ({ address }) => | ||
generatePayload({ address, chainId: 84532 }), | ||
doLogout: () => logout(), | ||
}; | ||
|
||
export function AuthHook() { | ||
const { connect } = useConnectModal(); | ||
const wallet = useActiveWallet(); | ||
const { isLoggedIn, doLogout } = useSiweAuth( | ||
wallet, | ||
wallet?.getAccount(), | ||
auth, | ||
); | ||
|
||
const onClick = async () => { | ||
if (isLoggedIn) { | ||
await doLogout(); | ||
} else { | ||
await connect({ | ||
auth, | ||
onConnect: (wallet) => { | ||
console.log("connected to", wallet); | ||
}, | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<Button type="button" onClick={onClick}> | ||
{isLoggedIn ? "Sign out" : "Sign in"} | ||
</Button> | ||
); | ||
} |
36 changes: 36 additions & 0 deletions
36
apps/playground-web/src/components/auth/basic-auth-hook.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { RequestCookie } from "next/dist/compiled/@edge-runtime/cookies"; | ||
import { cookies } from "next/headers"; | ||
import { cn } from "../../lib/utils"; | ||
import { AuthHook } from "./auth-hook"; | ||
|
||
export async function BasicAuthHookPreview() { | ||
const jwt = (await cookies()).get("jwt"); | ||
return ( | ||
<div className="flex flex-col gap-5"> | ||
<div className="mx-auto"> | ||
<AuthHook /> | ||
</div> | ||
{jwt && !!jwt.value && ( | ||
<table className="table-auto border-collapse rounded-lg backdrop-blur"> | ||
<tbody> | ||
{Object.keys(jwt).map((key) => ( | ||
<tr key={key} className=""> | ||
<td className="rounded border p-2">{key}</td> | ||
<td | ||
className={cn( | ||
"max-h-[200px] max-w-[250px] overflow-y-auto whitespace-normal break-words border p-2", | ||
{ | ||
"text-xs": key === "value", | ||
}, | ||
)} | ||
> | ||
{jwt[key as keyof RequestCookie]} | ||
</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters