mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
clean up type mess in SSR functions
This commit is contained in:
@@ -1,7 +1,14 @@
|
|||||||
import { GetServerSideProps } from 'next'
|
|
||||||
import { getBookingByUUID } from '../db/index'
|
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 {
|
const {
|
||||||
res,
|
res,
|
||||||
params: { uuid: uuids },
|
params: { uuid: uuids },
|
||||||
@@ -12,14 +19,14 @@ export const getServerSideBooking: GetServerSideProps = async (context) => {
|
|||||||
if (!booking) {
|
if (!booking) {
|
||||||
res.statusCode = 404
|
res.statusCode = 404
|
||||||
res.end()
|
res.end()
|
||||||
return { props: {} }
|
return { props: { booking: null } }
|
||||||
}
|
}
|
||||||
|
|
||||||
await booking.populate('booker').execPopulate()
|
await booking.populate('booker').execPopulate()
|
||||||
await booking.populate('bill').execPopulate()
|
await booking.populate('bill').execPopulate()
|
||||||
|
|
||||||
// TODO: hack, not sure why _id is not serilizable
|
// 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 {
|
return {
|
||||||
props: { booking: bookingJSON },
|
props: { booking: bookingJSON },
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
import { withIronSession, Handler } from 'next-iron-session'
|
import { withIronSession, Handler } from 'next-iron-session'
|
||||||
import { getBaseURL } from '../helpers/url'
|
import { getBaseURL } from '../helpers/url'
|
||||||
|
|
||||||
|
export enum USER_ROLE {
|
||||||
|
ADMIN = 'admin',
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserData {
|
||||||
|
username: string
|
||||||
|
role: USER_ROLE
|
||||||
|
}
|
||||||
|
|
||||||
const SESSION_SECRET =
|
const SESSION_SECRET =
|
||||||
process.env.SESSION_SECRET || 'dev-env-default-secret-991823723'
|
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) {
|
export function isAdminSession(req: any) {
|
||||||
const user = req?.session.get('user')
|
const user = req?.session.get('user') as UserData
|
||||||
if (user && user.role === 'admin') {
|
if (user && user.role === USER_ROLE.ADMIN) {
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
export function redirectToLogin(req: any, res: any) {
|
export function redirectToLogin(req: any, res: any) {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { GetServerSideProps } from 'next'
|
|
||||||
import Footer from '../../../../components/footer'
|
import Footer from '../../../../components/footer'
|
||||||
import Header from '../../../../components/header'
|
import Header from '../../../../components/header'
|
||||||
import Input from '../../../../components/input'
|
import Input from '../../../../components/input'
|
||||||
@@ -17,27 +16,26 @@ import withSession, {
|
|||||||
} from '../../../../lib/session'
|
} from '../../../../lib/session'
|
||||||
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
|
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
|
||||||
|
|
||||||
export const getServerSideProps: GetServerSideProps = withSession(
|
export const getServerSideProps = withSession(async (context) => {
|
||||||
async (context) => {
|
const { req, res } = context
|
||||||
const { req, res } = context
|
|
||||||
|
|
||||||
const adminUser = isAdminSession(req, res)
|
const adminUser = isAdminSession(req)
|
||||||
|
|
||||||
if (!adminUser) {
|
if (!adminUser) {
|
||||||
redirectToLogin(req, res)
|
redirectToLogin(req, res)
|
||||||
return { props: {} }
|
return { props: {} }
|
||||||
}
|
|
||||||
|
|
||||||
const milageMax = await getMilageMax()
|
|
||||||
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, milageMax, user: adminUser },
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
const milageMax = await getMilageMax()
|
||||||
|
const serverSideBookingProps = await getServerSideBooking(context)
|
||||||
|
return {
|
||||||
|
props: {
|
||||||
|
...serverSideBookingProps.props.booking,
|
||||||
|
milageMax,
|
||||||
|
user: adminUser,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const milageTarifOptions = Object.values(MILAGE_TARIFS).map((tarif) => {
|
const milageTarifOptions = Object.values(MILAGE_TARIFS).map((tarif) => {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ export const getServerSideProps: GetServerSideProps = withSession(
|
|||||||
async (context) => {
|
async (context) => {
|
||||||
const { req, res } = context
|
const { req, res } = context
|
||||||
|
|
||||||
const adminUser = isAdminSession(req, res)
|
const adminUser = isAdminSession(req)
|
||||||
|
|
||||||
if (!adminUser) {
|
if (!adminUser) {
|
||||||
redirectToLogin(req, res)
|
redirectToLogin(req, res)
|
||||||
|
|||||||
Reference in New Issue
Block a user