Skip to content
Open
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
1 change: 0 additions & 1 deletion components/Dashboard/FormPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const actionButtons = [
const FormPane: React.FC = () => {
const { project } = useSelectedProject()
const { forms } = useListForms(project?.id)

const router = useRouter()

const [selectedRow, setSelectedRow] = useState(false)
Expand Down
31 changes: 31 additions & 0 deletions components/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useAuth } from '@/context/Authentication'
import { useRouter } from 'next/router'
import { useEffect, ReactNode } from 'react'

interface ProtectedRouteProps {
children?: ReactNode
}

const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ children }) => {
const { isAuthenticated } = useAuth()
const router = useRouter()

useEffect(() => {
async function checkAuth() {
if (
router.pathname !== '/__app/signin' &&
router.pathname !== '/__app/signup' &&
isAuthenticated &&
!(await isAuthenticated())
) {
router.push('/signin')
}
}

checkAuth()
}, [isAuthenticated, router])

return <>{children}</>
}

export default ProtectedRoute
27 changes: 26 additions & 1 deletion context/Authentication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { useQueryClient } from '@tanstack/react-query'

import { graphqlClient } from '@/api'
import { CreateUserData } from '@/gql/graphql'
import { signinWithEmailAndPasswordQuery } from '@/graphql/queries/user'
import {
getSessionUserQuery,
signinWithEmailAndPasswordQuery,
} from '@/graphql/queries/user'
import { useCreateUser } from '@/hooks/mutation/user'

interface AuthenticationProviderProps {
Expand All @@ -18,6 +21,7 @@ interface IAuthenticationContext {
}) => Promise<null | string | void>
signOut?: () => Promise<void>
createUserWithEmailPassword?: (data: CreateUserData) => Promise<any>
isAuthenticated?: () => Promise<boolean>
}

const defaultContextValues: IAuthenticationContext = {}
Expand Down Expand Up @@ -74,12 +78,33 @@ export const AuthenticationProvider: React.FC<AuthenticationProviderProps> = (
router.push('/signin')
}, [queryClient, isClient, router])

const isAuthenticated: IAuthenticationContext['isAuthenticated'] =
async () => {
const token = localStorage.getItem('__authentication_token__')
if (!token) {
return false
}

const loggedInUser = await graphqlClient
.request(getSessionUserQuery, {})
.catch((error) => {
console.log(error.message)
})

if (!loggedInUser?.getSessionUser) {
return false
}

return true
}

return (
<AuthenticationContext.Provider
value={{
signInWithEmailAndPassword,
signOut,
createUserWithEmailPassword,
isAuthenticated,
}}
>
{children}
Expand Down
9 changes: 6 additions & 3 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Toaster } from 'react-hot-toast'
import { AuthenticationProvider } from '@/context/Authentication'
import '@/styles/globals.css'
import ModalWrapper from '@/wrappers/ModalWrapper'
import ProtectedRoute from '@/components/ProtectedRoute'

const queryClient = new QueryClient({
defaultOptions: {
Expand Down Expand Up @@ -37,9 +38,11 @@ export default function App({ Component, pageProps }: AppProps) {
defaultTheme="system"
enableSystem
>
<ModalWrapper />
<Toaster position="top-right" />
<Component {...pageProps} />
<ProtectedRoute>
<ModalWrapper />
<Toaster position="top-right" />
<Component {...pageProps} />
</ProtectedRoute>
</NextThemesProvider>
</AuthenticationProvider>
<ReactQueryDevtools />
Expand Down