Redirect If Not Authed Pattern
Hi, is there any reason that it's not good to use the following pattern to gate pages?
export async function getServerSideProps(ctx: GetServerSidePropsContext) {
// Redirect to Landing Page if Not Logged in
return redirectIfNotAuthed({
ctx,
redirectUrl: "/",
});
}
where we have
export async function redirectIfNotAuthed({
ctx,
redirectUrl,
callback,
}: {
ctx: GetServerSidePropsContext;
redirectUrl: string;
callback?: () => any;
}) {
const session = await getSession(ctx);
if (!session) {
if (callback) {
callback();
}
return {
redirect: {
permanent: false,
destination: redirectUrl,
},
props: {},
};
} else {
if (callback) {
callback();
}
return { props: {} };
}
}
It works fine but was just wondering if there is some reason this scales poorly or somethin9 Replies
the idea is fine
that is something that iron session allow you to do
npm
iron-session
Node.js stateless session utility using signed and encrypted cookies to store data. Works with Next.js, Express, NestJs, Fastify, and any Node.js HTTP framework.. Latest version: 6.3.1, last published: a month ago. Start using iron-session in your project by running
npm i iron-session
. There are 20 other projects in the npm registry using iron...HoC is better to identify i would argue
HoC?
high order component
a function that returns a funcion
on that instance is called high order function*
oh gotcha
okay cool so should be fine to do it this way?
either way should be fine
if its readable for the person using it, then is fine
I have done it like this:
ah, this wrapper is in the create-t3-app starter, didnt realize it was supposed to be used like that