all repos — caroster @ 78ecb0cb7a059f82b469e466269e08d9f1244514

[Octree] Group carpool to your event https://caroster.io

🔒️ Fix authentication and redirections
Tim Izzo tim@octree.ch
Mon, 24 Oct 2022 15:02:27 +0000
commit

78ecb0cb7a059f82b469e466269e08d9f1244514

parent

5b6803da163ba94ef50ba2bb8d4076a8c9d2492f

M frontend/containers/GenericToolbar/index.tsxfrontend/containers/GenericToolbar/index.tsx

@@ -24,7 +24,7 @@ const router = useRouter();

const theme = useTheme(); const [anchorEl, setAnchorEl] = useState(null); - const {profile} = useProfile(); + const {profile, connected} = useProfile(); useEffect(() => { window.scrollTo(0, 0);

@@ -72,7 +72,7 @@ id="MenuMoreInfo"

onClick={e => setAnchorEl(e.currentTarget)} size="large" > - {profile ? ( + {connected && profile ? ( <Avatar sx={{ width: theme.spacing(3),
M frontend/layouts/ConfirmLayout.tsxfrontend/layouts/ConfirmLayout.tsx

@@ -1,5 +1,5 @@

import Card from '@mui/material/Card'; -import { styled } from '@mui/material/styles'; +import {styled} from '@mui/material/styles'; import CardMedia from '@mui/material/CardMedia'; import CardContent from '@mui/material/CardContent'; import Layout from './Centered';

@@ -8,32 +8,24 @@

const PREFIX = 'CommonConfirm'; const classes = { - wrapper: `${PREFIX}-wrapper` + wrapper: `${PREFIX}-wrapper`, }; -const StyledLayout = styled(Layout)(( - { - theme - } -) => ({ +const StyledLayout = styled(Layout)(({theme}) => ({ [`& .${classes.wrapper}`]: { - padding: theme.spacing(0, 8 ), + padding: theme.spacing(0, 8), '&:last-child': { paddingBottom: theme.spacing(12), }, - } + }, })); const CommonConfirm = ({children}) => { - - return ( <StyledLayout displayMenu={false}> <Card> <CardMedia component={Logo} /> - <CardContent className={classes.wrapper}> - {children} - </CardContent> + <CardContent className={classes.wrapper}>{children}</CardContent> </Card> </StyledLayout> );
M frontend/lib/apolloClient.tsfrontend/lib/apolloClient.ts

@@ -4,7 +4,7 @@ import {setContext} from '@apollo/client/link/context';

import {onError} from '@apollo/client/link/error'; import merge from 'deepmerge'; import isEqual from 'lodash/isEqual'; -import {useSession} from 'next-auth/react'; +import {signOut, useSession} from 'next-auth/react'; import {Session} from 'next-auth'; export const APOLLO_STATE_PROP_NAME = '__APOLLO_STATE__';

@@ -22,11 +22,14 @@ },

}; }); -const errorLink = onError(({graphQLErrors = [], operation}) => { - console.error({graphQLErrors, operation}); - const message = graphQLErrors?.[0]?.message; +const errorLink = onError(({operation, networkError}) => { + console.error({networkError, operation}); + const responseStatus = networkError?.response?.status; - if (message === 'Forbidden') window.location.href = '/auth/login'; + if (responseStatus === 401) + signOut({ + callbackUrl: '/auth/login', + }); }); const httpLink = (uri: string) =>
M frontend/pages/api/nauth/[...nextauth].jsfrontend/pages/api/nauth/[...nextauth].js

@@ -83,7 +83,8 @@ async redirect({url, baseUrl}) {

// Allows relative callback URLs if (url.startsWith('/')) return `${baseUrl}${url}`; // Allows callback URLs on the same host - else if (new URL(url).host === new URL(baseUrl).host) return url; + else if (new URL(url).host === new URL(baseUrl).host) + return `${url}/dashboard`; return baseUrl; }, },
M frontend/pages/auth/confirm/google.tsxfrontend/pages/auth/confirm/google.tsx

@@ -22,7 +22,7 @@ const [updateMe] = useUpdateMeMutation();

const getRedirectUrl = useRedirectUrlStore(s => s.getRedirectUrl); const onSubmit = async () => { await updateMe({variables: {userUpdate: {newsletterConsent}}}); - const callbackUrl = getRedirectUrl() || '/'; + const callbackUrl = getRedirectUrl() || '/dashboard'; router.push(callbackUrl); };
M frontend/pages/dashboard.tsxfrontend/pages/dashboard.tsx

@@ -106,7 +106,21 @@ destination: '/',

permanent: false, }, }; - else return pageUtils.getServerSideProps()(context); + + const {provider, userCreatedAt} = session?.token || {}; + const isFirstLogin = userCreatedAt + ? moment().subtract({seconds: 3}).isBefore(userCreatedAt) + : false; + + if (provider === 'google' && isFirstLogin) + return { + redirect: { + destination: '/auth/confirm/google', + permanent: false, + }, + }; + + return pageUtils.getServerSideProps()(context); }; export default Dashboard;
M frontend/pages/index.tsxfrontend/pages/index.tsx

@@ -1,14 +1,11 @@

import {useRouter} from 'next/router'; import {useTranslation} from 'react-i18next'; -import moment from 'moment'; import Layout from '../layouts/Centered'; import CreateEvent from '../containers/CreateEvent'; import LanguagesIcon from '../containers/Languages/Icon'; import Logo from '../components/Logo'; -import {getSession, useSession} from 'next-auth/react'; +import {useSession} from 'next-auth/react'; import pageUtils from '../lib/pageUtils'; -import {useEffect} from 'react'; -import useRedirectUrlStore from '../stores/useRedirectUrl'; import theme from '../theme'; import Paper from '@mui/material/Paper';

@@ -22,12 +19,6 @@ const router = useRouter();

const session = useSession(); const isAuthenticated = session.status === 'authenticated'; const isReady = session.status !== 'loading'; - const getRedirectUrl = useRedirectUrlStore(s => s.getRedirectUrl); - - useEffect(() => { - const redirectUrl = getRedirectUrl(); - if (redirectUrl) router.push(redirectUrl); - }, []); const noUserMenuActions = [ {

@@ -76,21 +67,6 @@ </Layout>

); }; -export const getServerSideProps = async (context: any) => { - const session = await getSession(context); - const {provider, userCreatedAt} = session?.token || {}; - const isFirstLogin = userCreatedAt - ? moment().subtract({seconds: 3}).isBefore(userCreatedAt) - : false; - if (provider === 'google' && isFirstLogin) - return { - redirect: { - destination: '/auth/confirm/google', - permanent: false, - }, - }; - - return pageUtils.getServerSideProps()(context); -}; +export const getServerSideProps = pageUtils.getServerSideProps(); export default Home;