From 2008ad263ce1c1f7d32409a069545f300ffa3334 Mon Sep 17 00:00:00 2001 From: mihaic195 Date: Fri, 2 Jul 2021 13:28:33 +0300 Subject: [PATCH] fix: ssr session on teams page --- pages/_app.tsx | 3 ++- pages/settings/teams.tsx | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pages/_app.tsx b/pages/_app.tsx index e2964ddee3..8b0b900996 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -1,8 +1,9 @@ import '../styles/globals.css'; import {createTelemetryClient, TelemetryProvider} from '../lib/telemetry'; import { Provider } from 'next-auth/client'; +import type { AppProps } from "next/app" -function MyApp({ Component, pageProps }) { +function MyApp({ Component, pageProps }: AppProps) { return ( diff --git a/pages/settings/teams.tsx b/pages/settings/teams.tsx index 36b57984ff..21df9f4d10 100644 --- a/pages/settings/teams.tsx +++ b/pages/settings/teams.tsx @@ -1,8 +1,10 @@ +import { GetServerSideProps } from "next" import Head from 'next/head'; import Shell from '../../components/Shell'; import SettingsShell from '../../components/Settings'; import { useEffect, useState } from 'react'; -import { useSession } from 'next-auth/client'; +import type { Session } from "next-auth" +import { useSession, getSession } from 'next-auth/client'; import { UsersIcon, } from "@heroicons/react/outline"; @@ -165,3 +167,15 @@ export default function Teams() { ); } + +// Export the `session` prop to use sessions with Server Side Rendering +export const getServerSideProps: GetServerSideProps<{session: Session | null}> = async (context) => { + const session = await getSession(context); + if (!session) { + return { redirect: { permanent: false, destination: '/auth/login' } }; + } + + return { + props: { session } + } +}