irongit

Building a new website everyday to kickstart any new project I am recruited for.

verify passphrase

huncholanehuncholaneauthored
parent 0fd40c7commit 0f0ed3b241dd528266a1e006fee0005e8a0af7b8Browse files

5 files changed, +54 -4

+6-0008_hygofitweb/app/actions.ts
@@ -0,0 +1,6 @@
1+"use server";
2+
3+export async function verifyGuess(guess: string): boolean {
4+ console.log("guess", guess);
5+ return guess === process.env.MESSAGE_SECRET;
6+}
+34-2008_hygofitweb/app/page.tsx
@@ -1,7 +1,39 @@
1+"use client";
2+
3+import { useState } from "react";
4+import { verifyGuess } from "./actions";
5+
16 export default function Home() {
7+ const [isVerified, setIsVerified] = useState(false);
28 return (
3- <div>
4- <div className="h-96">Content</div>
9+ <div className="flex w-full justify-center h-full">
10+ {isVerified ? (
11+ <form
12+ className="flex flex-col gap-4"
13+ action={() => {
14+ // Send a message to apn
15+ }}
16+ >
17+ <h1 className="mx-auto">Send a message to the app users</h1>
18+ <input className="mx-auto" type="text" name="message" />
19+ <button className="mx-auto" type="submit">
20+ Send
21+ </button>
22+ </form>
23+ ) : (
24+ <form
25+ className="flex flex-col gap-4"
26+ action={(formData) => {
27+ verifyGuess(formData.get("guess") as string) && setIsVerified(true);
28+ }}
29+ >
30+ <h1 className="mx-auto">Guess the secret phrases</h1>
31+ <input className="mx-auto" type="password" name="guess" />
32+ <button className="mx-auto" type="submit">
33+ Submit
34+ </button>
35+ </form>
36+ )}
537 </div>
638 );
739 }
+0-1008_hygofitweb/auth.ts
@@ -1,7 +1,6 @@
11 import { prisma } from "@/prisma";
22 import { PrismaAdapter } from "@auth/prisma-adapter";
33 import NextAuth from "next-auth";
4-import Credentials from "next-auth/providers/credentials";
54
65 export const { handlers, auth, signIn, signOut } = NextAuth({
76 adapter: PrismaAdapter(prisma),
+7-1008_hygofitweb/middleware.ts
@@ -1,6 +1,12 @@
1+import { prisma } from "./prisma";
2+
13 const deviceMiddleware = async (req: Request) => {
24 const headers = new Headers(req.headers);
3- const deviceId = headers.get("Device-ID");
5+ const id = headers.get("Device-ID");
6+
7+ if (id) {
8+ prisma.device.upsert({ where: { id }, update: {}, create: { id } });
9+ }
410 };
511
612 export default deviceMiddleware;
+7-0008_hygofitweb/prisma/migrations/20240910080104_make_device/migration.sql
@@ -0,0 +1,7 @@
1+-- CreateTable
2+CREATE TABLE "Device" (
3+ "id" TEXT NOT NULL
4+);
5+
6+-- CreateIndex
7+CREATE UNIQUE INDEX "Device_id_key" ON "Device"("id");