K
Kinde13mo ago
IkiTg07

Is there a way to track session state

I want to know if there's a way for me to track, at the root level (layout.tsx) , the state of the user session. Logged in, Logging out...
17 Replies
Oli - Kinde
Oli - Kinde13mo ago
Hi @IkiTg07 , What SDK are you using?
IkiTg07
IkiTg07OP13mo ago
Hello, I’m using the one for NextJs
Oli - Kinde
Oli - Kinde13mo ago
Hey @IkiTg07 , Absolutely, you can track the state of the user session at the root level in your Next.js application. You can use the getKindeServerSession function from the @kinde-oss/kinde-auth-nextjs/server package to get the user session data. Here's an example of how you can use it in your layout.tsx: import { useEffect, useState } from "react"; import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server"; export default function RootLayout({children}: {children: React.ReactNode}) { const [user, setUser] = useState<any>(); const [authStatus, setAuthStatus] = useState(null); useEffect(() => { const getKindeSession = async () => { const {getUser, isAuthenticated} = getKindeServerSession(); const user = await getUser(); const authenticated = await isAuthenticated(); setUser(user); setAuthStatus(authenticated); }; getKindeSession(); }, []); return ( <div> {/* You can now use user and authStatus in your layout */} {children} </div> ); } In this example, user and authStatus states are updated whenever the session changes. You can use these states throughout your layout to track the user session. Please note that this is a server-side operation, so it won't cause any re-renders on the client side. If you need to react to changes in the session state on the client side, you might want to consider using a state management library like Redux or Zustand, or the built-in Context API in React. Also, remember to handle the case where user and authStatus are null or undefined, as they will be before the first render and until the session data is fetched. Let me know if you need more help with this!
IkiTg07
IkiTg07OP13mo ago
But on that note wouldn’t it create an error ? getKindeServerSession works on the server but useEffect only works in client components so I’ll have to mark the layout as “use client” and getKindeServerSession will fail
Oli - Kinde
Oli - Kinde13mo ago
Let me ask my NextJS experts in my team and get back to you.
IkiTg07
IkiTg07OP13mo ago
Okay thank you 😊
peteswah
peteswah13mo ago
hey @IkiTg07 I think the best we've got at the moment is useKindeServerSession() in the Layout which returns isAuthenticated
const {isAuthenticated} = useKindeServerSession();

if(!(await isAuthenticated())) return <>Not authenticated</>
const {isAuthenticated} = useKindeServerSession();

if(!(await isAuthenticated())) return <>Not authenticated</>
IkiTg07
IkiTg07OP13mo ago
We do really need a method to get the session in the client-side because I need has Oli suggested to use useEffect but as soon as we go from server-comp to -> client-comp I can no longer get the isAuthenticated status. I am building an upload functionality using edgestore and because I have a context for authenticated users the owner told me I should call a reset utility everytime the user auth status changes because it is causing issues in my app. And that reset util can only be used inside client-components
Oli - Kinde
Oli - Kinde13mo ago
Hi @IkiTg07 , It's late here in Sydney so it will be hard for me to get the attention of my nextJS expert teammates, but I will get them to help you first thing Monday.
IkiTg07
IkiTg07OP13mo ago
Okay no prob for me thank you !
peteswah
peteswah13mo ago
Hey @IkiTg07 I happen to be up now haha Was just working on the nextjs sdk v2 which should allow you to do this client side
peteswah
peteswah13mo ago
Once it has been merged you can update to the latest version of @kinde-oss/kinde-auth-nextjs and you should be able to follow these docs: https://kinde.notion.site/Next-js-App-Router-v2-e7a16d8ae38e45b6ad052910075e24ef?pvs=4
Kinde on Notion
Next.js App Router v2
Getting started
peteswah
peteswah13mo ago
Something like this:
// app/protected/page.tsx - Client component
"use client";

import { useKindeBrowserClient } from "@kinde-oss/kinde-auth-nextjs";
import { LoginLink } from "@kinde-oss/kinde-auth-nextjs/components";

export default function Admin() {
const { isAuthenticated, isLoading } = useKindeBrowserClient();

if (isLoading) return <div>Loading...</div>;

return isAuthenticated ? (
<div>Admin content</div>
) : (
<div>
You have to <LoginLink>Login</LoginLink> to see this page
</div>
);
}
// app/protected/page.tsx - Client component
"use client";

import { useKindeBrowserClient } from "@kinde-oss/kinde-auth-nextjs";
import { LoginLink } from "@kinde-oss/kinde-auth-nextjs/components";

export default function Admin() {
const { isAuthenticated, isLoading } = useKindeBrowserClient();

if (isLoading) return <div>Loading...</div>;

return isAuthenticated ? (
<div>Admin content</div>
) : (
<div>
You have to <LoginLink>Login</LoginLink> to see this page
</div>
);
}
IkiTg07
IkiTg07OP13mo ago
Awesome ! I like it thanks ! Some predictions on when that v2 will be merged ?
peteswah
peteswah13mo ago
Hey @IkiTg07 today!
IkiTg07
IkiTg07OP13mo ago
Haha awesome ! Thanks and great job to the team
Want results from more Discord servers?
Add your server