import { initializeSite, isAuthPath } from "@utils/app";
+import { getHttpBaseInternal } from "@utils/env";
import { ErrorPageData } from "@utils/types";
+import * as cookie from "cookie";
import fetch from "cross-fetch";
import type { Request, Response } from "express";
import { StaticRouter, matchPath } from "inferno-router";
import { renderToString } from "inferno-server";
-import IsomorphicCookie from "isomorphic-cookie";
import { GetSite, GetSiteResponse, LemmyHttp } from "lemmy-js-client";
import { App } from "../../shared/components/app/app";
-import { getHttpBaseInternal } from "../../shared/env";
import {
InitialFetchRequest,
IsoDataOptionalSite,
export default async (req: Request, res: Response) => {
try {
const activeRoute = routes.find(route => matchPath(req.path, route));
- let auth: string | undefined = IsomorphicCookie.load("jwt", req);
+
+ let auth = req.headers.cookie
+ ? cookie.parse(req.headers.cookie).jwt
+ : undefined;
const getSiteForm: GetSite = { auth };
const headers = setForwardedHeaders(req.headers);
+
const client = wrapClient(
new LemmyHttp(getHttpBaseInternal(), { fetchFunction: fetch, headers })
);
let routeData: RouteData = {};
let errorPageData: ErrorPageData | undefined = undefined;
let try_site = await client.getSite(getSiteForm);
+
if (try_site.state === "failed" && try_site.msg == "not_logged_in") {
console.error(
"Incorrect JWT token, skipping auth so frontend can remove jwt cookie"
routeData = await activeRoute.fetchInitialData(initialFetchReq);
}
+
+ if (!activeRoute) {
+ res.status(404);
+ }
} else if (try_site.state === "failed") {
+ res.status(500);
errorPageData = getErrorPageData(new Error(try_site.msg), site);
}
const error = Object.values(routeData).find(
- res => res.state === "failed"
+ res => res.state === "failed" && res.msg !== "couldnt_find_object" // TODO: find a better way of handling errors
) as FailedRequestState | undefined;
// Redirect to the 404 if there's an API error
if (error) {
console.error(error.msg);
+
if (error.msg === "instance_is_private") {
return res.redirect(`/signup`);
} else {
+ res.status(500);
errorPageData = getErrorPageData(new Error(error.msg), site);
}
}
// If an error is caught here, the error page couldn't even be rendered
console.error(err);
res.statusCode = 500;
+
return res.send(
process.env.NODE_ENV === "development" ? err.message : "Server error"
);