registration form complete and inserting to db
This commit is contained in:
parent
af8dee7b81
commit
99873efddb
@ -11,8 +11,8 @@ const handler = NextAuth({
|
|||||||
// e.g. domain, username, password, 2FA token, etc.
|
// e.g. domain, username, password, 2FA token, etc.
|
||||||
// You can pass any HTML attribute to the <input> tag through the object.
|
// You can pass any HTML attribute to the <input> tag through the object.
|
||||||
credentials: {
|
credentials: {
|
||||||
username: { label: "Username", type: "text", placeholder: "jsmith" },
|
email: {},
|
||||||
password: { label: "Password", type: "password" },
|
password: {},
|
||||||
},
|
},
|
||||||
async authorize(credentials, req) {
|
async authorize(credentials, req) {
|
||||||
// Add logic here to look up the user from the credentials supplied
|
// 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 };
|
||||||
|
|||||||
@ -2,25 +2,35 @@ import { hash } from "bcrypt";
|
|||||||
import "dotenv/config";
|
import "dotenv/config";
|
||||||
import { db, connection } from "@/db/db";
|
import { db, connection } from "@/db/db";
|
||||||
import { users } from "@/db/schema";
|
import { users } from "@/db/schema";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
export default async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const { email, password } = await request.json();
|
const { email, password } = await request.json();
|
||||||
// validate email and password
|
// validate email and password(temporarily hard coded)
|
||||||
if (email != process.env.ADMIN_EMAIL && password != process.env.ADMIN_PWD) {
|
if (
|
||||||
return 0;
|
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);
|
const hashedPassword = await hash(password, 10);
|
||||||
|
// console.log({message:'hash is', hashedPassword})
|
||||||
|
|
||||||
const response = await db.insert(users).values({
|
const response = await db.insert(users).values({
|
||||||
email: email,
|
email: email,
|
||||||
password: password,
|
password: password,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log({ e });
|
console.log({ e });
|
||||||
}
|
}
|
||||||
|
return NextResponse.json({ message: "pass" });
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,28 +1,12 @@
|
|||||||
import React, { FormEvent } from "react";
|
import React from "react";
|
||||||
import { Input } from "@/components/ui/input";
|
import RegistrationForm from "./registrationForm";
|
||||||
import { Button } from "@/components/ui/button";
|
|
||||||
|
|
||||||
export default async function Admin() {
|
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 (
|
return (
|
||||||
<form
|
<div className="flex flex-col bg-emerald-700 w-full h-[800svh]">
|
||||||
onSubmit={handleSubmit}
|
<div className="absolute w-full h-[20svh] top-60">
|
||||||
className="flex flex-col gap-2 mx-auto max-w-md mt-20"
|
<RegistrationForm />
|
||||||
>
|
</div>
|
||||||
<Input type="email" placeholder="email" />
|
</div>
|
||||||
<Input type="password" placeholder="password" />
|
|
||||||
<Button className="bg-blue-500 text-white text-lg">Submit</Button>
|
|
||||||
</form>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
33
Websites/jefes-nextjs/app/register/registrationForm.tsx
Normal file
33
Websites/jefes-nextjs/app/register/registrationForm.tsx
Normal 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;
|
||||||
Loading…
Reference in New Issue
Block a user