❌ Attempted to access a server-side environment variable on the client

Hi all, I just started a new T3 project where I would like to redirect my users from the login page based on the role that they have set in their profile. Instead of using UseEffect, I was hoping it would be faster using ServerSideProps instead, but the error I get says that I am trying to use a NODE_ENV environment variable on the client side. Do I need to do any special customization for being able to do this?
47 Replies
peternovak
peternovakOP2y ago
The code that I am trying to add is as a test as simple as this:
peternovak
peternovakOP2y ago
That code above does not work. It generates me the error that the NODE_ENV can't be found
peternovak
peternovakOP2y ago
This piece of code does not really do anything, but it runs without errors:
peternovak
peternovakOP2y ago
This is kind of what I ideally would have liked to do using the getServerAuthSession in vanilla auth.ts:
deforestor
deforestor2y ago
That's because you have to validate it on the schema.mjs
deforestor
deforestor2y ago
deforestor
deforestor2y ago
Here, you can see that we have the client env
deforestor
deforestor2y ago
If you use an older version of t3, before t3-env, then don't forget import the correct env
peternovak
peternovakOP2y ago
Thanks for writing back @deforestor ! I have pretty much the latest code, created my app in last week, so currently on 7.13.1. My env.mjs looks like this:
deforestor
deforestor2y ago
Yeah there's the problem, you should have the client envirionment variables on the client object Hold on a minute. If you're talking about the env in the auth options file, then there's something weirder going on thinkies
peternovak
peternovakOP2y ago
All I wanted to do really is to check what role that the user has when they log in, so I could direct them to a 'Fill out your profile' page or to show them a dashboard suitable for their role
peternovak
peternovakOP2y ago
I was hoping that using getServerSideProps would save me that pageload I would get if using useEffect
deforestor
deforestor2y ago
It should try with the getSession from NextAuth
deforestor
deforestor2y ago
But that should have worked. It's working on mine
deforestor
deforestor2y ago
I'm doing the same thing as you
peternovak
peternovakOP2y ago
Cool, give me a sec!
deforestor
deforestor2y ago
you'd do getSession({ctx})
peternovak
peternovakOP2y ago
You mean something like this?
peternovak
peternovakOP2y ago
Hmm, I must be missing something here
deforestor
deforestor2y ago
no
deforestor
deforestor2y ago
deforestor
deforestor2y ago
same thing you did before
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const session = await getSession({ctx});
console.log(session)
return {}
};
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const session = await getSession({ctx});
console.log(session)
return {}
};
just for a testing purpose, I want to see if that would work Because I don't thing this should cause that error at all. It is happening on the server
peternovak
peternovakOP2y ago
if using getSession() was what you had in mind I got this:
peternovak
peternovakOP2y ago
The code snippet you sent gave this:
deforestor
deforestor2y ago
oops sorry It was supposed to be getSession
peternovak
peternovakOP2y ago
Thanks, this is the best attempt so far... it does nothing, which means no references for NODE_ENV and not linting warnings. Not it is just about getting the code to actually do something 🙂
deforestor
deforestor2y ago
That still returns nothing?
peternovak
peternovakOP2y ago
No, nothing that I can relate to this component. Could I be missing something trivial in the rest of the page?
deforestor
deforestor2y ago
wait hold on You're checking the server's terminal, correct? Because you can't log the serverside props in the browser. Also, useSession would return undefined because you're still not logged in, so that's expected
peternovak
peternovakOP2y ago
I am getting this in the servers terminal:
peternovak
peternovakOP2y ago
But I do get this in my console.log, so the signin in itself seams to be working
deforestor
deforestor2y ago
Hmm.. you're doing it in the log in page, right?
peternovak
peternovakOP2y ago
No, I am doing this in a '/user/signin' route
deforestor
deforestor2y ago
I see. Yeah this is really strange, I've had all those problems before, but my solutions are not working here But I have not tried with the newest t3 yet I'll say though that getServerAuthSession on the getServerSideProps should not throw that error. In fact, I tried creating a new project and doing it, and it really didn't throw
peternovak
peternovakOP2y ago
Wow, you where right!
peternovak
peternovakOP2y ago
I tried this in my main index.tsx:
peternovak
peternovakOP2y ago
Everything worked
deforestor
deforestor2y ago
try the getServerAuthSession now, it should be the correct one yeah I assumed it's because signIn shouldn't have the session yet
peternovak
peternovakOP2y ago
Ok, I believe this has to do where the file is located. The initial file I tested with is not in the /pages directory, and making it an NextPage was a test I did quite late. When I moved my getServerSideProps to the page directory of the '/user/signin', everything worked
deforestor
deforestor2y ago
Ah- Yes, Next.js' folder structure is non optional
peternovak
peternovakOP2y ago
I didn't think about it at all before you asked what route i was using
deforestor
deforestor2y ago
You CANNOT put page files on other places, and you cannot put components on ./pages Now that error makes sense haha
peternovak
peternovakOP2y ago
Yeah, I will never forget that lesson after this! 🙂 That A LOT for all the help tonight!! 🙏 Super appreciated!
deforestor
deforestor2y ago
no problem man
peternovak
peternovakOP2y ago
Can't believe I didn't think about the file structure before you asked!!
deforestor
deforestor2y ago
Yeah take a read on the routing docs if you're still confused, it's pretty simple
peternovak
peternovakOP2y ago
Will do! Thanks a lot!!
Want results from more Discord servers?
Add your server