generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
5 changed files
with
71 additions
and
9 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,12 +1,56 @@ | ||
"use client"; | ||
|
||
import { useEffect, useState } from "react"; | ||
import { useReadLocalStorage } from "usehooks-ts"; | ||
import { useAccount } from "wagmi"; | ||
import SIWE from "~~/components/SIWE"; | ||
import { GrantData } from "~~/services/database/schema"; | ||
import { notification } from "~~/utils/scaffold-eth"; | ||
|
||
const AdminPage = () => { | ||
const [grants, setGrants] = useState<GrantData[]>([]); | ||
const { isConnected } = useAccount(); | ||
const jwt = useReadLocalStorage("jwt"); | ||
|
||
// In use effect, make get request (with JWT) to /api/grants/all | ||
useEffect(() => { | ||
const getGrants = async () => { | ||
try { | ||
const response = await fetch("/api/grants/all", { | ||
headers: { | ||
Authorization: `Bearer ${jwt}`, | ||
}, | ||
}); | ||
const grants: GrantData[] = (await response.json()).data; | ||
setGrants(grants); | ||
} catch (error) { | ||
notification.error("Error getting grants"); | ||
} | ||
}; | ||
|
||
if (jwt) getGrants(); | ||
}, [jwt]); | ||
|
||
const Home = () => { | ||
return ( | ||
<div className="container mx-auto max-w-screen-md mt-12"> | ||
<h1 className="text-4xl font-bold">Admin page</h1> | ||
<SIWE /> | ||
<h2 className="font-bold mt-8">Admin data:</h2> | ||
{!isConnected || !jwt ? ( | ||
<p>Connect & authenticate to see admin data</p> | ||
) : ( | ||
<> | ||
<h2 className="font-bold mt-8">All grants:</h2> | ||
{grants.map(grant => ( | ||
<div key={grant.id} className="border p-4 my-4"> | ||
<h3 className="font-bold">{grant.title}</h3> | ||
<p>{grant.description}</p> | ||
</div> | ||
))} | ||
</> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Home; | ||
export default AdminPage; |
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,22 @@ | ||
import { NextResponse } from "next/server"; | ||
import jwt from "jsonwebtoken"; | ||
import { getAllGrants } from "~~/services/database/grants"; | ||
|
||
export async function GET(request: Request) { | ||
// ToDo. We probably want to use a middleware for this. | ||
const authHeader = request.headers.get("Authorization"); | ||
if (!authHeader || !authHeader.startsWith("Bearer ")) { | ||
return new Response("Unauthorized", { status: 401 }); | ||
} | ||
|
||
const token = authHeader.split(" ")[1]; | ||
try { | ||
jwt.verify(token, process.env.JWT_SECRET || ""); | ||
} catch (error) { | ||
return new Response("Unauthorized", { status: 401 }); | ||
} | ||
|
||
const grants = await getAllGrants(); | ||
|
||
return NextResponse.json({ data: grants }); | ||
} |
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