Accessing Secret Values

Learn how to access the secret values in the configuration object in an application.

Previously, we’ve used a couple of values that should be secret, but we initially kept them on the code. Those are values that might change from environment to environment, and configuration we want to keep as a secret for security reasons. This requirement makes it impossible to have them checked out into version control, so they have to live somewhere else.

One common practice to do this is to get these values from the environment, using environment variables. Deno provides an API that we’ll use to read from environment variables. We’ll be extending the config module so that it also includes secret values on its exported object of type Configuration.

Here are the values that are supposed to be secrets and that are still living in the code:

  • MongoDB username
  • MongoDB password
  • JWT encryption key

Add the logic to the configuration object

Let’s get them out of the code and add them to the configuration object by following these steps:

  1. In src/config/index.ts, add the MongoDB username and password to the configuration and the key to JWT in the configuration type, as follows:

Get hands-on with 1400+ tech skills courses.