Skip to content

Commit

Permalink
fix TypeError: Cannot read properties of undefined (reading 'session') (
Browse files Browse the repository at this point in the history
#2946)

see nextauthjs/next-auth#4845
Not sure it fixed or not, since I'm not able to reproduce the issue
locally. Also this might effect the performance, just a bit I think
  • Loading branch information
notmd committed Apr 28, 2023
1 parent 67499fd commit 268ea95
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions website/src/pages/_app.tsx
Expand Up @@ -3,8 +3,10 @@ import "focus-visible";

import { boolean } from "boolean";
import type { AppContext, AppProps } from "next/app";
import App from "next/app";
import Head from "next/head";
import { SessionProvider } from "next-auth/react";
import { Session } from "next-auth";
import { getSession, SessionProvider } from "next-auth/react";
import { appWithTranslation, useTranslation } from "next-i18next";
import React, { useEffect } from "react";
import { FlagsProvider } from "react-feature-flags";
Expand Down Expand Up @@ -56,10 +58,15 @@ function MyApp({ Component, pageProps: { session, ...pageProps }, cookie, env }:
);
}

type AppInitialProps = { env: BrowserEnv; cookie: string };
type AppInitialProps = { env: BrowserEnv; cookie: string; session: Session };

MyApp.getInitialProps = async (context: AppContext): Promise<AppInitialProps> => {
const appProps = await App.getInitialProps(context);
const session = await getSession();

MyApp.getInitialProps = ({ ctx: { req } }: AppContext): AppInitialProps => {
return {
...appProps,
session,
env: {
ENABLE_CHAT: boolean(process.env.ENABLE_CHAT),
ENABLE_EMAIL_SIGNIN: boolean(process.env.ENABLE_EMAIL_SIGNIN),
Expand All @@ -68,7 +75,7 @@ MyApp.getInitialProps = ({ ctx: { req } }: AppContext): AppInitialProps => {
CURRENT_ANNOUNCEMENT: process.env.CURRENT_ANNOUNCEMENT,
NODE_ENV: process.env.NODE_ENV,
},
cookie: req?.headers.cookie || "",
cookie: context.ctx.req?.headers.cookie || "",
};
};

Expand Down

0 comments on commit 268ea95

Please sign in to comment.