❌ 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
peternovak12mo ago
The code that I am trying to add is as a test as simple as this:
peternovak
peternovak12mo ago
That code above does not work. It generates me the error that the NODE_ENV can't be found
peternovak
peternovak12mo ago
This piece of code does not really do anything, but it runs without errors:
peternovak
peternovak12mo ago
This is kind of what I ideally would have liked to do using the getServerAuthSession in vanilla auth.ts:
deforestor
deforestor12mo ago
That's because you have to validate it on the schema.mjs
deforestor
deforestor12mo ago
deforestor
deforestor12mo ago
Here, you can see that we have the client env
deforestor
deforestor12mo ago
If you use an older version of t3, before t3-env, then don't forget import the correct env
peternovak
peternovak12mo 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
deforestor12mo 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
peternovak12mo 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
peternovak12mo ago
I was hoping that using getServerSideProps would save me that pageload I would get if using useEffect
deforestor
deforestor12mo ago
It should try with the getSession from NextAuth
deforestor
deforestor12mo ago
But that should have worked. It's working on mine
deforestor
deforestor12mo ago
I'm doing the same thing as you
peternovak
peternovak12mo ago
Cool, give me a sec!
deforestor
deforestor12mo ago
you'd do getSession({ctx})
peternovak
peternovak12mo ago
You mean something like this?
peternovak
peternovak12mo ago
Hmm, I must be missing something here
deforestor
deforestor12mo ago
no
deforestor
deforestor12mo ago
deforestor
deforestor12mo 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
peternovak12mo ago
if using getSession() was what you had in mind I got this:
peternovak
peternovak12mo ago
The code snippet you sent gave this:
deforestor
deforestor12mo ago
oops sorry It was supposed to be getSession
peternovak
peternovak12mo 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
deforestor12mo ago
That still returns nothing?
peternovak
peternovak12mo ago
No, nothing that I can relate to this component. Could I be missing something trivial in the rest of the page?
deforestor
deforestor12mo 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
peternovak12mo ago
I am getting this in the servers terminal:
peternovak
peternovak12mo ago
But I do get this in my console.log, so the signin in itself seams to be working
deforestor
deforestor12mo ago
Hmm.. you're doing it in the log in page, right?
peternovak
peternovak12mo ago
No, I am doing this in a '/user/signin' route
deforestor
deforestor12mo 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
peternovak12mo ago
Wow, you where right!
peternovak
peternovak12mo ago
I tried this in my main index.tsx:
peternovak
peternovak12mo ago
Everything worked
deforestor
deforestor12mo 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
peternovak12mo 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
deforestor12mo ago
Ah- Yes, Next.js' folder structure is non optional
peternovak
peternovak12mo ago
I didn't think about it at all before you asked what route i was using
deforestor
deforestor12mo ago
You CANNOT put page files on other places, and you cannot put components on ./pages Now that error makes sense haha
peternovak
peternovak12mo ago
Yeah, I will never forget that lesson after this! 🙂 That A LOT for all the help tonight!! 🙏 Super appreciated!
deforestor
deforestor12mo ago
no problem man
peternovak
peternovak12mo ago
Can't believe I didn't think about the file structure before you asked!!
deforestor
deforestor12mo ago
Yeah take a read on the routing docs if you're still confused, it's pretty simple
peternovak
peternovak12mo ago
Will do! Thanks a lot!!
Want results from more Discord servers?
Add your server
More Posts
replace [projectId] with project nameCurrently my url looks like this: http://localhost:3000/projects/asdjkfb67284v. How can I replace itLinter & type check in nextjs apphi if all the linter & typecheck is already done in each PR branch with CI, do i still need to do Consumer tRPC client package from private API type?I have a monrepo (template from t3-turbo), with a tRPC API package. I'd like to expose a router for SKIP_ENV_VALIDATIONI have this set to 'true' in my .env but I'm still getting errors about missing envs (they aren't miHow to add unique username to user that signed in via provider?What I'm trying to do: I'm trying to force user to add username after they signed in via provider. ARecommended architecture for my simple web scraping data visualizer for gym capacityHello. As of now, I have set up a lambda function that runs every 15min (scheduled by AWS EventBridgIf I make a cross platform website/app with nextJS + react native, how does the backend work?i havent used nextJS before so its a gray area for me , will my react-native app share the same backwhat should i know about backend before starting with nextjs?I am learning react for a while, and I am felling pretty comfortable with building things with that Invalid Environment VariablesI am having an issue with accessing my ENVs within my drizzle.config.ts. I'm getting an error about I need help understanding request body typesHello 👋 I need help understanding how to tell typescript what type of value I expect `json` to be