clean up type mess in SSR functions

This commit is contained in:
Thomas Ruoff
2020-11-06 22:51:56 +01:00
parent ecfa0c2c06
commit 71c16a289d
4 changed files with 42 additions and 28 deletions

View File

@@ -1,7 +1,14 @@
import { GetServerSideProps } from 'next'
import { getBookingByUUID } from '../db/index'
export const getServerSideBooking: GetServerSideProps = async (context) => {
export interface ServerSideBooking {
props: {
booking: object
}
}
export const getServerSideBooking = async (
context: any
): Promise<ServerSideBooking> => {
const {
res,
params: { uuid: uuids },
@@ -12,14 +19,14 @@ export const getServerSideBooking: GetServerSideProps = async (context) => {
if (!booking) {
res.statusCode = 404
res.end()
return { props: {} }
return { props: { booking: null } }
}
await booking.populate('booker').execPopulate()
await booking.populate('bill').execPopulate()
// TODO: hack, not sure why _id is not serilizable
const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON()))
const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON())) as object
return {
props: { booking: bookingJSON },
}

View File

@@ -1,6 +1,15 @@
import { withIronSession, Handler } from 'next-iron-session'
import { getBaseURL } from '../helpers/url'
export enum USER_ROLE {
ADMIN = 'admin',
}
export interface UserData {
username: string
role: USER_ROLE
}
const SESSION_SECRET =
process.env.SESSION_SECRET || 'dev-env-default-secret-991823723'
@@ -16,13 +25,13 @@ export default function withSession(handler: Handler) {
})
}
export function isAdminSession(req: any, res: any) {
const user = req?.session.get('user')
if (user && user.role === 'admin') {
export function isAdminSession(req: any) {
const user = req?.session.get('user') as UserData
if (user && user.role === USER_ROLE.ADMIN) {
return user
}
return false
return null
}
export function redirectToLogin(req: any, res: any) {