Accessing Secret Values
Learn how to access the secret values in the configuration object in an application.
We'll cover the following
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:
- In
src/config/index.ts
, add the MongoDBusername
andpassword
to the configuration and thekey
to JWT in the configuration type, as follows:
Get hands-on with 1400+ tech skills courses.