Nextjs-auth0: Fetch data when using withPageAuthRequired()

Created on 14 Apr 2021  路  4Comments  路  Source: auth0/nextjs-auth0

Hello, I don't quite understand the documentation and I have a question.
I want to combine the withPageAuthRequired() function provided from the SDK with my own so I can fetch some data with Axios with SSR.

How do I do that?
Currently I am doing this:

const Create = () => {
 // code
};

export default Create;
export const getServerSideProps = withPageAuthRequired();
question

All 4 comments

Hi there, after following along with the Auth0 tutorials on YouTube I came to a similar question. I understand that this SDK has undergone a major update since the tutorials were made but it leaves things a little confused (at least for me).

Regarding the above question, I was able to piece things together from looking at the docs and the migration guide to come up with the following...

export const getServerSideProps = withPageAuthRequired({
    getServerSideProps: async ({ req, res }) => {
        const { user } = getSession(req, res);
        const postsForUser = // code that fetches users posts from DB // 

        return { props: { posts } };
    },
});

export default function Posts({ user, posts }) {
....

This totally works but it left me feeling I had got something wrong by using withPageAuthRequired and getSession together. Is this a viable way to do things when you want the functionality withPageAuthRequired provides (e.g redirect) but you also need to make use of the user in getServerSideProps?

Thanks for your time and effort in making this SDK 馃憤

Hey, thank you for responding. That explains a lot to me.

Thanks @pasenidis

@flatsteve

This totally works but it left me feeling I had got something wrong by using withPageAuthRequired and getSession together. Is this a viable way to do things when you want the functionality withPageAuthRequired provides (e.g redirect) but you also need to make use of the user in getServerSideProps?

Yep - this is the way :) getSession is just looking up a key on a map, so it's v cheap

@adamauth Perfecto, thank you for your time and explanation 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jkjustjoshing picture jkjustjoshing  路  4Comments

flapjackfritz picture flapjackfritz  路  6Comments

synoptase picture synoptase  路  6Comments

aorsten picture aorsten  路  6Comments

RyanPayso13 picture RyanPayso13  路  4Comments