File size: 2,619 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import express, { json } from "express";
import cors from "cors";
import fetch from "node-fetch";
import dotenv from "dotenv";
import Airtable from "airtable";

const app = express();
const port = process.env.PORT || 3000;

dotenv.config({ path: ".env.backend" });
const AIRTABLE_KEY = process.env.AIRTABLE_KEY;
const AIRTABLE_ID = process.env.AIRTABLE_ID;
const CAPTCHA_SECRET = process.env.CAPTCHA_SECRET;

const edgyBase_backend = new Airtable({ apiKey: AIRTABLE_KEY }).base(
  AIRTABLE_ID
);

app.use(
  cors({
    origin: "*",
    methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
    allowedHeaders: ["Content-Type", "Authorization"],
  })
);
app.use(json());

app.get("/", (req, res) => {
  res.send("Server Deployed 🥳");
});

app.get("/airtable", async (req, res) => {
  const airtableUrl = `https://api.airtable.com/v0/${AIRTABLE_ID}/users`;

  try {
    const response = await fetch(airtableUrl, {
      method: "GET",
      headers: {
        Authorization: `Bearer ${AIRTABLE_KEY}`,
        "Content-Type": "application/json",
      },
    });
    if (!response.ok) {
      const errorData = await response.text();
      throw new Error(
        `HTTP error! Status: ${response.status}. Response: ${errorData}`
      );
    }

    const data = await response.json();
    return res.json(data);
  } catch (error) {
    console.error("Error:", error.message);
    return res
      .status(500)
      .json({ error: "An error occurred while fetching data from Airtable." });
  }
});
app.post("/airtable", async (req, res) => {
  const { userId, formValue } = req.body;
  const key = Object.keys(formValue)[0];
  const value = Object.values(formValue)[0];
  const form = { [key]: value };
  try {
    await edgyBase_backend("users").update([
      {
        id: userId,
        fields: form,
      },
    ]);
    res.status(200).json({ success: true });
  } catch (error) {
    res.status(500).json({ success: false, reason: error.message });
  }
});

app.post("/verify-recaptcha", async (req, res) => {
  const { token } = req.body;
  const uri = `https://www.google.com/recaptcha/api/siteverify?secret=${CAPTCHA_SECRET}&response=${token}`;

  fetch(uri, {
    method: "post",
  })
    .then((response) => response.json())
    .then((google_response) => {
      if (google_response.success === true) {
        return res.send({ response: "Successful" });
      } else {
        return res.send({ response: "Failed" });
      }
    })
    .catch((error) => {
      return res.json({ error });
    });
});

app.listen(port, () =>
  console.log(`Server is running on http://localhost:${port}`)
);