Search code examples
ruby-on-railsrubypush-notificationruby-on-rails-5web-push

Where should I put keys at my rails app?


I am trying to use the gem webpush to create push notifications at my rails app.

At this part at tutorial he say:

"Use webpush to generate a VAPID key that has both a public_key and private_key attribute to be saved on the server side."

# One-time, on the server
vapid_key = Webpush.generate_key

# Save these in your application server settings
vapid_key.public_key
vapid_key.private_key

My doubt is: What exactly is "application server settings"? Where should I put these keys at my rails app?


Solution

  • Idealy it should be stored in environment variables (depends on the OS you use). If you are using dotenv gem and find it conveniant to use dotenv in production you can store it in .env file. To use a variable use ENV['NAME']

    Also for this purpose you can use default rails config/secrets.yml file. To use a variable use Rails.application.secrets.name.

    Also you can combine env variables with secrets.yml file like:

    secrets.yml

    ...
     key: ENV['NAME']
    

    benefit: use variable independent of rails environment.

    Notice: Neve share you credentials file to git or any public repo! If you need to share this file with other developers just send them copy with development keys.

    Links:

    dotenv

    environment variables

    secrets.yml