getServerSideProps() fetches NextAuth session put fails to pass props to Next.js page components

I'm building a note-taking app with ct3a, currently working on implementing basic auth features in the UI. Here's my GitHub repo - the relevant code can be found in the auth branch: https://github.com/ChromeUniverse/luccanotes/tree/auth I'm trying to call getServerSideProps() in /pages/notes/index.tsx to fetch my NextAuth session with ct3a's getServerAuthSession() wrapper and pass it as a prop to the main NotesPage() page component. Next.js appears to be fetching the auth session properly - I confirm this through the serialized __NEXT_DATA__ in the browser - but for reason the session won't get passed down into the main page component. Server-side console logs also confirm this:
getServerSideProps session: {
user: {
name: 'Lucca Rodrigues',
email: ***REDACTED FOR PRIVACY***,
image: 'https://lh3.googleusercontent.com/a/AEdFTp4oe6lvmwJ97pwAFe47WzIF6S0dIez7j-T-0lT3hA=s96-c',
id: 'clefvo6jy0002wghoecqhp8y3'
},
expires: '2023-03-24T18:48:28.460Z'
}
Notes page props: { className: 'jsx-3109855100 ' }
getServerSideProps session: {
user: {
name: 'Lucca Rodrigues',
email: ***REDACTED FOR PRIVACY***,
image: 'https://lh3.googleusercontent.com/a/AEdFTp4oe6lvmwJ97pwAFe47WzIF6S0dIez7j-T-0lT3hA=s96-c',
id: 'clefvo6jy0002wghoecqhp8y3'
},
expires: '2023-03-24T18:48:28.460Z'
}
Notes page props: { className: 'jsx-3109855100 ' }
I've tried strongly typing getServerSideProps and my main NotesPage page component with Next's TypeScript types but still no luck:
// src/pages/notes/index.tsx
export const getServerSideProps: GetServerSideProps<{
session: Session;
}> = async (context) => {
const session = await getServerAuthSession(context);

console.log("getServerSideProps session:", session);

if (!session) {
return {
redirect: {
destination: "/",
permanent: false,
},
};
}

// Pass data to the page via props
return { props: { session } };
};

function NotesPage({
session,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
// component logic and UI markup here...
}
// src/pages/notes/index.tsx
export const getServerSideProps: GetServerSideProps<{
session: Session;
}> = async (context) => {
const session = await getServerAuthSession(context);

console.log("getServerSideProps session:", session);

if (!session) {
return {
redirect: {
destination: "/",
permanent: false,
},
};
}

// Pass data to the page via props
return { props: { session } };
};

function NotesPage({
session,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
// component logic and UI markup here...
}
What am I missing here? Did I misspell any functions or forget to export a function that Next.js needs for server-side rendering?
GitHub
GitHub - ChromeUniverse/luccanotes at auth
A full-stack note-taking app for Markdown lovers ❤️ - GitHub - ChromeUniverse/luccanotes at auth
25 Replies
lucca
lucca2y ago
Here is the serialized __NEXT_DATA__ in the browser. I'm assuming this means that getServerSideProps() is working? But why isn't the actual page component receiving the props?
cje
cje2y ago
You still need to call useSession in the component
cje
cje2y ago
lucca
lucca2y ago
But why would that be case? If you're already passing the session to your component through getServerSideProps() what's the point of the useSession() hook? I couldn't find anything mentioning this in the NextAuth docs
cje
cje2y ago
The session lives in context look at what happens when you pass a second arg in props i agree it feels a bit like magic and could be documented more clearly
cje
cje2y ago
lucca
lucca2y ago
Interesting. Got the same results here. So literally every other arg will get passed to components as props expect the NextAuth session? that's pretty weird ngl Yeah I've been double-checking the NextAuth docs and I still haven't found any mention of this but using gssp and useSession() totally works The page is in fact server-side rendered but the session gets passed as a prop behind the scenes and is only acessible through useSession()...? I think?
cje
cje2y ago
Look in app.tsx When you log props in your component, you’re not logging directly what gssp passes Session gets destructured iirc
lucca
lucca2y ago
Oh, you mean this? (line 20)
cje
cje2y ago
Yea Session gets put in the provider and everything else gets passed down
lucca
lucca2y ago
well shit that actually makes sense
cje
cje2y ago
Having session as a random object in your props wouldn’t actually do much
lucca
lucca2y ago
Okay but something that's still kinda confusing is that useSession() still works even if you don't use gssp in your page I'm not sure what it does behind the scenes (an API request maybe?) but it can still return your current auth session without the page being server-rendered first
cje
cje2y ago
UseSession first checks the provider Or actually that’s not really true It always only checks the provider But if the provider has no session or an outdated session, it makes a network request Iirc it actually treats the gssp session as outdated so it fetches again on hydration
lucca
lucca2y ago
huh the more you know tysm! @cje btw how did you figure this out? I mean, the session destruct in _app.tsx seems kinda obvious in hindsight but again, there's nothing in the docs about this useSession() behavior, and using gssp seems like a pretty common use-case too imo
cje
cje2y ago
I put some console.logs in the callback functions that you can customise Also read some of the library code a while ago When create t3 app’s auth. was suddenly broken due to a bug in next-auth
lucca
lucca2y ago
Oh i see, cool cool cool I guess I'm just surprised I've haven't seen this mentioned elsewhere thanks again tho
cje
cje2y ago
If in doubt, command-click the import 🙂
lucca
lucca2y ago
in this case what import should I cmd-click exactly? 😅
cje
cje2y ago
Whichever one you don’t understand what it does
lucca
lucca2y ago
But that usually just points to TS type definitions right? This might just be my noob brain kicking in but I'm not sure how to take advantage of this (in this particular scenario with useSession() specifically)
cje
cje2y ago
All the libraries are just JavaScript So you can read them It’s difficult at first, but a good thing to get started with early Also when you’re reading files in your node_modules, you can stick console logs etc in there
lucca
lucca2y ago
Oh right, I see what you mean Yeah I'm ngl, reading through lib code seems kinda daunting Messing around with code in node_modules seems kinda dangerous lol I mean it's not even version-tracked, is this actually safe to do? If i end deleting something on accident, do I just npm i again?
cje
cje2y ago
Yea
lucca
lucca2y ago
okie dokie then lol Yeah imma give it a go sometime later then Thanks again!
Want results from more Discord servers?
Add your server