feat: 🔒️ Redirect unauthenticated user to event creation page
Tim Izzo tim@octree.ch
Mon, 19 Sep 2022 08:23:28 +0000
2 files changed,
26 insertions(+),
10 deletions(-)
M
frontend/pages/dashboard.tsx
→
frontend/pages/dashboard.tsx
@@ -9,6 +9,7 @@ import DashboardEmpty from '../containers/DashboardEmpty';
import Loading from '../containers/Loading'; import Fab from '../containers/Fab'; import pageUtils from '../lib/pageUtils'; +import {getSession} from 'next-auth/react'; const Dashboard = () => { const {t} = useTranslation();@@ -79,6 +80,17 @@ };
const sortDesc = ({date: dateA}, {date: dateB}) => dateB.localeCompare(dateA); -export const getServerSideProps = pageUtils.getServerSideProps(); +export const getServerSideProps = async (context: any) => { + const session = await getSession(context); + + if (!session) + return { + redirect: { + destination: '/', + permanent: false, + }, + }; + else return pageUtils.getServerSideProps()(context); +}; export default Dashboard;
M
frontend/pages/profile.tsx
→
frontend/pages/profile.tsx
@@ -3,22 +3,16 @@ import {useTranslation} from 'react-i18next';
import Loading from '../containers/Loading'; import Profile from '../containers/Profile'; import Layout from '../layouts/Centered'; -import {useSession, signOut} from 'next-auth/react'; +import {useSession, signOut, getSession} from 'next-auth/react'; import pageUtils from '../lib/pageUtils'; import useProfile from '../hooks/useProfile'; -import {useEffect} from 'react'; const ProfilePage = () => { const router = useRouter(); const {t} = useTranslation(); const session = useSession(); - const isAuthenticated = session.status === 'authenticated'; const {profile} = useProfile(); - useEffect(() => { - if (!isAuthenticated) router.push('/'); - }, [isAuthenticated]); - const menuActions = [ { label: t('menu.new_event'),@@ -33,7 +27,6 @@ },
]; if (session.status === 'loading') return <Loading />; - else if (!isAuthenticated) return null; return ( <Layout menuTitle={t('profile.title')} menuActions={menuActions} goBack>@@ -42,6 +35,17 @@ </Layout>
); }; -export const getServerSideProps = pageUtils.getServerSideProps(); +export const getServerSideProps = async (context: any) => { + const session = await getSession(context); + + if (!session) + return { + redirect: { + destination: '/', + permanent: false, + }, + }; + else return pageUtils.getServerSideProps()(context); +}; export default ProfilePage;