-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
454f131
commit 3380069
Showing
16 changed files
with
514 additions
and
16 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
|
||
POSTGRES_URL="postgresql://postgres:mysecretpassword@localhost:5432/postgres" | ||
NEXTAUTH_URL=http://localhost:3000 | ||
NEXTAUTH_SECRET=somereallysecretsecret |
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,39 @@ | ||
import type { NextPage } from "next"; | ||
import { getServerSession } from "next-auth"; | ||
import { Address } from "~~/components/scaffold-eth"; | ||
import { getAllSubmissions } from "~~/services/database/repositories/submissions"; | ||
import { authOptions } from "~~/utils/auth"; | ||
|
||
const Admin: NextPage = async () => { | ||
const session = await getServerSession(authOptions); | ||
|
||
if (session?.user?.role !== "admin") { | ||
return <div className="flex items-center text-xl flex-col flex-grow pt-10 space-y-4">Access denied</div>; | ||
} | ||
|
||
const submissions = await getAllSubmissions(); | ||
return ( | ||
<> | ||
<div className="flex items-center flex-col flex-grow pt-10 space-y-4"> | ||
{submissions.map(submission => { | ||
return ( | ||
<div key={submission.id} className="card bg-primary text-primary-content"> | ||
<div className="card-body"> | ||
<h2 className="card-title">{submission.title}</h2> | ||
{submission.linkToRepository && ( | ||
<a href={submission.linkToRepository} className="link" target="_blank"> | ||
{submission.linkToRepository} | ||
</a> | ||
)} | ||
<p>{submission.description}</p> | ||
{submission.builder && <Address address={submission.builder} />} | ||
</div> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default Admin; |
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,18 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import NextAuth from "next-auth"; | ||
import { authOptions, providers } from "~~/utils/auth"; | ||
|
||
// For more information on each option (and a full list of options) go to | ||
// https://next-auth.js.org/configuration/options | ||
async function auth(req: NextRequest, res: NextResponse) { | ||
const { searchParams } = new URL(req.url as string); | ||
const isDefaultSigninPage = searchParams.get("nextauth")?.includes("signin"); | ||
|
||
if (isDefaultSigninPage) { | ||
providers.pop(); | ||
} | ||
|
||
return await NextAuth(req, res as unknown as { params: { nextauth: string[] } }, authOptions); | ||
} | ||
|
||
export { auth as GET, auth as POST }; |
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,38 @@ | ||
"use client"; | ||
|
||
import { useEffect } from "react"; | ||
import { useRouter } from "next/navigation"; | ||
import { useConnectModal } from "@rainbow-me/rainbowkit"; | ||
import { useAccount } from "wagmi"; | ||
import { useAuthSession } from "~~/hooks/useAuthSession"; | ||
import { useHandleLogin } from "~~/hooks/useHandleLogin"; | ||
|
||
export default function Siwe() { | ||
const { isConnected } = useAccount(); | ||
const { openConnectModal } = useConnectModal(); | ||
const { handleLogin } = useHandleLogin(); | ||
const { isAuthenticated } = useAuthSession(); | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
if (isAuthenticated) { | ||
router.push("/"); | ||
} | ||
}, [router, isAuthenticated]); | ||
|
||
return ( | ||
<button | ||
className="btn btn-primary my-10 self-center" | ||
onClick={e => { | ||
e.preventDefault(); | ||
if (!isConnected) { | ||
openConnectModal?.(); | ||
} else { | ||
handleLogin(); | ||
} | ||
}} | ||
> | ||
Sign in with Ethereum | ||
</button> | ||
); | ||
} |
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
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,11 @@ | ||
import { UseSessionOptions, useSession } from "next-auth/react"; | ||
|
||
export const useAuthSession = <R extends boolean>(options?: UseSessionOptions<R>) => { | ||
const sessionData = useSession(options); | ||
|
||
const isAdmin = sessionData?.data?.user?.role === "admin"; | ||
const address = sessionData?.data?.user?.address; | ||
const isAuthenticated = sessionData.status === "authenticated"; | ||
|
||
return { ...sessionData, isAdmin, address, isAuthenticated }; | ||
}; |
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,34 @@ | ||
import { useCallback } from "react"; | ||
import { getCsrfToken, signIn } from "next-auth/react"; | ||
import { SiweMessage } from "siwe"; | ||
import { useAccount, useSignMessage } from "wagmi"; | ||
|
||
export const useHandleLogin = () => { | ||
const { signMessageAsync } = useSignMessage(); | ||
const { address, chain } = useAccount(); | ||
|
||
const handleLogin = useCallback(async () => { | ||
try { | ||
const message = new SiweMessage({ | ||
domain: window.location.host, | ||
address: address, | ||
statement: "Sign in with Ethereum to the app.", | ||
uri: window.location.origin, | ||
version: "1", | ||
chainId: chain?.id, | ||
nonce: await getCsrfToken(), | ||
}); | ||
const signature = await signMessageAsync({ | ||
message: message.prepareMessage(), | ||
}); | ||
signIn("credentials", { | ||
message: JSON.stringify(message), | ||
signature, | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}, [address, chain?.id, signMessageAsync]); | ||
|
||
return { handleLogin }; | ||
}; |
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,23 @@ | ||
import { NextResponse } from "next/server"; | ||
import type { NextRequest } from "next/server"; | ||
import { getToken } from "next-auth/jwt"; | ||
|
||
const SIGN_IN_PAGE = "/siwe"; | ||
|
||
export async function middleware(request: NextRequest) { | ||
const token = await getToken({ req: request, secret: process.env.NEXTAUTH_SECRET }); | ||
|
||
// If no token and the request is not for the sign-in page, redirect to the sign-in page | ||
if (!token && request.nextUrl.pathname !== SIGN_IN_PAGE) { | ||
const url = request.nextUrl.clone(); | ||
url.pathname = SIGN_IN_PAGE; | ||
return NextResponse.redirect(url); | ||
} | ||
|
||
// If a token is found or the request is for the sign-in page, proceed as normal | ||
return NextResponse.next(); | ||
} | ||
|
||
export const config = { | ||
matcher: "/((?!api|static|.*\\..*|_next).*)", | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import "next-auth"; | ||
import { DefaultUser } from "next-auth"; | ||
import { DefaultJWT } from "next-auth/jwt"; | ||
|
||
declare module "next-auth" { | ||
export interface Session { | ||
user: { | ||
address?: string | null; | ||
role?: string | null; | ||
}; | ||
expires: ISODateString; | ||
} | ||
|
||
export interface User extends DefaultUser { | ||
role?: string | null; | ||
address?: string | null; | ||
} | ||
|
||
export interface JWT extends DefaultJWT { | ||
role?: string | null; | ||
address?: string | null; | ||
} | ||
} |
Oops, something went wrong.