K
Kindeβ€’9mo ago
TARS

Can't get the user info from const { getUser } = getKindeServerSession();

I must be totally braindead but I'm just trying to get the information about the user in this way. Using next.js. import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
```
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";

const Page = () => {
const { getUser } = getKindeServerSession();
const user = getUser();

return (
<MpWrapper className="mt-32">
<h1>Dashboard</h1>
<p>User: {user.email}</p>
</MpWrapper>
);
};
```
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";

const Page = () => {
const { getUser } = getKindeServerSession();
const user = getUser();

return (
<MpWrapper className="mt-32">
<h1>Dashboard</h1>
<p>User: {user.email}</p>
</MpWrapper>
);
};
Property 'email' does not exist on type 'Promise<KindeUser | null>'. Isn't it supposed to be an object? I'm logged in and everthing. I'm every writing exactly as Josh tried coding does
18 Replies
MALEV0L3NT
MALEV0L3NTβ€’9mo ago
Hey, I answered in #πŸ’¬β”ƒgeneral but I'll answer here too. You need to make your Page function an async function and to await your getUser call:
const Page = async () => {
...
const user = await getUser();
...
}
const Page = async () => {
...
const user = await getUser();
...
}
TARS
TARSβ€’9mo ago
thank you so much sir, of course. How could I have missed this. I spent 3 hours on this. HGHAHAHHA
MALEV0L3NT
MALEV0L3NTβ€’9mo ago
getUser is an asynchronous function which means in order to use the value it returns (KindeUser | null), we need to wait for the execution to finish. There's multiple ways we can do this, but the best is probably to await it No worries πŸ™‚
TARS
TARSβ€’9mo ago
oh so before the data was actually there it was just a promise
MALEV0L3NT
MALEV0L3NTβ€’9mo ago
Yes exactly. The Promise object represents the eventual completion of a task
TARS
TARSβ€’9mo ago
Its strange it worked for Josh tried coding in his popular video "Build a Complete SaaS Platform with Next.js 13, React, Prisma, tRPC, Tailwind | Full Course 2023". He didnt have async.
MALEV0L3NT
MALEV0L3NTβ€’9mo ago
When we run what you had: const user = getUser(); JavaScript calls the getUser() function and then immediately continues. This returns a Promise as we can't guarantee the getUser function is finished, being an asynchronous task. When we write the await keyword, we're telling JavaScript to wait at that point until the getUser() function is finished
TARS
TARSβ€’9mo ago
I learn new things every day. Thanks.
MALEV0L3NT
MALEV0L3NTβ€’9mo ago
MDN Web Docs
Promise - JavaScript | MDN
The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
MDN Web Docs
await - JavaScript | MDN
The await operator is used to wait for a Promise and get its fulfillment value. It can only be used inside an async function or at the top level of a module.
MALEV0L3NT
MALEV0L3NTβ€’9mo ago
Here's some further reading if you're interested, they're important concepts, especially now with RSC
TARS
TARSβ€’9mo ago
Yeah I know in theory what asyncronious js is. What is RSC? Thanks btw for the extended explanations. Really appreciated.
MALEV0L3NT
MALEV0L3NTβ€’9mo ago
React Server Components (next.js app router)
TARS
TARSβ€’9mo ago
oh yes! ok! I've said for 5 years I'm not going to become a decent developer its just something life put me to do πŸ˜„ but still I learn more everyday. haha. It's actually very fun once you start grasping the concepts a bit better. And kinde is very good for me, because all hackers from Russia are trying to break all Swedish systems now I dont have to worry about the auth security lol
MALEV0L3NT
MALEV0L3NTβ€’9mo ago
I agree πŸ™‚ Btw I see the josh tried coding video you're talking about, curious that he's not using async and it's working for him... Maybe one of the Kinde guys can answer that for us
TARS
TARSβ€’9mo ago
Next time something like this happens I have more wisdom. I agree pretty strange indeed.
peteswah
peteswahβ€’9mo ago
Ah yep, in the Joshtriedcoding video it may have been a previous version of the sdk πŸ™‚
Daniel_Kinde
Daniel_Kindeβ€’9mo ago
Yes, the API was updated to be async I believe after JostTriedCoding video.
MALEV0L3NT
MALEV0L3NTβ€’9mo ago
There we go, that makes sense. Thanks for the clarification πŸ™‚
Want results from more Discord servers?
Add your server