Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Naman-B-Parlecha committed Sep 22, 2024
2 parents fa52747 + 59d307f commit a68022e
Show file tree
Hide file tree
Showing 34 changed files with 9,065 additions and 4,755 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
71 changes: 42 additions & 29 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,45 @@ jobs:
node-version: [18.x] # specify the Node.js version

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Set up environment variables
run: |
echo "NEXT_PUBLIC_FIREBASE_API_KEY=${{ secrets.NEXT_PUBLIC_FIREBASE_API_KEY }}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=${{ secrets.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN }}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_FIREBASE_PROJECT_ID=${{ secrets.NEXT_PUBLIC_FIREBASE_PROJECT_ID }}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=${{ secrets.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET }}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=${{ secrets.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID }}" >> $GITHUB_ENV
echo "NEXT_PUBLIC_FIREBASE_APP_ID=${{ secrets.NEXT_PUBLIC_FIREBASE_APP_ID }}" >> $GITHUB_ENV
- name: Install dependencies
run: npm install

- name: Build the project
run: npm run build
env:
NEXT_PUBLIC_FIREBASE_API_KEY: ${{ secrets.NEXT_PUBLIC_FIREBASE_API_KEY }}
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN: ${{ secrets.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN }}
NEXT_PUBLIC_FIREBASE_PROJECT_ID: ${{ secrets.NEXT_PUBLIC_FIREBASE_PROJECT_ID }}
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET: ${{ secrets.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET }}
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID: ${{ secrets.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID }}
NEXT_PUBLIC_FIREBASE_APP_ID: ${{ secrets.NEXT_PUBLIC_FIREBASE_APP_ID }}
- name: Checkout code
uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Set up environment variables
env:
NEXT_PUBLIC_FIREBASE_API_KEY: ${{ vars.NEXT_PUBLIC_FIREBASE_API_KEY }}
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN: ${{ vars.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN }}
NEXT_PUBLIC_FIREBASE_PROJECT_ID: ${{ vars.NEXT_PUBLIC_FIREBASE_PROJECT_ID }}
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET: ${{ vars.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET }}
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID: ${{ vars.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID }}
NEXT_PUBLIC_FIREBASE_APP_ID: ${{ vars.NEXT_PUBLIC_FIREBASE_APP_ID }}
run: |
echo "Environment variables are set."
echo "NEXT_PUBLIC_FIREBASE_API_KEY starts with: ${NEXT_PUBLIC_FIREBASE_API_KEY:0:5}..."
echo "NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN: ${NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN}"
echo "NEXT_PUBLIC_FIREBASE_PROJECT_ID: ${NEXT_PUBLIC_FIREBASE_PROJECT_ID}"
echo "NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET: ${NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET}"
echo "NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID: ${NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID}"
echo "NEXT_PUBLIC_FIREBASE_APP_ID: ${NEXT_PUBLIC_FIREBASE_APP_ID}"
- name: Install dependencies
run: npm install

- name: "Debug: Print Environment Variables Before Build"
run: |
echo "Checking environment variables:"
printenv | grep 'NEXT_PUBLIC_' || echo "No NEXT_PUBLIC_ variables found."
- name: Build the project
env:
NEXT_PUBLIC_FIREBASE_API_KEY: ${{ vars.NEXT_PUBLIC_FIREBASE_API_KEY }}
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN: ${{ vars.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN }}
NEXT_PUBLIC_FIREBASE_PROJECT_ID: ${{ vars.NEXT_PUBLIC_FIREBASE_PROJECT_ID }}
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET: ${{ vars.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET }}
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID: ${{ vars.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID }}
NEXT_PUBLIC_FIREBASE_APP_ID: ${{ vars.NEXT_PUBLIC_FIREBASE_APP_ID }}
run: npm run build
64 changes: 64 additions & 0 deletions app/(default)/api/registration/pbctf/route.ts
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!" });
}
178 changes: 127 additions & 51 deletions app/(default)/api/registration/recruitment/route.ts
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" });
}
Loading

0 comments on commit a68022e

Please sign in to comment.