Skip to content

Commit

Permalink
fix(auth) - session change in client 🚑
Browse files Browse the repository at this point in the history
Session change was not visible on client as Auth.js login and signout stopped refreshing the page
solution to implement Session provider with getSession() rather than auth()
refer: nextauthjs/next-auth#10016 (comment)
also fixed Skeleton Lint issues in Loading 🚨
  • Loading branch information
Sanchit-Warang committed May 27, 2024
1 parent 3f6cc48 commit dd8c8b8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
13 changes: 10 additions & 3 deletions src/app/(protected)/settings/signout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
'use client'
import { logout } from '@/server/actions/auth'



const Signout = () => {
return <button onClick={() => logout()}>signout</button>
return (
<button
onClick={async () => {
await logout()
location.reload()
}}
>
signout
</button>
)
}

export default Signout
2 changes: 1 addition & 1 deletion src/app/movie/[movieid]/loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const MoviePageLoading = () => {
return (
<>
<Skeleton>
<VideoPlayer url={`TEMP`} backdrop_path={`TEMP`} poster_path={`TEMP`} />
<VideoPlayer url={`TEMP`} backdrop_path={`TEMP`} poster_path={`TEMP`} type='movie' tmdbId={1}/>
</Skeleton>
<div className="m-10 flex gap-5 flex-wrap">
<Skeleton className='h-[25rem]'>
Expand Down
52 changes: 52 additions & 0 deletions src/app/providers.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
'use client'
/*
workaround for https://github.com/nextauthjs/next-auth/issues/10016#issuecomment-1983672453
the workaround can be understood by the comments below
*/


import { NextUIProvider } from '@nextui-org/react'
import ReactQueryProviders from '@/lib/react-query/react-query-provider'
import { Toaster } from 'react-hot-toast'
import posthog from 'posthog-js'
import { PostHogProvider } from 'posthog-js/react'

// Next Session imports
import { Session } from "next-auth";
import {
SessionProvider as NextSessionProvider,
getSession
} from "next-auth/react";
import { usePathname } from "next/navigation";
import {
ReactNode,
useCallback,
useEffect,
useState
} from "react";
// Next Session imports done





if (typeof window !== 'undefined') {
posthog.init(
process.env.NEXT_PUBLIC_POSTHOG_KEY
Expand All @@ -17,7 +42,33 @@ if (typeof window !== 'undefined') {
}

export function Providers({ children }: { children: React.ReactNode }) {

// Next Session Fix
const [ session, setSession ] = useState<Session | null>(null);
const pathName = usePathname();

const fetchSession = useCallback(async () => {
try {
const sessionData = await getSession();
setSession(sessionData);
} catch (error) {
setSession(null);

if (process.env.NODE_ENV === "development") {
console.error(error);
}
}
}, []);

useEffect(() => {
fetchSession().finally();
}, [fetchSession, pathName]);

//Next Session fix end


return (
<NextSessionProvider session={session}>
<ReactQueryProviders>
<NextUIProvider>
<PostHogProvider client={posthog}>
Expand All @@ -26,5 +77,6 @@ export function Providers({ children }: { children: React.ReactNode }) {
</PostHogProvider>
</NextUIProvider>
</ReactQueryProviders>
</NextSessionProvider>
)
}
20 changes: 15 additions & 5 deletions src/app/scProviders.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { SessionProvider } from 'next-auth/react'
import { auth } from '@/auth'
/*
the Session Provider Stopped Working
the auth() functions works only on reload but the login and signout stopped reloading
so made a Next Session provider in client side
the fix is in providers file
used this for the fix https://github.com/nextauthjs/next-auth/issues/10016#issuecomment-1983672453
*/

// import { SessionProvider } from 'next-auth/react'
// import { auth } from '@/auth'
import { Providers } from './providers'

const SCProviders = async ({ children }: { children: React.ReactNode }) => {
const session = await auth()
// const session = await auth()
return (
<SessionProvider session={session}>
<>
{/* <SessionProvider session={session}> */}
<Providers>{children}</Providers>
</SessionProvider>
{/* </SessionProvider> */}
</>
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/tv/[tvId]/[season]/[episode]/loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const TVShowPageLoading = () => {
return (
<>
<Skeleton>
<VideoPlayer url={`TEMP`} backdrop_path={`TEMP`} poster_path={`TEMP`} />
<VideoPlayer url={`TEMP`} backdrop_path={`TEMP`} poster_path={`TEMP`} type='movie' tmdbId={1}/>
</Skeleton>
<div className="m-10 flex gap-5 flex-wrap">
<Skeleton className="h-[25rem]">
Expand Down

0 comments on commit dd8c8b8

Please sign in to comment.