Is it okay to fetch data like this? I don't know, seems like I am making some mistakes
Unanswered
Giant Angora posted this in #help-forum
Giant AngoraOP
import { getServerAuthSession } from '@/server/auth';
import { api } from '@/trpc/server';
import MainNav from './MainNav';
import UserNav from './UserNav';
import LogInButton from '@/app/components/LogInButton';
export default async function Header() {
const session = await getServerAuthSession();
const user =
session && session.user
? await api.user.getById({
id: session.user.id,
})
: null;
return (
<header className="sticky top-0 z-10 w-full">
<div className="m-auto flex max-w-screen-xl items-center justify-between py-4">
<MainNav />
{user ? <UserNav user={user} /> : <LogInButton />}
</div>
</header>
);
}11 Replies
Madeiran sardinella
Hi, I think it's ok. Maybe if you use an if instead of a ternary it looks more legible.
On the other hand, can you make the user who returns with the session contain all the data you need? to avoid that additional request
On the other hand, can you make the user who returns with the session contain all the data you need? to avoid that additional request
Giant AngoraOP
I’ve been trying to add new data to the user session without any luck, I am using next-auth
Here is the other thread I created about my issue https://nextjs-forum.com/post/1245804937573564487
Blue orchard bee
That looks fine, you could maybe make it more clear in another variable.
const { id } = session && session.user
const user = api.user.getById({ id })@Giant Angora Here is the other thread I created about my issue https://discord.com/channels/752553802359505017/1245804937573564487
Madeiran sardinella
Okay, I understand, can you try adding this to your authOptions providers?
This is just an example with google provider.
providers: [
GoogleProvider({
// ...
profile(profile, tokens) {
return {
id: tokens.id_token || profile.id,
name: profile.name,
email: profile.email,
credits: profile.credits ?? 0,
}
},
}),
],This is just an example with google provider.
Giant AngoraOP
Okay give me a sec
Where I am supposed to get this tokens and profile? Getting bunch of errors
Madeiran sardinella
Only TS errors?
Giant AngoraOP
Yes
Maybe that can be useful for you