Rake aborted. Devise.secret_key was not set [duplicate] - ruby-on-rails

I am developing a Rails 4 app using the Active Admin gem for the administration back end. Active Admin in turn uses Devise for user authentication. Now, when I try to deploy the app using capistrano on the VPS server, I get the below error:
rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'
A Google search does not do much for this error. Any suggestions why it is throwing an error? Should I add the secret key to devise initializer, as I cannot find any place to set such config key in initializers/devise.rb?

I ran bundle update this morning and started getting the same error.
I added it as a line in config/initializers/devise.rb and the error was fixed.
This seems to be the commit which introduced it.

What worked for me on Rails 4.1 and Devise 3.2.4 is in config/initializers/devise.rb:
config.secret_key = ENV['DEVISE_SECRET_KEY'] if Rails.env.production?

As of Devise 3.2.3 for Rails 4+ applications the key setting location defaults to YourAppName::Application.config.secret_key_base found in config/initializers/secret_token.rb

This solved my problem:
Add the code below to your config/initializers/devise.rb file.
config.secret_key = '-- secret key --'
Replace '-- secret key--' with your own key. I recommend storing it in an ENV variable for security purpose.

As per changelog:
Devise will use the secret_key_base on Rails 4+ applications as its secret_key. You can change this and use your own secret by changing the devise.rb initializer.
I went to config/secrets.yml and changed the production value.
Before:
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
After:
production:
secret_key_base: string of charaters
Of course, that should be set to the environment variable, which I will set later, but this at least got it running. I got my string by using bundle exec rake secret.

Could it be, that you did not run rails g devise:install?
Running rails generate devise User without the previous command does cause this problem.

In config/initializers/devise.rb I put:
config.secret_key = ENV["SECRET_KEY_BASE"] if Rails.env.production?
Because if you put:
$ heroku config
You'll see a secret_key_base for the mode production.

I solve my initializer problem with this ugly approach:
config.secret_key = 'some1234keyq23' if Rails.env == 'production'
in config/initializers/devise.rb
It now works in production as well as in development !

I cloned my repository onto a new machine from git. The
config/secrets.yml
file was on my .gitignore list, so that file didn't exist, and Devise doesn't create the file.
I added the file, then re-ran
rails generate devise MODEL
and it worked.

Check if your config\initializers\secret_token.rb has:
YourAppName::Application.config.secret_token
It should be:
YourAppName::Application.config.secret_key_base

I has same issue. The problem was caused by these lines in routes.rb:
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration'
get '/users/sign_out' => 'devise/sessions#destroy'
end
I commented them and after that i run:
$ rails generate devise:install
And it has evaluated perfectly. And after that I uncommented routes.

Well, I have been following this post and tried almost everything here.
I have added the key to devise.rb. But I was still getting the same error.
Maybe a stupid answer, but all I had to do was to push the devise.rb key to the repository.

Fix:
In the production server:
sudo -H nano /etc/environment
Then in the file add:
export SECRET_KEY_BASE="yourkey"
export DEMO03_DATABASE_PASSWORD="yourpass"
to set this permanently, and system wide (all users, all processes) add set variable
In the local project devise.rb file:
config.secret_key = ENV["SECRET_KEY_BASE"] if Rails.env.production?
Technical details:
Ubuntu 16.04
Devise (4.2.0)
rails 5.0.1
capistrano (3.7.1)

Ran into the same trouble with Rails 5.2.0 and Devise 4.4.1
Drop the following into /config/initializers/devise.rb
config.secret_key = Rails.application.credentials.secret_key_base

Trying to give a somewhat more complete answer to the ones above:
As mentioned in the devise_auth_token gem's documentation
...Additionally, you can configure other aspects of devise by manually
creating the traditional devise.rb file at
config/initializers/devise.rb. Here are some examples of what you can
do in this file:
Devise.setup do |config|
# The e-mail address that mail will appear to be sent from
# If absent, mail is sent from "please-change-me-at-config-initializers-devise#example.com"
config.mailer_sender = "support#myapp.com"
# If using rails-api, you may want to tell devise to not use ActionDispatch::Flash
# middleware b/c rails-api does not include it.
# See: http://stackoverflow.com/q/19600905/806956
config.navigational_formats = [:json] end
I had the same problem, and like metioned here, I created the devise initializer, and add the config.secret_key = ENV['DEVISE_SECRET_KEY'] line to it.

I do not know right solution but it's working. You can try it. I was cloned my project from my GitLab account and when I run in my local server, I have an error Message:
rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'
Open config/initializers/devise.rb and add this line
config.secret_key = '<%= ENV["SECRET_KEY_BASE"] %>'
This code line is solved my problem.

Related

Using OmniAuth to Establish A Facebook Authentication

I am trying to provide Sign Up with either an email and password or using Facebook.
I have tried using https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview
I followed each of the steps, but I run into the following error in Terminal when I try to run rails server:
'users/omniauth.callbacks' is not a supported controller name
I thought this was referring to the file I was instructed to make (app/controllers/users/omniauth_callback_controllers.rb) but deleting the file does nothing. I have Devise implemented, and I also installed the Omniauth gem. What am I doing incorrectly?
Steps I took:
Added the gem omniauth-facebook to my Gemfile
Ran bundle install in Terminal
Ran rails g migration AddColumnsToUsers provider uid
Ran rake db:migrate
Added the line config.omniauth :facebook, "APP_ID", "APP_SECRET" to the file config/initializers/devise.rb
Added link to Sign Up on sign up page using the line <%= link_to "Sign In With Facebooks", user_omniauth_authorize_path(:facebook %>
Added the line devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks} to the config/routes.rb file
(The part I was confused about) Added a new folder, users, and a new file, omniauth_callbacks_controller.rb (file path is app/controllers/users/omniauth_callbacks_controller.rb) and included code (see Imgur link)
Added code to the app/models/user.rb file (see Imgur link)
http://imgur.com/a/TyVoK

RAILS : undefined method `secret_key=' for Devise:Module

I defined a model in rails using command rails generate model testdetails . After that I went to db/migrate and wrote the fields for this model
class CreateTestDetails < ActiveRecord::Migration
def self.up
create_table :test_details do |t|
t.column :TestName ,:string
t.column :TestType ,:integer
end
end
end
then i did db:migrate , it throws some error . I google it and found out that it may be coming due to devise version ( I am using devise for authentication) , I updated Gemfile and wrote the version of devise (2.1) and did bundle install . After that I again did db : migrate but it is showing this error
rake aborted!
undefined method secret_key=' for Devise:Module
/home/vibhor/rails_projects/recruit/config/initializers/devise.rb:7:inblock in '
/home/vibhor/rails_projects/recruit/config/initializers/devise.rb:3:in <top (required)>'
/home/vibhor/rails_projects/recruit/config/environment.rb:5:in'
Tasks: TOP => db:migrate => environment
what should i do so that this model can be created without any error? i am using rails 3.2.13 and ruby 2.0.0
I think this is due to gem version so update it to latest version 3.x or remove that line from your config/initializers/devise.rb file.
In your config/initializers/devise.rb file add this line:
config.secret_key = 'Your secret Key'
And use rake secret to generate your secret key.
There's an issue open in github if you need more info.
Had the same problems lately. Check devise initializers, as it has changed lately. For me it solved the problem.

Devise Secret Key was not set

I am developing a Rails 4 app using the Active Admin gem for the administration back end. Active Admin in turn uses Devise for user authentication. Now, when I try to deploy the app using capistrano on the VPS server, I get the below error:
rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'
A Google search does not do much for this error. Any suggestions why it is throwing an error? Should I add the secret key to devise initializer, as I cannot find any place to set such config key in initializers/devise.rb?
I ran bundle update this morning and started getting the same error.
I added it as a line in config/initializers/devise.rb and the error was fixed.
This seems to be the commit which introduced it.
What worked for me on Rails 4.1 and Devise 3.2.4 is in config/initializers/devise.rb:
config.secret_key = ENV['DEVISE_SECRET_KEY'] if Rails.env.production?
As of Devise 3.2.3 for Rails 4+ applications the key setting location defaults to YourAppName::Application.config.secret_key_base found in config/initializers/secret_token.rb
This solved my problem:
Add the code below to your config/initializers/devise.rb file.
config.secret_key = '-- secret key --'
Replace '-- secret key--' with your own key. I recommend storing it in an ENV variable for security purpose.
As per changelog:
Devise will use the secret_key_base on Rails 4+ applications as its secret_key. You can change this and use your own secret by changing the devise.rb initializer.
I went to config/secrets.yml and changed the production value.
Before:
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
After:
production:
secret_key_base: string of charaters
Of course, that should be set to the environment variable, which I will set later, but this at least got it running. I got my string by using bundle exec rake secret.
Could it be, that you did not run rails g devise:install?
Running rails generate devise User without the previous command does cause this problem.
In config/initializers/devise.rb I put:
config.secret_key = ENV["SECRET_KEY_BASE"] if Rails.env.production?
Because if you put:
$ heroku config
You'll see a secret_key_base for the mode production.
I solve my initializer problem with this ugly approach:
config.secret_key = 'some1234keyq23' if Rails.env == 'production'
in config/initializers/devise.rb
It now works in production as well as in development !
I cloned my repository onto a new machine from git. The
config/secrets.yml
file was on my .gitignore list, so that file didn't exist, and Devise doesn't create the file.
I added the file, then re-ran
rails generate devise MODEL
and it worked.
Check if your config\initializers\secret_token.rb has:
YourAppName::Application.config.secret_token
It should be:
YourAppName::Application.config.secret_key_base
I has same issue. The problem was caused by these lines in routes.rb:
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration'
get '/users/sign_out' => 'devise/sessions#destroy'
end
I commented them and after that i run:
$ rails generate devise:install
And it has evaluated perfectly. And after that I uncommented routes.
Well, I have been following this post and tried almost everything here.
I have added the key to devise.rb. But I was still getting the same error.
Maybe a stupid answer, but all I had to do was to push the devise.rb key to the repository.
Fix:
In the production server:
sudo -H nano /etc/environment
Then in the file add:
export SECRET_KEY_BASE="yourkey"
export DEMO03_DATABASE_PASSWORD="yourpass"
to set this permanently, and system wide (all users, all processes) add set variable
In the local project devise.rb file:
config.secret_key = ENV["SECRET_KEY_BASE"] if Rails.env.production?
Technical details:
Ubuntu 16.04
Devise (4.2.0)
rails 5.0.1
capistrano (3.7.1)
Ran into the same trouble with Rails 5.2.0 and Devise 4.4.1
Drop the following into /config/initializers/devise.rb
config.secret_key = Rails.application.credentials.secret_key_base
Trying to give a somewhat more complete answer to the ones above:
As mentioned in the devise_auth_token gem's documentation
...Additionally, you can configure other aspects of devise by manually
creating the traditional devise.rb file at
config/initializers/devise.rb. Here are some examples of what you can
do in this file:
Devise.setup do |config|
# The e-mail address that mail will appear to be sent from
# If absent, mail is sent from "please-change-me-at-config-initializers-devise#example.com"
config.mailer_sender = "support#myapp.com"
# If using rails-api, you may want to tell devise to not use ActionDispatch::Flash
# middleware b/c rails-api does not include it.
# See: http://stackoverflow.com/q/19600905/806956
config.navigational_formats = [:json] end
I had the same problem, and like metioned here, I created the devise initializer, and add the config.secret_key = ENV['DEVISE_SECRET_KEY'] line to it.
I do not know right solution but it's working. You can try it. I was cloned my project from my GitLab account and when I run in my local server, I have an error Message:
rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'
Open config/initializers/devise.rb and add this line
config.secret_key = '<%= ENV["SECRET_KEY_BASE"] %>'
This code line is solved my problem.

Devise confirmation link is missing domain

Devise confirmation_url is producing just a relative url with no domain such as:
http://users/confirmation?confirmation_token=...
I tried changing
confirmation_url(#resources, :confirmation_token => #resource.confirmation_token)
to
confirmation_url(:confirmation_token => #resource.confirmation_token)
but it produces the same url. I upgraded to devise 2.2.3 but same outcome. Rails 3.1.4
Update:
I have set in my production.rb:
config.action_mailer.default_url_options = { :host => 'mysubdomain.mysite.com' }
and I tried setting
before_filter set_actionmailer_host
def set_actionmailer_host
ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
to no avail in application controller (https://github.com/plataformatec/devise/issues/1567)
Update:
This occurs in both development and production.
Update
I can't understand why Devise isn't using the template in app/views/devise/mailer/confirmation_instructions.html.haml If I could edit that I could append the host manually: 'http://mysite.com/' + confirmation_url(...
I tried setting the scoped views setting but it didn't have any effect
This is a disaster, users can't confirm their registration :(
You might have missed to add the default url hosts to your environment config-files. When you run the rails g devise:install command you usually receive those instructions.
# config/environments/development.rb
# Default actiomailer url host (required by devise)
config.action_mailer.default_url_options = { host: 'myapp.dev' }
You need this setting for all your environments.
# config/environments/production.rb
# Default actiomailer url host (required by devise)
config.action_mailer.default_url_options = { host: 'myproductionapp.com' }

Rails - can't make database migration on Heroku for app using Devise authentication

My application uses Devise authentication gem.
When i do
rake db:migrate
locally, everything is going well, but when I do this on Heroku:
heroku run rake db:migrate --app myappname
I get
rake aborted!
uninitialized constant Devise::Encryptors::Base
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
I have no Idea what could go wrong.
I run into the same issue because I implemented a custom encryptor. Since version 2.1 of devise custom encryptors have been extracted to a separate gem. To get it working do the following.
Add the devise-encryptable gem to your Gemfile.
gem 'devise-encryptable'
Subclass from Devise::Encryptable::Encryptors::Base instead of Devise::Encryptors::Base.
# lib/devise/encryptors/md5.rb
require 'digest/md5'
module Devise
module Encryptable
module Encryptors
class Md5 < Base
def self.digest(password, stretches, salt, pepper)
str = [password, salt].flatten.compact.join
Digest::MD5.hexdigest(str)
end
end
end
end
end
I updated the how-to page of devise as well. I hope this solves your problem.

Resources