Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
Pass data from _app's getServerSideProps to pages' getServerSideProps

Is there a way to pass data from _app's getServerSideProps to page's getServerSideProps? My issue is as follows: I have a custom _app with getServerSideProps. Here I check if the user is authenticated and if so, load the user from DB. Both (isAuthenticated and user) are then passed to the _app's props.

Then I have actual pages. Some of these pages are public and some need authorization, so they have their own getServerSideProps. I would like to access the user data loaded in _app's getServerSideProps from within the specific pages' getServerSideProps in order to decide if a redirect should take place. Otherwise, I'd have to load the user info twice - first on the _app level and then on the page level.

Is there a way to load the data only once (on the _app level) and reuse it on the page level?

// pages/_app.jsx

const App = ...

App.getServerSideProps = async(context) => {
    let isAuthenticated = false;
    let user = {};

    if (context?.req) {
        // get token from Cookies, validate token & load user
    }

    return {
        props: { isAuthenticated, user },
    }
}
// pages/profile.jsx

const ProfilePage = ...

ProfilePage.getServerSideProps = async(context) => {
    // get token, load user - in this step, I'd like to reuse the previously loaded data

    if (!isAuthenticated || !user.HAS_SOME_SPECIFIC_PERMISSION) {
        return {
            redirect: {
                destination: '/login',
            },
        }
    }

    return { props: {} }
}

Edit

Before I finished writing this question, I already decided to get rid of my own auth and use NextAuth.js. However, while I no longer need an answer to this question, it still seems like an interesting "mental exercise" question, so I'm leaving it here... in case anybody needs to pass data from top level getServerSideProps to page level getServerSideProps.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda