SupabaseS
Supabase4y ago
Shin

Check for user SSRNextJs, send props either way

I have a page which shows content for logged-in users if the user is indeed logged in. If they are not logged in then that content doesn't show. So I would like to get the user (if they exist) in getServerSideProps. However I only seem to be able to load the page for logged in users or redirect if they are not logged in as per https://github.com/supabase/auth-helpers/blob/main/packages/nextjs/README.md (Server-side rendering (SSR) - withPageAuth).

I did find this https://github.com/supabase/auth-helpers/issues/109 where someone suggests to use authRequired: false, but whilst that does load the page without the user being logged in, if the user is not logged in it does not pass through the props. I am passing through critical data in getServerSideProps so that's no good.

I created this test page:

...
export const getServerSideProps = withPageAuth({
    
    authRequired: false,
    
    async getServerSideProps(ctx) {

        const { user } = await getUser(ctx);
        const message = "Some message"

        if (user) {
            return {
                props: {
                    message: "User authenticated"
                }
            };
        }
        
        else {
            return {
                props: {
                    message: "User not authenticated"
                }
            };
        }
    }

});


If the user is logged in, message makes it through to props. If the user is not logged in message does not get through to props, all I get is a null user object.

Is there a way for me to check for the user SSR, if the user exists do some stuff, and then pass the rest of the data I am processing SSR through props to the app?
GitHub
A collection of framework specific Auth utilities for working with Supabase. - auth-helpers/README.md at main · supabase/auth-helpers
GitHub
Hey, Just wondering if you think there is any value in a withoutPageAuth helper function which redirects the user to a different page IF they are signed in. This would essentially be the opposite o...
Was this page helpful?