Skip to content

Commit

Permalink
Feat await gql client (#152)
Browse files Browse the repository at this point in the history
* fixing google and github signin

* remove clg

* await gql client

* adding comment
  • Loading branch information
papistacoding authored Jan 17, 2025
1 parent 860cb65 commit de26d43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 23 additions & 2 deletions apps/console/src/app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ import { useSession } from 'next-auth/react'
import { ThemeProvider } from '@/providers/theme'
import { usePathname } from 'next/navigation'
import { ReactNode, useEffect, useState } from 'react'
import { Loading } from '@/components/shared/loading/loading'

interface ProvidersProps {
children: ReactNode
}

//IF YOU ADD PUBLIC PAGE, ITS REQUIRED TO CHANGE IT IN middleware.tsx
const publicPages = ['/login', '/verify', '/resend-verify', '/waitlist', '/invite']

const Providers = ({ children }: ProvidersProps) => {
const { data: session, status } = useSession()
const pathname = usePathname()
const [client, setClient] = useState<Client | null>(null)
const [accessToken, setAccessToken] = useState<string | null>(null)

const isPublicPage = publicPages.includes(pathname)

useEffect(() => {
const tokenChanged = session?.user.accessToken && session?.user.accessToken !== accessToken

if (status === 'authenticated' && tokenChanged) {
setAccessToken(session?.user.accessToken)
setClient(createClient(session))
Expand All @@ -28,11 +35,25 @@ const Providers = ({ children }: ProvidersProps) => {
}
}, [session?.user.accessToken, status, pathname, accessToken])

if (status === 'loading' || (status === 'authenticated' && !client)) return null
if (status === 'loading') {
return <Loading />
}

if (isPublicPage) {
return (
<ThemeProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange>
{children}
</ThemeProvider>
)
}

if (!client) {
return <Loading />
}

return (
<ThemeProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange>
{client ? <GraphqlProvider value={client}>{children}</GraphqlProvider> : children}
<GraphqlProvider value={client}>{children}</GraphqlProvider>
</ThemeProvider>
)
}
Expand Down
2 changes: 2 additions & 0 deletions apps/console/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export const config = {
* - waitlist (waitlist page)
* - invite (invite verify page)
*/

//IF YOU ADD PUBLIC PAGE, ITS REQUIRED TO CHANGE IT IN Providers.tsx
'/((?!api|[_next/static]|[_next/image]|favicon.ico|backgrounds|backgrounds/|icons|icons/|login|verify|resend-verify|waitlist|invite).*)',
],
}

0 comments on commit de26d43

Please sign in to comment.