Running getServerSideProps auth checks from external function call

export const getServerSideProps: GetServerSideProps = async (ctx) => {
const session = await getServerAuthSession(ctx);

if (!session?.user.<AUTH_CHECK>) {
return {
redirect: {
destination: "/",
permanent: false,
},
props: {},
};
}
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const session = await getServerAuthSession(ctx);

if (!session?.user.<AUTH_CHECK>) {
return {
redirect: {
destination: "/",
permanent: false,
},
props: {},
};
}
Hello there, I have frequent auth checks in my t3 (pages router), Depending on certain permissions or data, the user will be redirected to other pages. This is the case for multiple pages. I just want to clarify if offloading the processing on an external function call (to make these function calls callable in other pages more organized and reusable, as well as to declutter the long and repetitive getServerSideProps) is recommended or are there certain security risks from offloading the function calls externally? I assume the auth checks would still be done server-side (i plan on putting them somewhere like ~/server/helpers/auth.ts)
6 Replies
reddie
reddieOPā€¢2mo ago
I am currently using this approach, i think it's a good way around it?
not sure how to properly type it though, the below works though
// ~/server/helpers/auth-helper.ts
import { type GetServerSidePropsResult } from "next";

export const returnToHome = {
redirect: {
destination: "/",
permanent: false,
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as GetServerSidePropsResult<Record<string, any>>;
// ~/server/helpers/auth-helper.ts
import { type GetServerSidePropsResult } from "next";

export const returnToHome = {
redirect: {
destination: "/",
permanent: false,
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as GetServerSidePropsResult<Record<string, any>>;
// ~/pages/page.tsx
if (authCheck) {
return returnToHome;
}
// ~/pages/page.tsx
if (authCheck) {
return returnToHome;
}
Neto
Netoā€¢2mo ago
you could handle it at the edge (middleware) if you are doing light checks using GetServerSideProps should work but it's slower for light checks
reddie
reddieOPā€¢2mo ago
Unfortunately, I am self-hosting this app via docker, so edge is not available
Neto
Netoā€¢2mo ago
oh buddy.... middleware runs on the edge runtime either way lmao if you aren't doing any server side loading you can just export it like that
reddie
reddieOPā€¢2mo ago
really? was not aware of that, mb mb.. i thought it was vercel-specific
Neto
Netoā€¢2mo ago
welcome to nextjs šŸ™‚
// src/lib/checker.ts
export function checker<T>(ctx: NextRequest): GetServerSidePropsResult<T> {
....
}

// src/pages/page.tsx

export const getServerSideProps = checker
// src/lib/checker.ts
export function checker<T>(ctx: NextRequest): GetServerSidePropsResult<T> {
....
}

// src/pages/page.tsx

export const getServerSideProps = checker
something like this?
Want results from more Discord servers?
Add your server