registration form complete and inserting to db

This commit is contained in:
ImAlpha 2023-12-02 01:24:02 -08:00
parent af8dee7b81
commit 99873efddb
4 changed files with 60 additions and 33 deletions

View File

@ -11,8 +11,8 @@ const handler = NextAuth({
// e.g. domain, username, password, 2FA token, etc.
// You can pass any HTML attribute to the <input> tag through the object.
credentials: {
username: { label: "Username", type: "text", placeholder: "jsmith" },
password: { label: "Password", type: "password" },
email: {},
password: {},
},
async authorize(credentials, req) {
// Add logic here to look up the user from the credentials supplied
@ -32,4 +32,4 @@ const handler = NextAuth({
],
});
export {handler as GET, handler as POST};
export { handler as GET, handler as POST };

View File

@ -2,25 +2,35 @@ import { hash } from "bcrypt";
import "dotenv/config";
import { db, connection } from "@/db/db";
import { users } from "@/db/schema";
import { NextResponse } from "next/server";
export default async function POST(request: Request) {
export async function POST(request: Request) {
try {
const { email, password } = await request.json();
// validate email and password
if (email != process.env.ADMIN_EMAIL && password != process.env.ADMIN_PWD) {
return 0;
// validate email and password(temporarily hard coded)
if (
email === process.env.ADMIN_EMAIL &&
password === process.env.ADMIN_PWD
) {
console.log({ message: "success!" });
console.log({ email, password });
}
// return if not admin
else {
console.log({ message: "Incorrect credentials for admin!" });
console.log({ email, password });
return NextResponse.json({ message: "failed" });
}
console.log({ email, password });
const hashedPassword = await hash(password, 10);
// console.log({message:'hash is', hashedPassword})
const response = await db.insert(users).values({
email: email,
password: password,
});
} catch (e) {
console.log({ e });
}
return NextResponse.json({ message: "pass" });
}

View File

@ -1,28 +1,12 @@
import React, { FormEvent } from "react";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import React from "react";
import RegistrationForm from "./registrationForm";
export default async function Admin() {
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const response = await fetch(`/api/auth/register`, {
method: "POST",
body: JSON.stringify({
email: formData.get("email"),
password: formData.get("password"),
}),
});
console.log({ response });
};
return (
<form
onSubmit={handleSubmit}
className="flex flex-col gap-2 mx-auto max-w-md mt-20"
>
<Input type="email" placeholder="email" />
<Input type="password" placeholder="password" />
<Button className="bg-blue-500 text-white text-lg">Submit</Button>
</form>
<div className="flex flex-col bg-emerald-700 w-full h-[800svh]">
<div className="absolute w-full h-[20svh] top-60">
<RegistrationForm />
</div>
</div>
);
}

View File

@ -0,0 +1,33 @@
"use client";
import React, { FormEvent } from "react";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
const RegistrationForm = () => {
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const response = await fetch(`/api/auth/register`, {
method: "POST",
body: JSON.stringify({
email: formData.get("email"),
password: formData.get("password"),
}),
});
console.log({ response });
};
return (
<div className="w-full h-full">
<form
onSubmit={handleSubmit}
className="flex flex-col gap-2 mx-auto max-w-md mt-20"
>
<Input name="email" type="email" placeholder="email" />
<Input name="password" type="password" placeholder="password" />
<Button className="bg-blue-500 text-white text-lg">Submit</Button>
</form>
</div>
);
};
export default RegistrationForm;