diff --git a/src/server/index.tsx b/src/server/index.tsx index c8726f24..3c427409 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -2,7 +2,7 @@ import express from "express"; import fs from "fs"; import { IncomingHttpHeaders } from "http"; import { Helmet } from "inferno-helmet"; -import { matchPath, StaticRouter } from "inferno-router"; +import { StaticRouter, matchPath } from "inferno-router"; import { renderToString } from "inferno-server"; import IsomorphicCookie from "isomorphic-cookie"; import { GetSite, GetSiteResponse, LemmyHttp } from "lemmy-js-client"; @@ -141,7 +141,7 @@ server.get("/*", async (req, res) => { const routeData = await Promise.all(promises); - // Redirect to the 404 if there's an API error + // Handle API errors if (routeData[0] && routeData[0].error) { const error = routeData[0].error; console.error(error); @@ -209,7 +209,7 @@ server.get("/*", async (req, res) => { ${helmet.link.toString()} - + @@ -223,10 +223,17 @@ server.get("/*", async (req, res) => { -`); + `); } catch (err) { - console.error(err); - return res.send(`404: ${removeAuthParam(err)}`); + var formatted: string; + if (err.status && err.message) { + res.status(err.status); + formatted = `${err.status}: ${err.message}`; + } else { + res.status(500); + formatted = err; + } + res.send(formatted); } }); diff --git a/src/shared/components/home/admin-settings.tsx b/src/shared/components/home/admin-settings.tsx index ab897fe1..100750d1 100644 --- a/src/shared/components/home/admin-settings.tsx +++ b/src/shared/components/home/admin-settings.tsx @@ -17,6 +17,7 @@ import { InitialFetchRequest } from "../../interfaces"; import { WebSocketService } from "../../services"; import { capitalizeFirstLetter, + check_auth, isBrowser, myAuth, randomStr, @@ -60,6 +61,8 @@ export class AdminSettings extends Component { this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); + check_auth(); + // Only fetch the data if coming from another route if (this.isoData.path == this.context.router.route.match.url) { this.state = { diff --git a/src/shared/components/home/setup.tsx b/src/shared/components/home/setup.tsx index 726edc31..6d9dd049 100644 --- a/src/shared/components/home/setup.tsx +++ b/src/shared/components/home/setup.tsx @@ -67,6 +67,9 @@ export class Setup extends Component { } render() { + if (this.state.siteRes.site_view.local_site.site_setup) { + throw { status: 403, message: "Site is already setup" }; + } return (
diff --git a/src/shared/components/person/inbox.tsx b/src/shared/components/person/inbox.tsx index 10af488e..49bd3eed 100644 --- a/src/shared/components/person/inbox.tsx +++ b/src/shared/components/person/inbox.tsx @@ -29,6 +29,7 @@ import { i18n } from "../../i18next"; import { CommentViewType, InitialFetchRequest } from "../../interfaces"; import { UserService, WebSocketService } from "../../services"; import { + check_auth, commentsToFlatNodes, createCommentLikeRes, editCommentRes, @@ -111,10 +112,7 @@ export class Inbox extends Component { this.handleSortChange = this.handleSortChange.bind(this); this.handlePageChange = this.handlePageChange.bind(this); - if (!UserService.Instance.myUserInfo && isBrowser()) { - toast(i18n.t("not_logged_in"), "danger"); - this.context.router.history.push(`/login`); - } + check_auth(); this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); diff --git a/src/shared/utils.ts b/src/shared/utils.ts index da67b629..28d95e14 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -1603,3 +1603,9 @@ export function getQueryString>( "?" ); } + +export function check_auth() { + if (!UserService.Instance.myUserInfo) { + throw { status: 401, message: "Login required" }; + } +}