|
| 1 | +import { NextResponse, NextRequest } from "next/server"; |
| 2 | +import { getDatabase, ref, onValue } from "firebase/database"; |
| 3 | +import emailjs from "emailjs-com"; |
| 4 | + |
| 5 | +const db = getDatabase(); |
| 6 | +const movementRef = ref(db, "movement"); |
| 7 | + |
| 8 | +// Function to fetch email list from backend |
| 9 | +const fetchEmails = async (token: string) => { |
| 10 | + try { |
| 11 | + const response = await fetch("/api/getMembers", { |
| 12 | + method: "GET", |
| 13 | + headers: { |
| 14 | + "Content-Type": "application/json", |
| 15 | + "authorization": `Bearer ${token}`, |
| 16 | + |
| 17 | + } |
| 18 | + }); |
| 19 | + |
| 20 | + const data = await response.json(); |
| 21 | + return data; |
| 22 | + } catch (error) { |
| 23 | + console.error("Error in fetchEmails:", error); |
| 24 | + return { error: "Failed to fetch members" }; |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +// API Handler |
| 29 | +export async function GET(req: NextRequest) { |
| 30 | + // Extract token from headers |
| 31 | + const authHeader = req.headers.get("authorization"); |
| 32 | + if (!authHeader) { |
| 33 | + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); |
| 34 | + } |
| 35 | + |
| 36 | + const token = authHeader.split(" ")[1]; |
| 37 | + console.log("Received Token in API:", token); |
| 38 | + |
| 39 | + // Fetch members using token |
| 40 | + const data = await fetchEmails(token); |
| 41 | + console.log("Fetched Members:", data); |
| 42 | + |
| 43 | + return NextResponse.json({ message: "API Email Trigger Response", data }, { status: 200 }); |
| 44 | +} |
0 commit comments