Skip to content

How to create the .env file ?

esafar edited this page Apr 27, 2023 · 7 revisions

Depending on your app, you could need more or less datas that must stay away from public github repositories. The more sensitives one are:

  1. API keys
  2. Secrets

For easier usage, it could also be interesting using env variables for urls and macros.

How to write my .env file for 42 auth?

DATABASE_NAME="postgres"
DATABASE_PORT="5432"

POSTGRES_USER="esafar"
POSTGRES_PASSWORD="strongpassword"
POSTGRES_DB="postgresdatabase"

DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE_NAME}:${DATABASE_PORT}/${POSTGRES_DB}?schema=public"
# If you are generating JsonWeb Tokens
JWT_SECRET='mysupersecret'

FRONTEND_URL="http://localhost:3000"
BACKEND_URL="http://localhost:3333"
SOCKET_URL="http://localhost:3080"
GAME_URL="http://localhost:4343"

# This is the redirect URI found in my 42 intranet, where the app was created
# You can simply copy past it
API42_REACT_APP_URL="https://api.intra.42.fr/oauth/authorize?client_id=myclientid&redirect_uri=myredirecturi&response_type=code"
# You can simply copy past it
API42_CLIENT_ID="myclientid"
# You can simply copy past it
API42_CLIENT_SECRET="myclientsecret"
# You enters here where you want your connection with 42 account to redirect you
# Example: for me, it was to my backend request: http://localhost:3333/auth/42/callback
API42_REDIRECT_URI="where_i_want_the_callback_to_go_after_connection"

How to use env variables in my backend?

  1. Install the packages:
$ npm i --save @nestjs/config
  1. Import the module in your code
  2. Call the var using:
process.env.API42_CLIENT_SECRET;

How to use env variables in my frontend?

  1. Depending on your app, it could be different. But the logic is the same to the backend. Let's say I am using React. The only difference would be that my env variables must start with REACT_APP_ in order to make react recognize them.

Documentation

how to import the module in my nestjs: https://docs.nestjs.com/techniques/configuration how to use env var in react: https://create-react-app.dev/docs/adding-custom-environment-variables/