Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Magic SDK for wallet connection and user authentication #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 15 additions & 12 deletions app/(general)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ReactNode } from "react"

import { NetworkStatus } from "@/components/blockchain/network-status"
import { WalletConnect } from "@/components/blockchain/wallet-connect"
import MagicProvider from "@/components/context/magicProvider"
import { Footer } from "@/components/layout/footer"
import { SiteHeader } from "@/components/layout/site-header"
import { FramerWrapper } from "@/components/providers/framer-wrapper"
Expand All @@ -13,17 +14,19 @@ interface RootLayoutProps {

export default function RootLayout({ children }: RootLayoutProps) {
return (
<FramerWrapper>
<div className="relative flex min-h-screen flex-col">
<SiteHeader />
<main className="flex-1">{children}</main>
<Opensource />
<Footer />
</div>
<NetworkStatus />
<div className="fixed bottom-6 right-6 z-50 block sm:hidden">
<WalletConnect />
</div>
</FramerWrapper>
<MagicProvider>
<FramerWrapper>
<div className="relative flex min-h-screen flex-col">
<SiteHeader />
<main className="flex-1">{children}</main>
<Opensource />
<Footer />
</div>
<NetworkStatus />
<div className="fixed bottom-6 right-6 z-50 block sm:hidden">
<WalletConnect />
</div>
</FramerWrapper>
</MagicProvider>
)
}
27 changes: 13 additions & 14 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Analytics } from "@vercel/analytics/react"
import { siteConfig } from "@/config/site"
import { cn } from "@/lib/utils"

import MagicProvider from "@/components/context/magicProvider"
import RootProvider from "@/components/providers/root-provider"
import { Toaster } from "@/components/ui/toaster"

Expand Down Expand Up @@ -93,19 +94,17 @@ export const metadata: Metadata = {

export default function RootLayout({ children }: { children: ReactNode }) {
return (
<>
<html lang="en" suppressHydrationWarning>
<body
className={cn(
"min-h-screen bg-background font-sans antialiased",
fontSans.variable
)}
>
<RootProvider>{children}</RootProvider>
<Analytics />
<Toaster />
</body>
</html>
</>
<html lang="en" suppressHydrationWarning>
<body
className={cn(
"min-h-screen bg-background font-sans antialiased",
fontSans.variable
)}
>
<RootProvider>{children}</RootProvider>
<Analytics />
<Toaster />
</body>
</html>
)
}
13 changes: 12 additions & 1 deletion components/blockchain/wallet-connect-custom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ConnectButton } from "@rainbow-me/rainbowkit"

import { Button } from "@/components/ui/button"

import { useMagic } from "../context/magicProvider"

interface WalletConnectCustomProps extends HTMLAttributes<HTMLDivElement> {
classNameConnect?: string
classNameConnected?: string
Expand All @@ -17,6 +19,15 @@ export const WalletConnectCustom = ({
labelWrongNetwork = "Wrong Network",
...props
}: WalletConnectCustomProps) => {
const { magic } = useMagic()
const handleConnect = async () => {
if (!magic) {
return
}

await magic.wallet.connectWithUI()
}

return (
<ConnectButton.Custom>
{({
Expand All @@ -37,7 +48,7 @@ export const WalletConnectCustom = ({
if (!connected) {
return (
<>
<Button variant="default" onClick={openConnectModal}>
<Button variant="default" onClick={handleConnect}>
{labelConnect}
</Button>
</>
Expand Down
27 changes: 16 additions & 11 deletions components/blockchain/wallet-connect.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { HtmlHTMLAttributes } from "react"
import { ConnectButton } from "@rainbow-me/rainbowkit"

import { useMagic } from "@/components/context/magicProvider"

import { Button } from "../ui/button"

export const WalletConnect = ({
className,
...props
}: HtmlHTMLAttributes<HTMLSpanElement>) => {
const { magic } = useMagic()

const handleConnect = async () => {
if (!magic) {
return
}
await magic.wallet.connectWithUI()
}

return (
<span className={className} {...props}>
<ConnectButton
showBalance={false}
accountStatus={{
smallScreen: "avatar",
largeScreen: "avatar",
}}
chainStatus={{
smallScreen: "icon",
largeScreen: "icon",
}}
/>
<Button variant="default" onClick={handleConnect}>
Log in
</Button>
</span>
)
}
50 changes: 50 additions & 0 deletions components/context/magicProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use client"

import {
createContext,
ReactNode,
useContext,
useEffect,
useMemo,
useState,
} from "react"
import { Magic as MagicBase } from "magic-sdk"

export type Magic = MagicBase<OAuthExtension[]>

type MagicContextType = {
magic: Magic | null
}

const MagicContext = createContext<MagicContextType>({
magic: null,
})

export const useMagic = () => useContext(MagicContext)

const MagicProvider = ({ children }: { children: ReactNode }) => {
const [magic, setMagic] = useState<Magic | null>(null)

useEffect(() => {
if (process.env.NEXT_PUBLIC_MAGIC_API_KEY) {
const magic = new MagicBase(process.env.NEXT_PUBLIC_MAGIC_API_KEY, {
network: {
rpcUrl: "https://rpc2.sepolia.org/",
chainId: 11155111,
},
})

setMagic(magic)
}
}, [])

const value = useMemo(() => {
return {
magic,
}
}, [magic])

return <MagicContext.Provider value={value}>{children}</MagicContext.Provider>
}

export default MagicProvider
6 changes: 4 additions & 2 deletions components/shared/is-wallet-connected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import { ReactNode } from "react"
import { useAccount } from "wagmi"

import { useMagic } from "../context/magicProvider"

interface IsWalletConnectedProps {
children: ReactNode
}

export const IsWalletConnected = ({ children }: IsWalletConnectedProps) => {
const { address } = useAccount()
const { magic } = useMagic()

if (address) return <>{children}</>
if (magic?.user.isLoggedIn) return <>{children}</>

return null
}
14 changes: 11 additions & 3 deletions integrations/siwe/components/button-siwe-login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useToast } from "@/lib/hooks/use-toast"
import { useUser } from "@/lib/hooks/use-user"
import { cn } from "@/lib/utils"

import { useMagic } from "@/components/context/magicProvider"
import { Icons } from "@/components/shared/icons"
import { Button } from "@/components/ui/button"
import {
Expand All @@ -35,6 +36,8 @@ export const ButtonSIWELogin = ({
...props
}: ButtonSIWELoginProps) => {
const router = useRouter()
const { magic } = useMagic()

const { toast } = useToast()
const { chain } = useNetwork()
const { address } = useAccount()
Expand Down Expand Up @@ -68,10 +71,15 @@ export const ButtonSIWELogin = ({
const labelClasses = cn({
"opacity-0": isLoading,
})
const handleAccount = async () => {
if (magic?.user.isLoggedIn) {
await magic.wallet.showUI()
}
}

return (
<>
{user?.isLoggedIn ? (
{magic?.user.isLoggedIn ? (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
Expand All @@ -98,9 +106,9 @@ export const ButtonSIWELogin = ({
</Link>
</DropdownMenuItem>
<DropdownMenuItem>
<Link className="w-full" href="/dashboard/profile">
<span className="w-full" onClick={handleAccount}>
Profile
</Link>
</span>
</DropdownMenuItem>
<DropdownMenuItem>
<Link className="w-full" href="/dashboard/subscription">
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"jotai": "^1.13.0",
"lucide-react": "^0.367.0",
"luxon": "^3.2.1",
"magic-sdk": "^28.19.1",
"marked": "^14.1.2",
"moment": "^2.29.4",
"mongodb": "^6.6.0",
Expand Down
Loading