❌ 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
The code that I am trying to add is as a test as simple as this:
That code above does not work. It generates me the error that the NODE_ENV can't be found
This piece of code does not really do anything, but it runs without errors:
This is kind of what I ideally would have liked to do using the getServerAuthSession in vanilla auth.ts:
That's because you have to validate it on the schema.mjs
Here, you can see that we have the client env
If you use an older version of t3, before t3-env, then don't forget import the correct env
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:
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 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
I was hoping that using getServerSideProps would save me that pageload I would get if using useEffect
It should
try with the
getSession
from NextAuthBut that should have worked. It's working on mine
I'm doing the same thing as you
Cool, give me a sec!
you'd do
getSession({ctx})
You mean something like this?
Hmm, I must be missing something here
no
same thing you did before
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
if using getSession() was what you had in mind I got this:
The code snippet you sent gave this:
oops sorry
It was supposed to be
getSession
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 🙂
That still returns nothing?
No, nothing that I can relate to this component. Could I be missing something trivial in the rest of the page?
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
I am getting this in the servers terminal:
But I do get this in my console.log, so the signin in itself seams to be working
Hmm.. you're doing it in the log in page, right?
No, I am doing this in a '/user/signin' route
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 throwWow, you where right!
I tried this in my main index.tsx:
Everything worked
try the getServerAuthSession now, it should be the correct one
yeah I assumed it's because signIn shouldn't have the session yet
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
Ah-
Yes, Next.js' folder structure is non optional
I didn't think about it at all before you asked what route i was using
You CANNOT put page files on other places, and you cannot put components on
./pages
Now that error makes sense hahaYeah, I will never forget that lesson after this! 🙂
That A LOT for all the help tonight!! 🙏
Super appreciated!
no problem man
Can't believe I didn't think about the file structure before you asked!!
Yeah take a read on the routing docs if you're still confused, it's pretty simple
Will do!
Thanks a lot!!