From af3262e55e6932a78019733f15a6d9e9b6380b51 Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Wed, 11 Sep 2024 23:44:31 +0200 Subject: [PATCH] cookie's now only used under /admin --- components/auth.tsx | 21 +++++++++++ components/header.tsx | 15 ++++---- components/user.tsx | 2 +- helpers/withAuth.tsx | 25 +++++++++++++ pages/_app.tsx | 52 +++++---------------------- pages/admin/bookings/[uuid]/bill.tsx | 12 ++++--- pages/admin/bookings/[uuid]/index.tsx | 3 +- pages/admin/index.tsx | 8 ++--- 8 files changed, 77 insertions(+), 61 deletions(-) create mode 100644 components/auth.tsx create mode 100644 helpers/withAuth.tsx diff --git a/components/auth.tsx b/components/auth.tsx new file mode 100644 index 0000000..7c8355b --- /dev/null +++ b/components/auth.tsx @@ -0,0 +1,21 @@ +import { useEffect } from 'react' + +import { useSession, signIn } from 'next-auth/react' + +export default function Auth({ children }) { + const { data: session, status } = useSession() + const isUser = !!session?.user + + useEffect(() => { + if (status === 'loading') return // Do nothing while loading + if (!isUser) signIn() // If not authenticated, force log in + }, [isUser, status]) + + if (isUser) { + return children + } + + // Session is being fetched, or no user. + // If no user, useEffect() will redirect. + return
Loading...
+} diff --git a/components/header.tsx b/components/header.tsx index 99990fa..8fe97f1 100644 --- a/components/header.tsx +++ b/components/header.tsx @@ -1,4 +1,5 @@ import { useState } from 'react' +import { createPortal } from 'react-dom' import Head from 'next/head' import Link from 'next/link' import { useRouter } from 'next/router' @@ -43,7 +44,10 @@ export default function Header() {
- + Pfadi-Bussle
@@ -60,7 +64,6 @@ export default function Header() { ) })} -
) -} \ No newline at end of file +} diff --git a/components/user.tsx b/components/user.tsx index 16aa416..4630532 100644 --- a/components/user.tsx +++ b/components/user.tsx @@ -24,4 +24,4 @@ export default function User() { ) -} \ No newline at end of file +} diff --git a/helpers/withAuth.tsx b/helpers/withAuth.tsx new file mode 100644 index 0000000..4a79e5e --- /dev/null +++ b/helpers/withAuth.tsx @@ -0,0 +1,25 @@ +import { SessionProvider } from 'next-auth/react' +import Auth from '../components/auth' +import User from '../components/user' + +// This is the HOC +function withAuth(WrappedComponent) { + // Return a new component + function withAuth({ session, ...pageProps }) { + // Render the WrappedComponent with additional props + return ( + + + + + + + ) + } + + withAuth.displayName = `withAuth(${WrappedComponent.displayName}` + + return withAuth +} + +export default withAuth diff --git a/pages/_app.tsx b/pages/_app.tsx index fa0491a..360dadc 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -1,52 +1,16 @@ -import { AxiomWebVitals } from 'next-axiom' -import { Analytics } from '@vercel/analytics/react'; -import { useEffect } from 'react' - -import type { AppProps } from 'next/app' - -import { useSession, signIn, SessionProvider } from 'next-auth/react' -import Layout from '../components/layout'; +import { Analytics } from '@vercel/analytics/react' +import Layout from '../components/layout' import '../styles/index.css' import '../styles/gfm.css' -function Auth({ children }) { - const { data: session, status } = useSession() - const isUser = !!session?.user - - useEffect(() => { - if (status === 'loading') return // Do nothing while loading - if (!isUser) signIn() // If not authenticated, force log in - }, [isUser, status]) - - if (isUser) { - return children - } - - // Session is being fetched, or no user. - // If no user, useEffect() will redirect. - return
Loading...
-} - -export default function MyApp({ - Component, - pageProps: { session, ...pageProps }, -}) { +export default function MyApp({ Component, pageProps }) { return (
- - - {Component.authenticationRequired ? ( - - - - ) : ( - - )} - - - - + + + +
) -} \ No newline at end of file +} diff --git a/pages/admin/bookings/[uuid]/bill.tsx b/pages/admin/bookings/[uuid]/bill.tsx index 669f545..0b917cb 100644 --- a/pages/admin/bookings/[uuid]/bill.tsx +++ b/pages/admin/bookings/[uuid]/bill.tsx @@ -9,6 +9,7 @@ import { log } from '../../../../helpers/log' import { getBillTotal, createBill, patchBill } from '../../../../helpers/bill' import { getBookingStatus } from '../../../../helpers/booking' import { getServerSideBooking } from '../../../../lib/getServerSideProps' +import withAuth from '../../../../helpers/withAuth' export const getServerSideProps = async (context) => { const milageMax = await getMilageMax() @@ -114,14 +115,14 @@ function BookingBillPage({ setStoringInProgress(false) } - const onAddAdditionalCost = function( + const onAddAdditionalCost = function ( event: React.MouseEvent ) { event.preventDefault() setAdditionalCosts([...additionalCosts, { name: '', value: 0 }]) } - const onRemoveAdditionalCost = function( + const onRemoveAdditionalCost = function ( event: React.MouseEvent, index: number ) { @@ -197,8 +198,9 @@ function BookingBillPage({ > - - +