forked from cox512/vercel-test-site-traditional
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
122 lines (100 loc) · 2.9 KB
/
app.js
File metadata and controls
122 lines (100 loc) · 2.9 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const express = require("express");
const app = express();
const path = require("path");
const jwt = require("jsonwebtoken");
require("dotenv").config();
const PORT = process.env.PORT || 3000;
console.log(
"DEBUG: Server starting with latest code -",
new Date().toISOString()
);
console.log("My port:", PORT);
const routes = require("./routes");
/**
* -------------- GENERAL SETUP ----------------
*/
// Create the Express application
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve static files
app.use(express.static(path.join(__dirname, "public")));
// Set up view engine
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
// Make environment variables available to all EJS templates
app.use((req, res, next) => {
res.locals.env = process.env;
next();
});
/**
* -------------- ROUTES ----------------
*/
// Imports all of the routes from ./routes/index.js
app.use(routes);
app.get("/new-tab", (req, res) => {
res.render("new-tab");
});
app.get("/page-two", (req, res) => {
res.render("page-two");
});
// JWT Generation API endpoint for local development
app.post("/api/generate-jwt", async (req, res) => {
// Set CORS headers
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
try {
// Get the API secret from environment variables
const apiSecret = process.env.INTERCOM_JSON_SECRET;
if (!apiSecret) {
return res.status(500).json({
error: "INTERCOM_JSON_SECRET environment variable is not set",
});
}
// Extract user data from request body
const userData = req.body || {};
// Create the JWT payload
const payload = {
user_id: userData.user_id || "anonymous_user",
email: userData.email || undefined,
name: userData.name || undefined,
// Add any other attributes that were passed
...userData,
};
// Remove undefined values from payload
Object.keys(payload).forEach((key) => {
if (payload[key] === undefined) {
delete payload[key];
}
});
// Generate the JWT token
const token = jwt.sign(payload, apiSecret, {
expiresIn: "1h",
algorithm: "HS256",
});
res.status(200).json({
token,
expires_in: 3600,
payload: payload,
});
} catch (error) {
console.error("JWT generation error:", error);
res.status(500).json({
error: "Failed to generate JWT token",
details: error.message,
});
}
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Something broke!");
});
/**
* -------------- SERVER ----------------
*/
if (process.env.NODE_ENV !== "production") {
app.listen(PORT, () => console.log("Listening on port:", PORT));
}
// Export the Express API
module.exports = app;