-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
72 lines (56 loc) · 1.68 KB
/
app.ts
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
import express, { RequestHandler } from "express";
import {
TinaNodeBackend,
LocalBackendAuthProvider,
} from "@tinacms/datalayer";
import { AuthJsBackendAuthProvider, TinaAuthJSOptions } from "tinacms-authjs";
import cookieParser from "cookie-parser";
import cors from "cors";
import dotenv from "dotenv";
import { databaseClient } from "./tina/__generated__/databaseClient";
dotenv.config();
if (!process.env.NEXTAUTH_SECRET) {
throw new Error("NEXTAUTH_SECRET must be defined")
}
if (!process.env.GITHUB_OWNER) {
throw new Error("GITHUB_OWNER must be defined")
}
if (!process.env.GITHUB_REPO) {
throw new Error("GITHUB_REPO must be defined")
}
if (!process.env.GITHUB_PERSONAL_ACCESS_TOKEN) {
throw new Error("GITHUB_PERSONAL_ACCESS_TOKEN must be defined")
}
const port = process.env.PORT || 3000;
const app = express();
app.use(express.static("_site"));
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(express.json());
app.use(cookieParser());
const isLocal = process.env.TINA_PUBLIC_IS_LOCAL === "true";
const handler = TinaNodeBackend({
authProvider: isLocal
? LocalBackendAuthProvider()
: AuthJsBackendAuthProvider({
authOptions: TinaAuthJSOptions({
databaseClient,
secret: process.env.NEXTAUTH_SECRET!,
}),
}),
databaseClient,
});
const handleTina: RequestHandler = async (req, res) => {
req.query = {
...(req.query || {}),
routes: req.params[0].split("/"),
};
await handler(req, res);
};
app.all("/api/tina/*", async (req, res, next) => {
// Modify request if needed
handleTina(req, res, next);
});
app.listen(port, () => {
console.log(`express backend listing on port ${port}`);
});