Skip to content

Instantly share code, notes, and snippets.

@christhekeele
Last active August 29, 2015 13:56
Show Gist options
  • Save christhekeele/9215735 to your computer and use it in GitHub Desktop.
Save christhekeele/9215735 to your computer and use it in GitHub Desktop.
Rake task to reset your Rails Application's secret token.
namespace :secret do
desc "Replace application's secret_key_base in the secret_token initalizer with a new one"
task :reset do
# Rails default location
@secret_token_file ||= Rails.root.join('config', 'initializers', 'secret_token.rb')
# Find first string in file
@secret_token_finder ||= /(?<=config\.secret_key_base\s\=\s["'])(.*?)(?=["'])/
File.open(@secret_token_file, 'r+') do |file|
File.write file, file.read.gsub(@secret_token_finder, SecureRandom.hex(64))
end
end
# Overridable:
# namespace :reset do
# task :custom do
# @secret_token_file = Rails.root.join('config', 'secrets', 'app_token.rb')
# end
# end
# Rake::Task['secret:reset'].enhance ['secret:reset:custom']
# If you're in to that:
# task 'tmp:sessions:clear' => 'secret:reset'
# Or in a capistrano deploy script. Etc.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment