From 37c3c6f7f402151caf8c428d11a513d98938306e Mon Sep 17 00:00:00 2001 From: Joao Ramos Date: Mon, 11 Mar 2024 17:00:38 +0000 Subject: [PATCH] Updated redirect implementation --- src/pages/action.tsx | 8 ++++---- src/pages/exam.tsx | 20 ++++++++------------ src/pages/exercises.tsx | 20 ++++++++------------ src/pages/generation.tsx | 20 ++++++++------------ src/pages/index.tsx | 20 ++++++++++++-------- src/pages/list/users.tsx | 11 ++++------- src/pages/login.tsx | 13 +++++-------- src/pages/payment-record.tsx | 26 +++++++++++--------------- src/pages/payment.tsx | 15 +++++++-------- src/pages/profile.tsx | 20 ++++++++------------ src/pages/record.tsx | 20 ++++++++------------ src/pages/settings.tsx | 20 ++++++++------------ src/pages/stats.tsx | 20 ++++++++------------ src/pages/tickets.tsx | 28 ++++++++++++---------------- 14 files changed, 111 insertions(+), 150 deletions(-) diff --git a/src/pages/action.tsx b/src/pages/action.tsx index bee4552a..c6cf8cbb 100644 --- a/src/pages/action.tsx +++ b/src/pages/action.tsx @@ -23,11 +23,11 @@ export function getServerSideProps({ res: any; }) { if (!query || !query.oobCode || !query.mode) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: {}, + redirect: { + destination: "/login", + permanent: false, + } }; } diff --git a/src/pages/exam.tsx b/src/pages/exam.tsx index 4d686e74..3f683604 100644 --- a/src/pages/exam.tsx +++ b/src/pages/exam.tsx @@ -10,24 +10,20 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/login", + permanent: false, + } }; } if (shouldRedirectHome(user)) { - res.setHeader("location", "/"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/", + permanent: false, + } }; } diff --git a/src/pages/exercises.tsx b/src/pages/exercises.tsx index f3bcfa84..9ab3b244 100644 --- a/src/pages/exercises.tsx +++ b/src/pages/exercises.tsx @@ -10,24 +10,20 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/login", + permanent: false, + } }; } if (shouldRedirectHome(user)) { - res.setHeader("location", "/"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/", + permanent: false, + } }; } diff --git a/src/pages/generation.tsx b/src/pages/generation.tsx index 58a5d641..b52f6ef6 100644 --- a/src/pages/generation.tsx +++ b/src/pages/generation.tsx @@ -26,24 +26,20 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/login", + permanent: false, + } }; } if (shouldRedirectHome(user) || user.type !== "developer") { - res.setHeader("location", "/"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/", + permanent: false, + } }; } diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 02ff0a8e..59401b80 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -35,6 +35,7 @@ import Select from "react-select"; import {USER_TYPE_LABELS} from "@/resources/user"; export const getServerSideProps = withIronSessionSsr(({req, res}) => { + debugger; const user = req.session.user; const envVariables: {[key: string]: string} = {}; @@ -45,14 +46,11 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { }); if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - envVariables, - }, + redirect: { + destination: "/login", + permanent: false, + } }; } @@ -61,7 +59,13 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { }; }, sessionOptions); -export default function Home({envVariables}: {envVariables: {[key: string]: string}}) { +interface Props { + user: any; + envVariables: {[key: string]: string}; +} +export default function Home(props: Props) { + const { envVariables} = props; + debugger; const [showDiagnostics, setShowDiagnostics] = useState(false); const [showDemographicInput, setShowDemographicInput] = useState(false); const [selectedScreen, setSelectedScreen] = useState("admin"); diff --git a/src/pages/list/users.tsx b/src/pages/list/users.tsx index 356ef810..3ec4c585 100644 --- a/src/pages/list/users.tsx +++ b/src/pages/list/users.tsx @@ -22,14 +22,11 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { }); if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - envVariables, - }, + redirect: { + destination: "/login", + permanent: false, + } }; } diff --git a/src/pages/login.tsx b/src/pages/login.tsx index 2a6990df..73582e1a 100644 --- a/src/pages/login.tsx +++ b/src/pages/login.tsx @@ -31,15 +31,12 @@ export const getServerSideProps = withIronSessionSsr(({ req, res }) => { }); if (user && user.isVerified) { - res.setHeader("location", "/"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - envVariables, - }, - }; + redirect: { + destination: "/", + permanent: false, + } + }; } return { diff --git a/src/pages/payment-record.tsx b/src/pages/payment-record.tsx index 941f9fab..e9368abf 100644 --- a/src/pages/payment-record.tsx +++ b/src/pages/payment-record.tsx @@ -42,25 +42,21 @@ export const getServerSideProps = withIronSessionSsr(({ req, res }) => { const user = req.session.user; if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, - }; + redirect: { + destination: "/login", + permanent: false, + } + }; } if (shouldRedirectHome(user) || !["admin", "developer"].includes(user.type)) { - res.setHeader("location", "/"); - res.statusCode = 302; - res.end(); - return { - props: { - user: null, - }, - }; + return { + redirect: { + destination: "/", + permanent: false, + } + }; } return { diff --git a/src/pages/payment.tsx b/src/pages/payment.tsx index bb68b16c..0fcc3a56 100644 --- a/src/pages/payment.tsx +++ b/src/pages/payment.tsx @@ -7,6 +7,7 @@ import PaymentDue from "./(status)/PaymentDue"; import { useRouter } from "next/router"; export const getServerSideProps = withIronSessionSsr(({ req, res }) => { + debugger; const user = req.session.user; const envVariables: { [key: string]: string } = {}; @@ -17,15 +18,12 @@ export const getServerSideProps = withIronSessionSsr(({ req, res }) => { }); if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - envVariables, - }, - }; + redirect: { + destination: "/login", + permanent: false, + } + }; } return { @@ -38,6 +36,7 @@ export default function Home({ }: { envVariables: { [key: string]: string }; }) { + debugger; const { user } = useUser({ redirectTo: "/login" }); const router = useRouter(); diff --git a/src/pages/profile.tsx b/src/pages/profile.tsx index e94d4026..68e652d2 100644 --- a/src/pages/profile.tsx +++ b/src/pages/profile.tsx @@ -37,24 +37,20 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/login", + permanent: false, + } }; } if (shouldRedirectHome(user)) { - res.setHeader("location", "/"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/", + permanent: false, + } }; } diff --git a/src/pages/record.tsx b/src/pages/record.tsx index d5c5d97f..c12888c6 100644 --- a/src/pages/record.tsx +++ b/src/pages/record.tsx @@ -30,24 +30,20 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/login", + permanent: false, + } }; } if (shouldRedirectHome(user)) { - res.setHeader("location", "/"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/", + permanent: false, + } }; } diff --git a/src/pages/settings.tsx b/src/pages/settings.tsx index eb1192b5..85cb5073 100644 --- a/src/pages/settings.tsx +++ b/src/pages/settings.tsx @@ -18,24 +18,20 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/login", + permanent: false, + } }; } if (shouldRedirectHome(user) || user.type !== "developer") { - res.setHeader("location", "/"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/", + permanent: false, + } }; } diff --git a/src/pages/stats.tsx b/src/pages/stats.tsx index ea8f9886..fdb36314 100644 --- a/src/pages/stats.tsx +++ b/src/pages/stats.tsx @@ -35,24 +35,20 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/login", + permanent: false, + } }; } if (shouldRedirectHome(user)) { - res.setHeader("location", "/"); - res.statusCode = 302; - res.end(); return { - props: { - user: null, - }, + redirect: { + destination: "/", + permanent: false, + } }; } diff --git a/src/pages/tickets.tsx b/src/pages/tickets.tsx index 21c6fd92..128058e4 100644 --- a/src/pages/tickets.tsx +++ b/src/pages/tickets.tsx @@ -35,28 +35,24 @@ export const getServerSideProps = withIronSessionSsr(({ req, res }) => { const user = req.session.user; if (!user || !user.isVerified) { - res.setHeader("location", "/login"); - res.statusCode = 302; - res.end(); - return { - props: { - user: null, - }, - }; + return { + redirect: { + destination: "/login", + permanent: false, + } + }; } if ( shouldRedirectHome(user) || ["admin", "developer", "agent"].includes(user.type) ) { - res.setHeader("location", "/"); - res.statusCode = 302; - res.end(); - return { - props: { - user: null, - }, - }; + return { + redirect: { + destination: "/", + permanent: false, + } + }; } return {