-
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/pbdsce/pbwebsite
- Loading branch information
Showing
11 changed files
with
300 additions
and
346 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
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,62 +1,67 @@ | ||
import { db } from "@/Firebase"; | ||
import { sihValidate } from "@/lib/utils"; | ||
import { addDoc, collection, getDocs } from "firebase/firestore"; | ||
import { NextResponse } from "next/server"; | ||
import { db } from '@/Firebase'; | ||
import { sihValidate } 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 formData = await request.json(); | ||
const { recaptcha_token, ...data } = formData; | ||
const recaptchaToken = recaptcha_token; | ||
if (!recaptchaToken) { | ||
throw new Error("Token not found!"); | ||
} | ||
const recaptchaSecretKey = process.env.RECAPTCHA_SECRET_KEY; | ||
|
||
// Verify reCAPTCHA token | ||
const recaptchaResponse = await fetch( | ||
`https://www.google.com/recaptcha/api/siteverify?secret=${recaptchaSecretKey}&response=${recaptchaToken}`, | ||
{ method: "POST" } | ||
); | ||
const recaptchaResult = await recaptchaResponse.json(); | ||
console.log(data); | ||
console.log(recaptchaResult); | ||
if (!recaptchaResult.success) { | ||
return NextResponse.json({ | ||
message: "reCAPTCHA validation failed", | ||
error: recaptchaResult["error-codes"], | ||
}); | ||
} | ||
|
||
// Validate the data | ||
const val = sihValidate(data); | ||
|
||
if (val.error) { | ||
return NextResponse.json({ message: "Validation error", error: val.error }); | ||
} | ||
|
||
try { | ||
// Save to Firebase | ||
const docRef = await addDoc(collection(db, "sih2024"), 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 }); | ||
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 = sihValidate(data); | ||
|
||
if (val.error) { | ||
return NextResponse.json({ message: 'Validation error', error: val.error }); | ||
} | ||
|
||
try { | ||
console.log('Data:', data); | ||
// Save to Firebase | ||
const docRef = await addDoc(collection(db, "sih2024"), 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 sih2024 collection | ||
const querySnapshot = await getDocs(collection(db, "sih2024")); | ||
// 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 }); | ||
} | ||
} | ||
try { | ||
// Get all registrations in sih2024 collection | ||
const querySnapshot = await getDocs(collection(db, "sih2024")); | ||
// 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
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; | ||
|
||
|
||
|
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
Oops, something went wrong.