Check auth next.js in layout hoc or _app.js with getServerSideProps?

I usually checked accessToken in localStorage on the client, if it is there, then send a request from the client to reset tokens, but I want to give this task to the server, and therefore I put accessToken and refreshToken in cookies and try to send a request to the server with cookies.

How can I send a request to the server only 1 time when logging in to the application?

I want to check cookies on the server for the presence of refreshToken, and if they are there, then update the token and transfer the user’s data in the response (in the body)

I want to do this without an extra request from the client, that is, without using useEffect, but nothing comes out.

AppLayout.js

import Head from "next/head";

const AppLayout = ({ children, data }) => {
    console.log(data)// undefined
    return (
        <div className="layout">
            <Head>
                <meta name="viewport" content="width=device-width, initial-scale=1" />
                <link rel="icon" href="/favicon.ico" />
            </Head>
            <header />
            <main>{children}</main>
            <footer />
        </div>
    );
};

export default AppLayout;

// why getServerSideProps dont work ?
export function getServerSideProps({ ctx }) {
    // console.log(ctx?.req?.headers?.cookie);
  
    const data = 'hello';
    console.log('getServerSideProps')// dont work
    return {
      data
    }
};

_app.js

import '@/styles/index.scss'
import AppLayout from '@/components/AppLayout/AppLayout'
import Link from 'next/link'

export default function App({ Component, pageProps }) {
  return (
    <AppLayout {...pageProps}>
      <Link href="/">home</Link>
      <Link href="gg">gg</Link>
      <Component {...pageProps} />
    </AppLayout>
  )
}