-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add recruitment form frontend and backend
- Loading branch information
kamini08
committed
Aug 17, 2024
1 parent
f062453
commit c9233d7
Showing
13 changed files
with
578 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { db } from "@/Firebase"; | ||
import { recruitValidate } from "@/lib/utils"; | ||
import { addDoc, collection, getDocs } from "firebase/firestore"; | ||
import { NextResponse } from "next/server"; | ||
|
||
// Verify reCAPTCHA Token | ||
async function verifyRecaptcha(token: string) { | ||
const secretKey = process.env.RECAPTCHA_SECRET_KEY; | ||
const response = await fetch( | ||
`https://www.google.com/recaptcha/api/siteverify`, | ||
{ | ||
method: "POST", | ||
headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
body: new URLSearchParams({ | ||
secret: secretKey || "", | ||
response: token, | ||
}), | ||
} | ||
); | ||
const data = await response.json(); | ||
return data.success; | ||
} | ||
|
||
// Add a new registration | ||
export async function POST(request: Request) { | ||
const data = await request.json(); | ||
const recaptchaToken = data.recaptchaToken; // Extract the reCAPTCHA token from the request | ||
|
||
// Verify the reCAPTCHA token | ||
const isRecaptchaValid = await verifyRecaptcha(recaptchaToken); | ||
|
||
if (!isRecaptchaValid) { | ||
return NextResponse.json({ | ||
message: "reCAPTCHA verification failed", | ||
error: true, | ||
}); | ||
} | ||
|
||
// Validate the data | ||
const val = recruitValidate(data); | ||
|
||
if (!Array.isArray(data)) { | ||
return NextResponse.json({ | ||
message: "Expected an array of JSON objects", | ||
error: true, | ||
}); | ||
} | ||
|
||
if (val.error) { | ||
return NextResponse.json({ message: "Validation error", error: val.error }); | ||
} | ||
|
||
try { | ||
// Save to Firebase | ||
const docRef = await addDoc(collection(db, "recruitment2024"), data); | ||
console.log("Document written with ID: ", docRef.id); | ||
} catch (error) { | ||
console.error(error); | ||
return NextResponse.json({ message: "An error occurred", error }); | ||
} | ||
// Return a response | ||
return NextResponse.json({ message: "Registration successful", data }); | ||
} | ||
|
||
// Get all registrations | ||
export async function GET() { | ||
try { | ||
// Get all registrations in recruitment2024 collection | ||
const querySnapshot = await getDocs(collection(db, "recruitment2024")); | ||
// Map the data to get only the data | ||
const data = querySnapshot.docs.map((doc) => doc.data()); | ||
return NextResponse.json({ data }); | ||
} catch (error) { | ||
console.error(error); | ||
return NextResponse.json({ message: "An error occurred", error }); | ||
} | ||
} |
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,29 @@ | ||
|
||
import RecruitmentForm from "@/components/forms/recruitmentForm"; | ||
import DotPattern from "@/components/magicui/dot-pattern"; | ||
import "../../css/additional-styles/form.css"; | ||
import { cn } from "@/lib/utils"; | ||
const RegisterPage = () => { | ||
return ( | ||
|
||
<div className="w-50 mt-16 mx-auto flex flex-col items-center justify-center"> | ||
|
||
<div className="form-container my-2"> | ||
<RecruitmentForm /> | ||
</div> | ||
<DotPattern | ||
width={20} | ||
height={20} | ||
cx={1} | ||
cy={1} | ||
cr={1} | ||
className={cn( | ||
"[mask-image:linear-gradient(to_bottom_right,white,transparent,transparent)] " | ||
)} | ||
/> | ||
</div> | ||
|
||
); | ||
}; | ||
|
||
export default RegisterPage; |
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
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,33 @@ | ||
import { useEffect } from "react"; | ||
|
||
const Recaptcha = ({ onChange }: { onChange: (token: string | null) => void }) => { | ||
useEffect(() => { | ||
const loadRecaptcha = () => { | ||
if (window.grecaptcha) { | ||
window.grecaptcha.ready(() => { | ||
window.grecaptcha.execute(process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY || '', { action: 'submit' }).then((token: string) => { | ||
onChange(token); | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
const script = document.createElement("script"); | ||
script.src = `https://www.google.com/recaptcha/api.js?render=${process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY}`; | ||
script.async = true; | ||
script.defer = true; | ||
script.onload = loadRecaptcha; | ||
document.head.appendChild(script); | ||
|
||
return () => { | ||
document.head.removeChild(script); | ||
}; | ||
}, [onChange]); | ||
|
||
return null; | ||
}; | ||
|
||
export default Recaptcha; | ||
|
||
|
||
|
Oops, something went wrong.