-
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.
Merge branch 'main' of https://github.com/Naman-B-Parlecha/pbwebsite
- Loading branch information
Showing
34 changed files
with
9,065 additions
and
4,755 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,3 @@ | ||
{ | ||
"extends": "next/core-web-vitals" | ||
} |
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,64 @@ | ||
import { db } from "@/Firebase"; | ||
|
||
import { recruitValidate } from "@/lib/server/utils"; | ||
|
||
import { | ||
addDoc, | ||
collection, | ||
getDocs, | ||
limit, | ||
query, | ||
where, | ||
} from "firebase/firestore"; | ||
import { NextResponse } from "next/server"; | ||
|
||
// Add a new registration | ||
export async function POST(request: Request) { | ||
const formData = await request.json(); | ||
const { recaptcha_token } = formData; | ||
|
||
|
||
const recaptchaToken = recaptcha_token; | ||
|
||
const details = { | ||
event: { | ||
token: recaptchaToken, | ||
siteKey: process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY, | ||
}, | ||
}; | ||
|
||
|
||
if (!recaptchaToken) { | ||
return NextResponse.json( | ||
{ | ||
message: "reCAPTCHA token not found! Try again", | ||
error: "reCAPTCHA token not found!", | ||
}, | ||
{ | ||
status: 500, | ||
} | ||
); | ||
} | ||
|
||
// Verify the reCATPTCHA token | ||
|
||
const recaptchaResponse = await fetch( | ||
`https://recaptchaenterprise.googleapis.com/v1/projects/${process.env.RECAPTCHA_PROJECT}/assessments?key=${process.env.RECAPTCHA_API_KEY}`, | ||
{ | ||
method: "POST", | ||
body: JSON.stringify(details), | ||
} | ||
); | ||
|
||
const recaptchaResult = await recaptchaResponse.json(); | ||
console.log(recaptchaResult.riskAnalysis.score); | ||
if (recaptchaResult.riskAnalysis.score < 0.7) { | ||
return NextResponse.json({ | ||
message: "reCAPTCHA validation failed", | ||
error: recaptchaResult["error-codes"], | ||
}); | ||
} | ||
|
||
// Return a response | ||
return NextResponse.json({ message: "Recaptcha validated!" }); | ||
} |
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 |
---|---|---|
@@ -1,77 +1,153 @@ | ||
import { db } from "@/Firebase"; | ||
import { recruitValidate } from "@/lib/utils"; | ||
import { addDoc, collection, getDocs } from "firebase/firestore"; | ||
|
||
import { recruitValidate } from "@/lib/server/utils"; | ||
|
||
import { | ||
addDoc, | ||
collection, | ||
getDocs, | ||
limit, | ||
query, | ||
where, | ||
} 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`, | ||
// Add a new registration | ||
export async function POST(request: Request) { | ||
const formData = await request.json(); | ||
const { recaptcha_token, ...data } = formData; | ||
const { email, whatsapp_number, college_id } = formData; | ||
|
||
// Only one Registration per person | ||
|
||
// Query for email | ||
const emailQuery = query( | ||
collection(db, "recruitment2024"), | ||
where("email", "==", email) | ||
); | ||
|
||
// Query for phone number | ||
const phoneQuery = query( | ||
collection(db, "recruitment2024"), | ||
where("whatsapp_number", "==", whatsapp_number) | ||
); | ||
|
||
// Query for college ID | ||
const collegeIdQuery = query( | ||
collection(db, "recruitment2024"), | ||
where("college_id", "==", college_id) | ||
); | ||
|
||
// Fetch results from all queries | ||
const [emailSnapshot, phoneSnapshot, collegeIdSnapshot] = await Promise.all([ | ||
getDocs(emailQuery), | ||
getDocs(phoneQuery), | ||
getDocs(collegeIdQuery) | ||
]); | ||
|
||
|
||
console.log(!emailSnapshot.empty); | ||
|
||
if (!emailSnapshot.empty) { | ||
return NextResponse.json( | ||
{ | ||
message: "Email is already registered!", | ||
error: "Email is already registered!", | ||
}, | ||
|
||
{ | ||
status: 500, | ||
} | ||
); | ||
} | ||
if (!phoneSnapshot.empty) { | ||
return NextResponse.json( | ||
{ | ||
message: "Whatsapp No. is already registered!", | ||
error: "Whatsapp No. is already registered!", | ||
}, | ||
|
||
{ | ||
status: 500, | ||
} | ||
); | ||
} | ||
if (!collegeIdSnapshot.empty) { | ||
return NextResponse.json( | ||
{ | ||
message: "College Id is already registered!", | ||
error: "College Id is already registered!", | ||
}, | ||
|
||
{ | ||
status: 500, | ||
} | ||
); | ||
} | ||
|
||
const recaptchaToken = recaptcha_token; | ||
|
||
const details = { | ||
event: { | ||
token: recaptchaToken, | ||
siteKey: process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY, | ||
}, | ||
}; | ||
|
||
|
||
if (!recaptchaToken) { | ||
return NextResponse.json( | ||
{ | ||
message: "reCAPTCHA token not found! Try again", | ||
error: "reCAPTCHA token not found!", | ||
}, | ||
{ | ||
status: 500, | ||
} | ||
); | ||
} | ||
|
||
// Verify the reCATPTCHA token | ||
|
||
const recaptchaResponse = await fetch( | ||
`https://recaptchaenterprise.googleapis.com/v1/projects/${process.env.RECAPTCHA_PROJECT}/assessments?key=${process.env.RECAPTCHA_API_KEY}`, | ||
{ | ||
method: "POST", | ||
headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
body: new URLSearchParams({ | ||
secret: secretKey || "", | ||
response: token, | ||
}), | ||
body: JSON.stringify(details), | ||
} | ||
); | ||
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) { | ||
const recaptchaResult = await recaptchaResponse.json(); | ||
console.log(recaptchaResult.riskAnalysis.score); | ||
if (recaptchaResult.riskAnalysis.score < 0.7) { | ||
return NextResponse.json({ | ||
message: "reCAPTCHA verification failed", | ||
error: true, | ||
message: "reCAPTCHA validation failed", | ||
error: recaptchaResult["error-codes"], | ||
}); | ||
} | ||
|
||
// 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 }); | ||
return NextResponse.json( | ||
{ message: "Validation error", error: val.error }, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
// Save to Firebase | ||
|
||
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 NextResponse.json( | ||
{ message: "An error occurred", error }, | ||
{ status: 500 } | ||
); | ||
} | ||
// 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 }); | ||
} | ||
return NextResponse.json({ message: "Registration successful" }); | ||
} |
Oops, something went wrong.