Skip to content

Commit

Permalink
user server side cookies for connected address
Browse files Browse the repository at this point in the history
  • Loading branch information
labintsev committed Dec 15, 2024
1 parent f872150 commit 52d048f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
10 changes: 8 additions & 2 deletions packages/nextjs/app/lib/actions.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
"use server";

import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { connectedAddressKey } from "./definitions";
import { sql } from "@vercel/postgres";

export async function setAddressCookie(address: string) {
const cookieStore = await cookies();
cookieStore.set(connectedAddressKey, address);
}

export default async function createAnswer(formData: FormData) {
const rawFormData = {
question: formData.get("question"),
answer: formData.get("radio-0"),
connectedAddress: formData.get("address"),
};
// Test it out:
// todo get id for current user address:
const sh_id = "d6e15727-9fe1-4961-8c5b-ea44a9bd8100";
console.log(rawFormData);

// todo fix query to store ids
await sql`
INSERT INTO answers (sh_id, question_id, choice_id, answer_time)
VALUES (${sh_id}, ${rawFormData.question?.toString()}, ${rawFormData.answer?.toString()}, now())
Expand Down
2 changes: 2 additions & 0 deletions packages/nextjs/app/lib/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const connectedAddressKey = "scaffoldEth2.address";

export type Question = {
id: string;
question: string;
Expand Down
7 changes: 7 additions & 0 deletions packages/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use client";

import { useEffect } from "react";
import Link from "next/link";
import { setAddressCookie } from "./lib/actions";
import type { NextPage } from "next";
import { useAccount } from "wagmi";
import { BugAntIcon, MagnifyingGlassIcon, StarIcon } from "@heroicons/react/24/outline";
Expand All @@ -9,6 +11,11 @@ import { Address } from "~~/components/scaffold-eth";
const Home: NextPage = () => {
const { address: connectedAddress } = useAccount();

useEffect(() => {
if (connectedAddress) {
setAddressCookie(connectedAddress);
}
}, [connectedAddress]);
return (
<>
<div className="flex items-center flex-col flex-grow pt-10">
Expand Down
23 changes: 18 additions & 5 deletions packages/nextjs/app/voting/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { cookies } from "next/headers";
import { fetchQuestions } from "../lib/data";
import { connectedAddressKey } from "../lib/definitions";
import VotingForm from "./_components/VotingForm";

export default async function Page() {
const questions = await fetchQuestions();
return (
<>
<VotingForm question={questions[2]} />
</>
);
const cookieStore = cookies();

const connectedAddress = cookieStore.get(connectedAddressKey)?.value;
console.log(connectedAddress);
if (connectedAddress) {
return (
<>
<VotingForm question={questions[2]} address={connectedAddress} />
</>
);
} else
return (
<>
<h1>Connect to wallet for voting!</h1>
</>
);
}

0 comments on commit 52d048f

Please sign in to comment.