Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️
PHP attendize-alternativedoor-managementevent-check-inevent-managementevent-registrationevent-ticketingeventbrite-alternativeeventslaravelopen-sourcephpreactself-hostedsell-ticketssell-tickets-onlineticket-sellingticketing-systemtickettailor-alternativetito-alternative
Fix error handling + token being persisted between sessions
Dave Earleyce4b97ad66ed
diff --git a/frontend/server.js b/frontend/server.jsindex 4fb8b0ad6..3bba255ac 100644--- a/frontend/server.js+++ b/frontend/server.js@@ -94,11 +94,16 @@ app.use("*", async (req, res) => {res.setHeader("Content-Type", "text/html");return res.status(200).end(html);} catch (error) {- if (!isProduction) {- vite.ssrFixStacktrace(error);+ if (error instanceof Response) {+ if (error.status >= 300 && error.status < 400) {+ return res.redirect(error.status, error.headers.get("Location") || "/");+ } else {+ return res.status(error.status).send(await error.text());+ }}- console.log(error.stack);- res.status(500).end(error.stack);++ console.error(error);+ res.status(500).send("Internal Server Error");}});diff --git a/frontend/src/App.tsx b/frontend/src/App.tsxindex ffdca9280..808d0b15a 100644--- a/frontend/src/App.tsx+++ b/frontend/src/App.tsx@@ -24,7 +24,6 @@ import {messages as fr} from "./locales/fr.po";import {messages as pt} from "./locales/pt.po";// @ts-ignoreimport {messages as es} from "./locales/es.po";-import {setAuthToken} from "./utilites/apiClient.ts";import {isSsr} from "./utilites/helpers.ts";declare global {@@ -63,7 +62,6 @@ export const App: FC<PropsWithChildren<{queryClient: QueryClient;helmetContext?: any;- token?: string;}>> = (props) => {const [isLoadedOnBrowser, setIsLoadedOnBrowser] = React.useState(false);@@ -74,10 +72,6 @@ export const App: FC<dynamicActivate(getSupportedLocale());}- if (props.token) {- setAuthToken(props.token);- }-useEffect(() => {setIsLoadedOnBrowser(!isSsr());}, [])diff --git a/frontend/src/api/client.ts b/frontend/src/api/client.tsindex 75965426d..b659dd97e 100644--- a/frontend/src/api/client.ts+++ b/frontend/src/api/client.ts@@ -26,12 +26,6 @@ export const api = axios.create({withCredentials: true,});--const existingToken = typeof window !== "undefined" ? window.localStorage.getItem('token') : undefined;-if (existingToken) {- setAuthToken(existingToken);-}-api.interceptors.response.use((response) => {// Securely update the token on each responsediff --git a/frontend/src/entry.client.tsx b/frontend/src/entry.client.tsxindex f7a97874d..33a95d0fb 100644--- a/frontend/src/entry.client.tsx+++ b/frontend/src/entry.client.tsx@@ -1,67 +1,57 @@import ReactDOM from "react-dom/client";-import {- createBrowserRouter,- matchRoutes,- RouterProvider,-} from "react-router-dom";-import { hydrate } from "@tanstack/react-query";+import {createBrowserRouter, matchRoutes, RouterProvider,} from "react-router-dom";+import {hydrate} from "@tanstack/react-query";-import { router } from "./router";-import { App } from "./App";-import { queryClient } from "./utilites/queryClient";+import {router} from "./router";+import {App} from "./App";+import {queryClient} from "./utilites/queryClient";declare global {- interface Window {- __REHYDRATED_STATE__?: unknown;- __EXTRA_STATE__? : {- token?: string;- };- }+ interface Window {+ __REHYDRATED_STATE__?: unknown;+ }}if (window.__REHYDRATED_STATE__) {- hydrate(queryClient, window.__REHYDRATED_STATE__);+ hydrate(queryClient, window.__REHYDRATED_STATE__);}-initClientApp();async function initClientApp() {- if (window.__REHYDRATED_STATE__) {- // Determine if any of the initial routes are lazy- const lazyMatches = matchRoutes(router, window.location)?.filter(- (m) => m.route.lazy- );-- // Load the lazy matches and update the routes before creating your router- // so we can hydrate the SSR-rendered content synchronously- if (lazyMatches && lazyMatches?.length > 0) {- await Promise.all(- lazyMatches.map(async (m) => {- const routeModule = await m.route.lazy?.();- Object.assign(m.route, { ...routeModule, lazy: undefined });- })- );+ if (window.__REHYDRATED_STATE__) {+ // Determine if any of the initial routes are lazy+ const lazyMatches = matchRoutes(router, window.location)?.filter(+ (m) => m.route.lazy+ );++ // Load the lazy matches and update the routes before creating your router+ // so we can hydrate the SSR-rendered content synchronously+ if (lazyMatches && lazyMatches?.length > 0) {+ await Promise.all(+ lazyMatches.map(async (m) => {+ const routeModule = await m.route.lazy?.();+ Object.assign(m.route, {...routeModule, lazy: undefined});+ })+ );+ }++ const browserRouter = createBrowserRouter(router);++ ReactDOM.hydrateRoot(+ document.getElementById("app") as HTMLElement,+ <App queryClient={queryClient}>+ <RouterProvider router={browserRouter} fallbackElement={null}/>+ </App>+ );+ } else {+ ReactDOM.createRoot(document.getElementById("app") as HTMLElement).render(+ <App queryClient={queryClient}>+ <RouterProvider+ router={createBrowserRouter(router)}+ fallbackElement={null}+ />+ </App>+ );}-- const browserRouter = createBrowserRouter(router);-- const extraState = window.__EXTRA_STATE__;-- ReactDOM.hydrateRoot(- document.getElementById("app") as HTMLElement,- <App queryClient={queryClient} token={extraState?.token}>- <RouterProvider router={browserRouter} fallbackElement={null} />- </App>- );- } else {- ReactDOM.createRoot(document.getElementById("app") as HTMLElement).render(- <App queryClient={queryClient}>- <RouterProvider- router={createBrowserRouter(router)}- fallbackElement={null}- />- </App>- );- }}diff --git a/frontend/src/entry.server.tsx b/frontend/src/entry.server.tsxindex d7a5db47a..9d45a1085 100644--- a/frontend/src/entry.server.tsx+++ b/frontend/src/entry.server.tsx@@ -6,6 +6,7 @@ import {createStaticHandler, createStaticRouter, StaticRouterProvider,} from "reimport {router} from "./router";import {App} from "./App";import {queryClient} from "./utilites/queryClient";+import {setAuthToken} from "./utilites/apiClient.ts";const helmetContext = {};@@ -13,6 +14,8 @@ export async function render(params: {req: express.Request;res: express.Response;}) {+ setAuthToken(params.req.cookies.token);+const {query, dataRoutes} = createStaticHandler(router);const remixRequest = createFetchRequest(params.req, params.res);const context = await query(remixRequest);@@ -23,7 +26,7 @@ export async function render(params: {const routerWithContext = createStaticRouter(dataRoutes, context);const appHtml = ReactDOMServer.renderToString(- <App queryClient={queryClient} helmetContext={helmetContext} token={params.req.cookies.token}>+ <App queryClient={queryClient} helmetContext={helmetContext}><StaticRouterProviderrouter={routerWithContext}context={context}diff --git a/frontend/src/router.tsx b/frontend/src/router.tsxindex bc8bd6320..829d34687 100644--- a/frontend/src/router.tsx+++ b/frontend/src/router.tsx@@ -324,9 +324,15 @@ export const router: RouteObject[] = [}return {event, promoCodeValid, promoCode};- } catch (error) {+ } catch (error: any) {+ // for 404s we want to return null so that the 404 page is shown+ if (error?.response?.status === 404) {+ return {event: null, promoCodeValid: undefined, promoCode: null};+ }+console.error(error);- return {event: null, promoCodeValid: undefined, promoCode: null};++ throw error;}},async lazy() {diff --git a/frontend/src/utilites/apiClient.ts b/frontend/src/utilites/apiClient.tsindex a9531387a..e5c02e972 100644--- a/frontend/src/utilites/apiClient.ts+++ b/frontend/src/utilites/apiClient.ts@@ -1,7 +1,15 @@import {api} from "../api/client.ts";import {publicApi} from "../api/public-client.ts";-export const setAuthToken = (token: string) => {+export const setAuthToken = (token?: string | undefined | null) => {+ if (!token) {+ console.log('No token provided');+ delete api.defaults.headers.common['Authorization'];+ delete publicApi.defaults.headers.common['Authorization'];+ return;+ }++ console.log('Setting token:', token);api.defaults.headers.common['Authorization'] = `Bearer ${token}`;publicApi.defaults.headers.common['Authorization'] = `Bearer ${token}`;};