Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix auth, layout and private route #71

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import React from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ToastContainer } from 'react-toastify';
import AuthContextProvider from './src/utils/Auth';
import { Layout } from './src/components';

const queryClient = new QueryClient();

// Get fonts on initial load of the app
export const onRenderBody = ({ setHeadComponents }) => {
// @import url('fonts/Roslindale/font.css');
setHeadComponents([
Expand Down Expand Up @@ -61,6 +63,11 @@ export const onRenderBody = ({ setHeadComponents }) => {
]);
};

export const wrapPageElement = ({ element, props }) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<Layout {...props}>{element}</Layout>
);

export const wrapRootElement = ({ element }) => (
<QueryClientProvider client={queryClient}>
<AuthContextProvider>{element}</AuthContextProvider>
Expand Down
6 changes: 6 additions & 0 deletions gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ToastContainer } from 'react-toastify';
import AuthContextProvider from './src/utils/Auth';
import { Layout } from './src/components';

const queryClient = new QueryClient();

Expand Down Expand Up @@ -61,6 +62,11 @@ export const onRenderBody = ({ setHeadComponents }) => {
]);
};

export const wrapPageElement = ({ element, props }) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<Layout {...props}>{element}</Layout>
);

export const wrapRootElement = ({ element }) => (
<QueryClientProvider client={queryClient}>
<AuthContextProvider>{element}</AuthContextProvider>
Expand Down
8 changes: 6 additions & 2 deletions src/components/marginals/Navbar/DesktopNav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function DesktopNav() {
const menuContext = useContext(MenuContext);
const { toggleMenuOpen, menuOpen } = menuContext;
const authContext = useContext(AuthContext);
const { authenticated } = authContext;
const { authenticated, login, logout } = authContext;

return (
<NavSection>
Expand Down Expand Up @@ -64,7 +64,11 @@ function DesktopNav() {
</ul>
</NavCenter>
<NavRight>
<Button variant='outline' text={authenticated ? 'logout' : 'Register'} />
<Button
variant='outline'
text={authenticated ? 'logout' : 'Register'}
onClick={authenticated ? logout : login}
/>
</NavRight>
</NavWrapper>
</Container>
Expand Down
17 changes: 14 additions & 3 deletions src/components/marginals/Navbar/MobileNav.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useContext } from 'react';

// Libraries
import { Link } from 'gatsby';
import { Link, navigate } from 'gatsby';

// Components
import { MenuContext } from './MenuContext';
Expand Down Expand Up @@ -29,6 +29,13 @@ const handleScroll = (id) => {
const scroll = newScrollObject();
const anchor = document.getElementById(id);
scroll.animateScroll(anchor);
} else {
navigate('/');
setTimeout(() => {
const scroll = newScrollObject();
const anchor = document.getElementById(id);
scroll.animateScroll(anchor);
}, 1000);
}
}
};
Expand All @@ -37,7 +44,7 @@ function MobileNav() {
const menuContext = useContext(MenuContext);
const { toggleMenuOpen } = menuContext;
const authContext = useContext(AuthContext);
const { authenticated } = authContext;
const { authenticated, login, logout } = authContext;

const onMenuClick = (id) => {
handleScroll(id);
Expand All @@ -64,7 +71,11 @@ function MobileNav() {
</li>
))}
</ul>
<Button variant='outline' text={authenticated ? 'logout' : 'Register'} />
<Button
variant='outline'
text={authenticated ? 'logout' : 'Register'}
onClick={authenticated ? logout : login}
/>
</div>
</StyledMobileNav>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export { default as ModalBox } from './ModalBox';
export { default as About } from './About';
export { default as Layout } from './Layout';
export { default as Input } from './partials/Input';
export { default as PrivateRoute } from './partials/PrivateRoute';
23 changes: 23 additions & 0 deletions src/components/shared/partials/PrivateRoute/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { navigate } from 'gatsby';
import React, { useContext } from 'react';
import { toast } from 'react-toastify';
import { AuthContext } from '../../../../utils/Auth';

const PrivateRoute = ({ children }) => {
const { authenticated, loading } = useContext(AuthContext);

if (loading) {
// TODO: Add loading spinner
return <div>Loading...</div>;
}

if (!authenticated) {
toast.error('You are not authenticated. Please login to access this page.');
navigate('/');
return null;
}

return children;
};

export default PrivateRoute;
6 changes: 2 additions & 4 deletions src/pages/404.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Helmet } from 'react-helmet';
import { Layout, PageNotFoundCard } from '../components';
import { PageNotFoundCard } from '../components';

export default function Error404() {
return (
Expand All @@ -10,9 +10,7 @@ export default function Error404() {
<title>Page not found</title>
<meta name='description' content='This is 404 page' />
</Helmet>
<Layout>
<PageNotFoundCard />
</Layout>
<PageNotFoundCard />
</>
);
}
3 changes: 1 addition & 2 deletions src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable react/jsx-props-no-spreading */
import React from 'react';
import { Helmet } from 'react-helmet';
import { Layout } from '../components';

export default function Home() {
return (
Expand All @@ -11,7 +10,7 @@ export default function Home() {
<title>Innovision | Home</title>
<meta name='description' content='This is home page' />
</Helmet>
<Layout>Hello Innovision 2023</Layout>
Hello Innovision 2023
</>
);
}
8 changes: 4 additions & 4 deletions src/pages/payment.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from 'react';
import { Heading1, Layout } from '../components';
import { Heading1, PrivateRoute } from '../components';

const PaymentPage = () => {
// eslint-disable-next-line no-console
console.log('Payment page');
console.log('PaymentPage');

return (
<Layout>
<PrivateRoute>
<Heading1>Payment</Heading1>
{/* Payment form here */}
TODO - Payment form
</Layout>
</PrivateRoute>
);
};

Expand Down
6 changes: 1 addition & 5 deletions src/pages/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import React from 'react';
import { Helmet } from 'react-helmet';

import { Layout } from '../components';
import FAQSection from '../components/FAQSection/FAQSection';

const Playground = () => (
Expand All @@ -12,10 +11,7 @@ const Playground = () => (
<title>Playground</title>
<meta name='description' content='This is playground' />
</Helmet>
<User />
<Layout>
<FAQSection />
</Layout>
<FAQSection />
</>
);

Expand Down
16 changes: 7 additions & 9 deletions src/pages/pricing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React from 'react';
import styled from 'styled-components';
import tw from 'twin.macro';
import { Container, Heading3, Body2, Layout } from '../components';
import { Container, Heading3, Body2 } from '../components';

const Title = styled(Heading3)`
${tw`my-4`}
Expand All @@ -17,14 +17,12 @@ const RefundContainer = styled.div`
`;

const privacy = () => (
<Layout>
<Container>
<RefundContainer>
<Title>Pricing</Title>
<Desc>The Registration fee for Innovision 2k23 is ₹700. Free for NITR Students</Desc>
</RefundContainer>
</Container>
</Layout>
<Container>
<RefundContainer>
<Title>Pricing</Title>
<Desc>The Registration fee for Innovision 2k23 is ₹700. Free for NITR Students</Desc>
</RefundContainer>
</Container>
);

export default privacy;
6 changes: 3 additions & 3 deletions src/pages/register.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from 'react';
import { Heading1, Layout } from '../components';
import { Heading1 } from '../components';

const RegistrationPage = () => {
// eslint-disable-next-line no-console
console.log('Registration page');

return (
<Layout>
<>
<Heading1>Registration</Heading1>
{/* Registration form here */}
TODO - Registration form
</Layout>
</>
);
};

Expand Down
6 changes: 2 additions & 4 deletions src/pages/txn-successful.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Helmet } from 'react-helmet';
import { Layout, SuccessCard } from '../components';
import { SuccessCard } from '../components';

export default function Successful() {
return (
Expand All @@ -10,9 +10,7 @@ export default function Successful() {
<title>Innovision | Success</title>
<meta name='description' content='This is success page' />
</Helmet>
<Layout>
<SuccessCard />
</Layout>
<SuccessCard />
</>
);
}
3 changes: 0 additions & 3 deletions src/utils/Auth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import React, { createContext, useEffect, useMemo, useState } from 'react';

import { GoogleAuthProvider, onAuthStateChanged, signInWithPopup } from 'firebase/auth';
import { navigate } from 'gatsby';
import { toast } from 'react-toastify';
import { auth } from '../config/firebase';
import Api from './Api';
Expand Down Expand Up @@ -48,7 +47,6 @@ const AuthContextProvider = ({ children }) => {
setAuthenticated(false);
setUserData({});
setToken('');
navigate('/');
}
setLoading(false);
});
Expand Down Expand Up @@ -91,7 +89,6 @@ const AuthContextProvider = ({ children }) => {
try {
auth.signOut();
setAuthenticated(false);
navigate('/');
toast.success('Logged out successfully');
} catch (error) {
toast.error(error.message ?? 'Unable to logout');
Expand Down
Loading