devise icon indicating copy to clipboard operation
devise copied to clipboard

Devise.secret_key was not set

Open brahmadpk opened this issue 13 years ago • 37 comments

Hi,

I am using devise in my rails 4 app. In my gem file like so:

gem 'devise', github: 'plataformatec/devise'

When I run

rails generate devise:install

I get the below errors:

Devise.secret_key was not set. Please add the following to your Devise initializer: (RuntimeError)

config.secret_key = '-- secret key --'

However I can't see any devise.rb file in the initializers folder.

How to solve this?

brahmadpk avatar Aug 06 '13 14:08 brahmadpk

As the message says:

Please add the following to your Devise initializer:

config.secret_key = '-- secret key --'

Also, I strongly recommend to not use Devise master for now. It is highly unstable.

josevalim avatar Aug 06 '13 14:08 josevalim

@josevalim Shouldn't rails generate devise:install create the devise initializer?

vwall avatar Aug 17 '13 23:08 vwall

It does, we generate for new applications. If you are upgrading, you need to add it.

josevalim avatar Aug 18 '13 06:08 josevalim

@josevalim It gives that error with a new app also.

vwall avatar Aug 18 '13 07:08 vwall

You are right, thanks. the issue has been fixed on master.

josevalim avatar Aug 18 '13 08:08 josevalim

I am having the same issue. Was this ever resolved?

marfo avatar Sep 07 '13 02:09 marfo

Sample problem here - and when trying to fix it by adding "config.secret_key = '{secretkey}' I get error "undefined method 'secret_key='/ Any idea what I've got wrong?

lpnwpddler avatar Sep 07 '13 19:09 lpnwpddler

I figured it out. I went to my routes.rb file and commented out the line devise_for :installs

Then I went back and reran rails generate devise:install. If that doesn't work, use the previous version of devise by editing your Gemfile's reference to Devise like this: gem 'devise', '3.0.3' and then follow the steps i mentioned above.

hope it helps!

marfo avatar Sep 07 '13 22:09 marfo

Same for me. I tried the above, it fails. Added devise.rb to /initializers, config.secret_key = 'safadfstgferster5tg, etc' Now error on restarting the server : Rails_projects/My_App/config/initializers/devise.rb:3:in <top (required)>': undefined local variable or methodconfig' for main:Object (NameError) gem 'devise', '3.0.3' did not fix the problem Now I cannot drop the table _devise_create_install.rb. :SQLException: no such table. Very puzzling!

arelem3 avatar Sep 08 '13 05:09 arelem3

@arelem3 what do you mean you added devise.rb to initializers? If you don't have this file in your initializers lib you should run Devise generator that will generate it for you: rails g devise:install

nashby avatar Sep 08 '13 11:09 nashby

@marfo thanks, worked for me

Crystark avatar Sep 09 '13 13:09 Crystark

@Dorian Rails 4.1 will come with a better way to handle tokens, we will migrate to such approach once Rails 4.1 is out.

josevalim avatar Sep 09 '13 19:09 josevalim

On 09/08/2013 09:50 PM, Vasiliy Ermolovich wrote:

@arelem3 https://github.com/arelem3 what do you mean you added |devise.rb| to initializers? If you don't have this file in your initializers lib you should run Devise generator that will generate it for you: |rails g devise:install|

— Reply to this email directly or view it on GitHub https://github.com/plataformatec/devise/issues/2554#issuecomment-24019574.

Thanks Vasiliy Ok here's where I'm at! I installed devise and cancan gems in my home version of the live app. Following instructions for devise I found that the devise:install did not generate the devise.rb file so I created it, also it would not create the user.. it was then that I asked the above question. The app had so many errors I deleted it and git cloned a fresh version. This time devise:install created all the files. Looking thru the logs I think I did devise :install in the first instance and not devise:install. Trap for the unwary.

3 immediate problems. First, devise created a /registrations folder in /devise, my app already has a full working registrations system. Second devise creates users. I already had a user system set up from following Agile 4 and Ryan Bate's tuts, this is useless for devise. Third, I have a sessions controller from the above tuts and devise sets up sessions, conflict again.

Deleted the app and cloned again. This time devise did it's thing and 'rails g devise member' worked. All good but now devise is looking for registrations on sign_up -- http://localhost:3000/registrations/member produces error: No route matches [POST] "/registrations/member I think it is looking in my /registrations and not devise/registration.

Unless there is some way of setting things in routes, I'm thinking I'll have to rewrite the app from scratch after installing devise and cancan and not use those conflicting names. Funny, All I wanted to do was stop everyone except the owners from viewing show, index, new.

I have no idea how to resolve this Roger

arelem3 avatar Sep 10 '13 01:09 arelem3

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 !

yasinishyn avatar Sep 13 '13 17:09 yasinishyn

@yasinishyn I'm getting the same error. I don't understand what secret key it is I'm supposed to put in though? Where do I find this key?

holgersindbaek avatar Sep 14 '13 13:09 holgersindbaek

@holgersindbaek put in config/initializers/devise.rb:

config.secret_key = 'd84134f28f412ca83539652b09f9f98eff96c9aa7c6c792d959416d5aa368a409362ec6e2ac90af0be62395ae3925f760d7f956fe4efba4eb11f0696956a8f5f'

Use rake secret to generate one.

JuanitoFatas avatar Sep 14 '13 13:09 JuanitoFatas

@JuanitoFatas That is some sort of secret key, but what purpose does it have, since it's not really secret?

holgersindbaek avatar Sep 14 '13 13:09 holgersindbaek

In previous versions, Devise stored the tokens for confirmation, reset password and unlock directly in the database. This meant that somebody with read access to the database could use such tokens to sign in as someone else by, for example, resetting their password.

In Devise 3.1, we store an encrypted token in the database and the actual token is sent only via e-mail to the user. This means that:

Devise now requires a config.secret_key configuration. As soon as you boot your application under Devise 3.1, you will get an error with information about how to proceed;

Read full article here.

You can use SettingsLogic to put this secret_key in config/application.yml and add config/application.yml to .gitignore. Remember to create a config/application.example.yml.

I hope this helps.

JuanitoFatas avatar Sep 14 '13 13:09 JuanitoFatas

I've tried all of the above, none of them work if you are using Rails 4, Devise 3.0.3 is there any other way to solve this problem. I am somewhat new to all of this but this hangup has curbed development for three straight days - would love a known solution.

@JuanitoFatas Can you go into more detail about SettingsLogic? Did it work? I can't tell if you are offering a known solution or something you think would fix the problem. Thanks!

ChrisDaltas avatar Sep 20 '13 05:09 ChrisDaltas

@ChrisDaltas Hi. I use SettingsLogic and devise 3.1 in one of my projects and it works.

JuanitoFatas avatar Sep 20 '13 06:09 JuanitoFatas