rip out all old auth data

This commit is contained in:
Thomas Ruoff
2021-10-02 01:03:02 +02:00
committed by Thomas Ruoff
parent b257bc8258
commit 0e84945ab4
8 changed files with 22 additions and 89 deletions

View File

@@ -1,9 +1,10 @@
import { useContext } from 'react'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import Link from 'next/link' import Link from 'next/link'
import { useSession } from 'next-auth/react'
import User from './user' import User from './user'
import UserContext from '../context/user'
import { USER_ROLE } from '../lib/session'
const pathNameLabelMap = { const pathNameLabelMap = {
'/login': 'Login', '/login': 'Login',
@@ -20,8 +21,8 @@ function getPathNameMap(route: string) {
} }
export default function Navigation() { export default function Navigation() {
const { data, status } = useSession();
const router = useRouter() const router = useRouter()
const { role } = useContext(UserContext)
const pathname = router.pathname const pathname = router.pathname
if (pathname.length === 0 || pathname === '/') { if (pathname.length === 0 || pathname === '/') {
@@ -30,17 +31,13 @@ export default function Navigation() {
const pathLabel = getPathNameMap(pathname) const pathLabel = getPathNameMap(pathname)
if (!pathLabel && role !== USER_ROLE.ADMIN) {
return null
}
return ( return (
<div className="flex flex-row items-center px-3 py-1 text-white text-base bg-blue-400 rounded-b-sm"> <div className="flex flex-row items-center px-3 py-1 text-white text-base bg-blue-400 rounded-b-sm">
<> <>
<h2 className="mx-1"> <h2 className="mx-1">
<span className="font-extrabold">{pathLabel}</span> <span className="font-extrabold">{pathLabel}</span>
</h2> </h2>
{role === USER_ROLE.ADMIN && ( {status === 'authenticated' && data.user.email && (
<Link href="/admin"> <Link href="/admin">
<a className="text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium"> <a className="text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">
Buchungen Buchungen

View File

@@ -1,9 +0,0 @@
import React from 'react'
import { UserData } from '../lib/session'
const UserContext = React.createContext<UserData>({
username: undefined,
role: undefined,
})
export default UserContext

5
package-lock.json generated
View File

@@ -8694,6 +8694,11 @@
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-1.1.76.tgz", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-1.1.76.tgz",
"integrity": "sha512-9/IECtNr8dXNmPWmFXepT0/7o5eolGesHUa3mtr0KlgnCvnZxwh2qensKL42JJY2vQKC3nIBXetFAqR+PW1CmA==" "integrity": "sha512-9/IECtNr8dXNmPWmFXepT0/7o5eolGesHUa3mtr0KlgnCvnZxwh2qensKL42JJY2vQKC3nIBXetFAqR+PW1CmA=="
}, },
"nodemailer": {
"version": "6.6.5",
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.6.5.tgz",
"integrity": "sha512-C/v856DBijUzHcHIgGpQoTrfsH3suKIRAGliIzCstatM2cAa+MYX3LuyCrABiO/cdJTxgBBHXxV1ztiqUwst5A=="
},
"normalize-package-data": { "normalize-package-data": {
"version": "2.5.0", "version": "2.5.0",
"resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz",

View File

@@ -19,6 +19,7 @@
"next-auth": "^4.0.0-beta.2", "next-auth": "^4.0.0-beta.2",
"next-iron-session": "4.1.14", "next-iron-session": "4.1.14",
"next-mdx-remote": "3.0.4", "next-mdx-remote": "3.0.4",
"nodemailer": "^6.6.5",
"p-retry": "4.6.1", "p-retry": "4.6.1",
"react": "17.0.2", "react": "17.0.2",
"react-calendar": "3.4.0", "react-calendar": "3.4.0",

View File

@@ -9,32 +9,18 @@ import { getMilageMax } from '../../../../db/index'
import { daysFormatFrontend } from '../../../../helpers/date' import { daysFormatFrontend } from '../../../../helpers/date'
import { getBillTotal, createBill, patchBill } from '../../../../helpers/bill' import { getBillTotal, createBill, patchBill } from '../../../../helpers/bill'
import { getBookingStatus } from '../../../../helpers/booking' import { getBookingStatus } from '../../../../helpers/booking'
import withSession, {
isAdminSession,
redirectToLogin,
} from '../../../../lib/session'
import { getServerSideBooking } from '../../../../lib/getServerSideProps' import { getServerSideBooking } from '../../../../lib/getServerSideProps'
export const getServerSideProps = withSession(async (context) => { export const getServerSideProps = async (context) => {
const { req, res } = context
const adminUser = isAdminSession(req)
if (!adminUser) {
redirectToLogin(req, res)
return { props: {} }
}
const milageMax = await getMilageMax() const milageMax = await getMilageMax()
const serverSideBookingProps = await getServerSideBooking(context) const serverSideBookingProps = await getServerSideBooking(context)
return { return {
props: { props: {
...serverSideBookingProps.props, ...serverSideBookingProps.props,
milageMax, milageMax,
user: adminUser,
}, },
};
} }
})
const milageTarifOptions = Object.values(MILAGE_TARIFS).map((tarif) => { const milageTarifOptions = Object.values(MILAGE_TARIFS).map((tarif) => {
return ( return (
@@ -217,8 +203,7 @@ export default function BookingBillPage({
> >
- -
</button> </button>
<label className="flabel inline">{`Kostenpunkt ${ <label className="flabel inline">{`Kostenpunkt ${index + 1
index + 1
}`}</label> }`}</label>
</div> </div>
<div className="ml-10 mb-3" key={`input{index}`}> <div className="ml-10 mb-3" key={`input{index}`}>

View File

@@ -1,40 +1,16 @@
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import { GetServerSideProps } from 'next'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import Link from 'next/link' import Link from 'next/link'
import Footer from '../../../../components/footer' import Footer from '../../../../components/footer'
import Header from '../../../../components/header' import Header from '../../../../components/header'
import Calendar from '../../../../components/calendar' import Calendar from '../../../../components/calendar'
import withSession, {
isAdminSession,
redirectToLogin,
} from '../../../../lib/session'
import { getServerSideBooking } from '../../../../lib/getServerSideProps' import { getServerSideBooking } from '../../../../lib/getServerSideProps'
import { Booking } from '../../../../db/booking' import { Booking } from '../../../../db/booking'
import { getBookingStatus, patchBooking } from '../../../../helpers/booking' import { getBookingStatus, patchBooking } from '../../../../helpers/booking'
import { daysFormatFrontend } from '../../../../helpers/date' import { daysFormatFrontend } from '../../../../helpers/date'
import { BOOKING_STATUS } from '../../../../db/enums' import { BOOKING_STATUS } from '../../../../db/enums'
export const getServerSideProps: GetServerSideProps = withSession( export const getServerSideProps = getServerSideBooking;
async (context) => {
const { req, res } = context
const adminUser = isAdminSession(req)
if (!adminUser) {
redirectToLogin(req, res)
return { props: {} }
}
const result = await getServerSideBooking(context)
return {
...result,
// TODO: have a closer look at this type issue. Seems like a bug
// @ts-ignore
props: { ...result.props, user: adminUser },
}
}
)
export default function ShowBookingAdmin({ export default function ShowBookingAdmin({
booking: bookingProp, booking: bookingProp,

View File

@@ -1,12 +1,7 @@
import { Bill } from '../../../../db/bill' import { Bill } from '../../../../db/bill'
import { createBill, patchBill } from '../../../../db/index' import { createBill, patchBill } from '../../../../db/index'
import withSession, { isAdminSession } from '../../../../lib/session'
export default withSession(async function billHandler(req, res): Promise<void> { export default async function billHandler(req, res): Promise<void> {
if (!isAdminSession(req)) {
res.status(403).send({ message: 'Not Authorized' })
return
}
const { const {
method, method,
@@ -41,4 +36,4 @@ export default withSession(async function billHandler(req, res): Promise<void> {
res.setHeader('Allow', ['POST', 'PATCH']) res.setHeader('Allow', ['POST', 'PATCH'])
res.status(405).end(`Method ${method} Not Allowed`) res.status(405).end(`Method ${method} Not Allowed`)
} }
}) }

View File

@@ -2,9 +2,8 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { BookingDocument } from '../../../../db/booking' import { BookingDocument } from '../../../../db/booking'
import { BOOKING_STATUS } from '../../../../db/enums' import { BOOKING_STATUS } from '../../../../db/enums'
import { getBookingByUUID } from '../../../../db/index' import { getBookingByUUID } from '../../../../db/index'
import withSession, { isAdminSession } from '../../../../lib/session'
export default withSession(async function userHandler( export default async function userHandler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse res: NextApiResponse
): Promise<void> { ): Promise<void> {
@@ -21,22 +20,6 @@ export default withSession(async function userHandler(
case 'PATCH': case 'PATCH':
booking = await getBookingByUUID(uuid) booking = await getBookingByUUID(uuid)
if (!isAdminSession(req)) {
const deniedPropsForUser = Object.keys(req.body).filter(
(key) => key !== 'status'
)
if (deniedPropsForUser.length) {
res
.status(400)
.end(
`The following attributes cannot be changed: ${deniedPropsForUser.join(
', '
)}`
)
break
}
}
if (!Object.values(BOOKING_STATUS).includes(req.body.status)) { if (!Object.values(BOOKING_STATUS).includes(req.body.status)) {
res res
.status(400) .status(400)
@@ -60,4 +43,4 @@ export default withSession(async function userHandler(
res.setHeader('Allow', ['PATCH']) res.setHeader('Allow', ['PATCH'])
res.status(405).end(`Method ${method} Not Allowed`) res.status(405).end(`Method ${method} Not Allowed`)
} }
}) }