Read rails encrypted credentials in webpacker react app
Asked Answered
K

1

6

With .env files, it was easy to inject variables in webpacker js packs. Since 5.2, you can use the encrypted secrets, but is there a way to read (decrypt) them in inject some of them on webpacker build time?

Kenaz answered 30/10, 2020 at 10:21 Comment(0)
S
10

You can read from credentials and pass that value to Webpacker.

  1. Create a config/initializers/webpacker.rb file.

  2. Pass it to the Webpacker::Compiler through file above.

    Webpacker::Compiler.env['VALUE'] = Rails.application.credentials.dig(:value)
    
  3. Read it as console.log(process.env.VALUE).

  4. If you want to try it on development, you need to add these to the bin/webpack-dev-server:

    require_relative '../config/application'
    Rails.application.initialize!
    

Sources:

Saltworks answered 13/12, 2020 at 15:19 Comment(4)
thats brilliant!Impossibility
require_relative '../config/environment' might server as shortcut for the webpack-dev-server snippet aboveImpossibility
@WasifHossain do you mean as removing the second step?Saltworks
i meant it as equivalent to the 4th stepImpossibility

© 2022 - 2024 — McMap. All rights reserved.