import Cookies from 'cookies'; import { GetServerSidePropsContext, GetServerSidePropsResult } from 'next'; import { getUserInfo } from './api/remote'; import { COOKIE_KEYS } from './constants'; import { CurrentUserDto } from './types/user-response.interface'; const inProd = process.env.NODE_ENV === 'production'; function withUser( handler: ( ctx: GetServerSidePropsContext, user?: CurrentUserDto ) => Promise> ) { return async (ctx: GetServerSidePropsContext) => { const cookies = new Cookies(ctx.req, ctx.res, { secure: inProd, keys: COOKIE_KEYS, }); const accessToken = cookies.get('authorization', { signed: true }); let user: CurrentUserDto | undefined; if (accessToken) { try { user = await getUserInfo(accessToken); } catch (e) {} } return handler(ctx, user); }; } export default withUser;