Keeping Your Data Secure: Environment Variables in NextJS / React

  Рет қаралды 25,821

Josh tried coding

Josh tried coding

Күн бұрын

Environment variables (.env) are THE way to keep your sensitive data like API keys secure and away from users. You define them in special .env.local files on your local machine and then define them wherever you deploy your React / NextJS project.
While you can use secure .env variables in NextJS, due to the nature of React (being client-side rendered), you can only use public environment variables in React. To interact with APIs in React, you'd make a request to a NodeJS server that contains the server-side env variables.
Hope you found this video helpful, and have fun coding your following projects!
Josh

Пікірлер: 25
@codewithguillaume
@codewithguillaume Жыл бұрын
Hello Josh, I think that a lot of juniors make time to understand that sensitive informations are a very serious subject. Thanks for this efficient tutorial - please continue videos !
@joshtriedcoding
@joshtriedcoding Жыл бұрын
Cheers, they really are one of the most important beginner-concepts
@snivels
@snivels Жыл бұрын
If you want a goldmine of a video to make, it'll be how to get runtime environment variables working (not build time) with next js and docker without them ending up inside the docker image. It seems like this is a huge issue, and has been for a while with no clear solution. Love your videos!
@skyhappy
@skyhappy 8 ай бұрын
What variables would only be passed in at runtime?
@snivels
@snivels 8 ай бұрын
@@skyhappy think environment urls or environment secrets to be used in api routes, you'd want them at runtime when you spin up the container so that you don't have to rebuild the image for every environment, you can just promote the same image through environments. That and you don't want secrets being written into the image, like they would be at build time.
@skyhappy
@skyhappy 8 ай бұрын
@@snivels do you know the solution to this
@srinivasjoshi4693
@srinivasjoshi4693 Жыл бұрын
Your Channel will blow up soon. Thank you for amazing content 🔥
@simonedwards7101
@simonedwards7101 10 ай бұрын
Thanks for the video Josh. Being new to all this I ended up using env-cmd in the scripts of package.json to load the differences between my home office and town office. Now I just "npm run dev-ho" or "npm run dev-to". I could have just used localhost for both client and server but wanted to do something fancy and use ip addresses based on the specific locations of the offices. All a good learning exercise either way 🙂
@peter042
@peter042 Жыл бұрын
These are high quality videos, keep pushing bro, and you'll get the rewards
@ThomasAlbuquerque
@ThomasAlbuquerque Жыл бұрын
I didn't know about NEXT_PUBLIC prefix in environment variables, thank you so much
@clickbaitpolice9792
@clickbaitpolice9792 10 ай бұрын
Josh please reply. I want to use Firebase. I’ve set up my variables but I can’t get my config file to read them. It’s all undefined. I’ve tried putting “use server” on the top of the JS file and still nothing works
@techwizard961
@techwizard961 9 ай бұрын
Same here. Have you found any solutions??
@skyhappy
@skyhappy 8 ай бұрын
You should use supabase, it is a better version of firebase as it uses SQL to model your schema which makes life easier. Also the API is way better made as it's essentially a query builder which means it's similar to SQL.
@davidlintin
@davidlintin 6 ай бұрын
Make sure your .env file is at the root of your project 👍🏻
@bsimpson888
@bsimpson888 8 ай бұрын
Hello Josh, I discovered that with the introduction of the app directory (next 13 i think ) , they changed the behavior of NEXT_PUBLIC environment variables. However it seems that only the variables specified during "build" time are available but not those specified during "runtiime" (e.g. set as env for a docker image ). It would be nice if someone ( preferably you ) could explain how to pass such "deployment" specific variables to the client side, when using next 13 or 14.
@dmytro.nazaruk
@dmytro.nazaruk 2 ай бұрын
Hey! What’s the name of extension that shows the weight of bundles next to import command? 3:03
@manoranjands9479
@manoranjands9479 2 күн бұрын
Thanks alot for this video
@kanodekoubillybrown3025
@kanodekoubillybrown3025 Жыл бұрын
My env variables are not available in production , why ?
@JoonatanKarhu
@JoonatanKarhu Жыл бұрын
Hi, I'm following your tutorial and my .env.local variable won't show in console.log(process.env.variableName); I'm looking for the log value by running in vs code command line: node fileName.js I get undefined as a value Please help, I have been stuck with this for a long time.
@arizona2233
@arizona2233 Жыл бұрын
adding NEXT_PUBLIC in front of the variable in .env is needed to console.log
@skyhappy
@skyhappy 8 ай бұрын
​@@arizona2233isn't it also because he has to run it off the build generated by next build, as in the building phase the variables are inserted?
@chelseamjames1
@chelseamjames1 Жыл бұрын
great explanation!
@saurabhdaswant7833
@saurabhdaswant7833 Жыл бұрын
very helpfull thanks!
@EmmanuelAgyemang-d7u
@EmmanuelAgyemang-d7u Ай бұрын
You are intelligent
@kacperkepinski4990
@kacperkepinski4990 Жыл бұрын
it would be better for me not to watch that
Players vs Corner Flags 🤯
00:28
LE FOOT EN VIDÉO
Рет қаралды 55 МЛН
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 15 МЛН
Worst flight ever
00:55
Adam W
Рет қаралды 22 МЛН
5 JavaScript API Key Mistakes (and how to fix them)
12:49
James Q Quick
Рет қаралды 77 М.
How to hide your API keys SAFELY when using React
24:45
Code with Ania Kubów
Рет қаралды 170 М.
Locking Down Next.js Security: Safeguard Your App!
20:25
Code Ryan
Рет қаралды 2,3 М.
10 common mistakes with the Next.js App Router
20:37
Vercel
Рет қаралды 214 М.
DON'T USE Environment Variables Without This
13:28
James Q Quick
Рет қаралды 21 М.
Fetching Data Doesn't Get Better Than This
6:58
Josh tried coding
Рет қаралды 118 М.
All 29 Next.js Mistakes Beginners Make
1:45:10
ByteGrad
Рет қаралды 127 М.
Players vs Corner Flags 🤯
00:28
LE FOOT EN VIDÉO
Рет қаралды 55 МЛН