mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 22:47:15 +01:00
make isAdminSession usable by api routes
This commit is contained in:
@@ -12,17 +12,20 @@ export default function withSession(handler: Handler) {
|
|||||||
// the next line allows to use the session in non-https environements like
|
// the next line allows to use the session in non-https environements like
|
||||||
// Next.js dev mode (http://localhost:3000)
|
// Next.js dev mode (http://localhost:3000)
|
||||||
secure: process.env.NODE_ENV === 'production',
|
secure: process.env.NODE_ENV === 'production',
|
||||||
path: '/admin',
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const isAdminSession = function (req: any, res: any) {
|
export function isAdminSession(req: any, res: any) {
|
||||||
const user = req?.session.get('user')
|
const user = req?.session.get('user')
|
||||||
if (user && user.role === 'admin') {
|
if (user && user.role === 'admin') {
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
export function redirectToLogin(req: any, res: any) {
|
||||||
const redirectTargetUrl = `${getBaseURL()}/admin/login?redirect=${encodeURIComponent(
|
const redirectTargetUrl = `${getBaseURL()}/admin/login?redirect=${encodeURIComponent(
|
||||||
req.url
|
req.url
|
||||||
)}`
|
)}`
|
||||||
@@ -31,6 +34,4 @@ export const isAdminSession = function (req: any, res: any) {
|
|||||||
Location: redirectTargetUrl,
|
Location: redirectTargetUrl,
|
||||||
})
|
})
|
||||||
res.end()
|
res.end()
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,10 @@ import { getMilageMax } from '../../../../db/index'
|
|||||||
import { dateFormatFrontend } from '../../../../helpers/date'
|
import { dateFormatFrontend } from '../../../../helpers/date'
|
||||||
import { getBillTotal } from '../../../../helpers/bill'
|
import { getBillTotal } from '../../../../helpers/bill'
|
||||||
import { getBookingStatus } from '../../../../helpers/booking'
|
import { getBookingStatus } from '../../../../helpers/booking'
|
||||||
import withSession, { isAdminSession } from '../../../../lib/session'
|
import withSession, {
|
||||||
|
isAdminSession,
|
||||||
|
redirectToLogin,
|
||||||
|
} from '../../../../lib/session'
|
||||||
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
|
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
|
||||||
|
|
||||||
export const getServerSideProps: GetServerSideProps = withSession(
|
export const getServerSideProps: GetServerSideProps = withSession(
|
||||||
@@ -21,6 +24,7 @@ export const getServerSideProps: GetServerSideProps = withSession(
|
|||||||
const adminUser = isAdminSession(req, res)
|
const adminUser = isAdminSession(req, res)
|
||||||
|
|
||||||
if (!adminUser) {
|
if (!adminUser) {
|
||||||
|
redirectToLogin(req, res)
|
||||||
return { props: {} }
|
return { props: {} }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,10 @@ 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 Calendar from '../../../../components/calendar'
|
import Calendar from '../../../../components/calendar'
|
||||||
import withSession, { isAdminSession } from '../../../../lib/session'
|
import withSession, {
|
||||||
|
isAdminSession,
|
||||||
|
redirectToLogin,
|
||||||
|
} from '../../../../lib/session'
|
||||||
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
|
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
|
||||||
import { BookingDocument } from '../../../../db/booking'
|
import { BookingDocument } from '../../../../db/booking'
|
||||||
import { getBookingStatus } from '../../../../helpers/booking'
|
import { getBookingStatus } from '../../../../helpers/booking'
|
||||||
@@ -14,10 +17,10 @@ export const getServerSideProps: GetServerSideProps = withSession(
|
|||||||
async (context) => {
|
async (context) => {
|
||||||
const { req, res } = context
|
const { req, res } = context
|
||||||
|
|
||||||
console.error('here')
|
|
||||||
const adminUser = isAdminSession(req, res)
|
const adminUser = isAdminSession(req, res)
|
||||||
|
|
||||||
if (!adminUser) {
|
if (!adminUser) {
|
||||||
|
redirectToLogin(req, res)
|
||||||
return { props: {} }
|
return { props: {} }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import withSession, { isAdminSession } from '../../../../../lib/session'
|
|||||||
|
|
||||||
export default withSession(async function billHandler(req, res) {
|
export default withSession(async function billHandler(req, res) {
|
||||||
if (!isAdminSession(req, res)) {
|
if (!isAdminSession(req, res)) {
|
||||||
|
res.status(403).send({ message: 'Not Authorized' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { BOOKING_STATUS } from '../../../../../db/enums'
|
|||||||
|
|
||||||
export default withSession(async function bookingHandler(req, res) {
|
export default withSession(async function bookingHandler(req, res) {
|
||||||
if (!isAdminSession(req, res)) {
|
if (!isAdminSession(req, res)) {
|
||||||
|
res.status(403).send({ message: 'Not Authorized' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,8 +43,10 @@ export default withSession(async function bookingHandler(req, res) {
|
|||||||
|
|
||||||
if (booking.status === BOOKING_STATUS.CONFIRMED) {
|
if (booking.status === BOOKING_STATUS.CONFIRMED) {
|
||||||
sendBookingConfirmed(booking)
|
sendBookingConfirmed(booking)
|
||||||
|
console.log(`Booking ${booking.uuid} confirm sent`)
|
||||||
} else if (booking.status === BOOKING_STATUS.REJECTED) {
|
} else if (booking.status === BOOKING_STATUS.REJECTED) {
|
||||||
sendBookingRejected(booking)
|
sendBookingRejected(booking)
|
||||||
|
console.log(`Booking ${booking.uuid} rejected sent`)
|
||||||
}
|
}
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|||||||
Reference in New Issue
Block a user