Search code examples
ruby-on-rails-4nginxdevisethincapistrano3

rails4 deployment logs out all users


got some strange behavior since quite a while. After deployment (with capistarano) all users need to sign in again!

happens in several apps, since Rails 4.x .. here some versions:

  • Rails: 4.2.5 / 4.2.6
  • Devise: 3.5.3 / 3.5.6 / 4.2.0
  • Capistrano: 3.4.0
  • Ruby: 2.2.1 / 2.3.0
  • Web-Server: nginx
  • App-Server: thin (1.6.4 / 1.7.0)

this is really annoying, especially because zero-downtime-deployment makes no sence anymore and remember-me doesn't work at all

models/user

class User < ActiveRecord::Base
  ..
  devise  :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, 
          :validatable, :confirmable, :lockable, :timeoutable, :omniauthable, :invitable, 
          :omniauth_providers => CONFIG[:devise_provider]
  ..
end

initializers/devise

Devise.setup do |config|

  config.secret_key = 'xxx-key-xxx'

  config.mailer_sender = CONFIG[:mail_system]

  config.mailer = 'AccountMailer'

  require 'devise/orm/active_record'

  config.case_insensitive_keys = [:email]

  config.strip_whitespace_keys = [:email]

  config.skip_session_storage = [:http_auth]

  config.stretches = Rails.env.test? ? 1 : 10

  config.invite_for = 0

  config.reconfirmable = true

  config.expire_all_remember_me_on_sign_out = true

  config.password_length = CONFIG[:password_min_length]..CONFIG[:password_max_length]

  config.timeout_in = 6.hours

  config.reset_password_within = 6.hours

  config.sign_out_via = :delete

  config.omniauth :facebook,      CONFIG[:facebook_id], CONFIG[:facebook_secret], {info_fields: 'email, first_name, last_name, gender', image_size: "large"}
  config.omniauth :google_oauth2, CONFIG[:google_id],   CONFIG[:google_secret], {
    skip_jwt: true,
    scope: "email, profile, plus.me",
    # prompt: "select_account",
    image_aspect_ratio: "square",
    image_size: 200
  }

end

Most of the apps are running in production, so I don't touch the database while deployment (except for migrations). Also happens in apps without devise_invitable, so this also don't causes it.

.. thanks for any help! ..

also posted as devise #4277


Solution

  • solved it!

    The problem was caused by the only thing I didn't mention: rvmor better rvm1-capistrano3which completely ignores ~/.bachrc, ~/.profile and so on.

    I had to put secret_key_base in /etc/environment now it works as expected.

    Till now secret_key_base was just a huge pain in the ass for me, because all documented usage didn't work and I had to put secret_key_base in :default_env and also inject it in some monit scripts. ie: to restart sidekiq or thin (therefore it was hardcoded in deploy.rb)

    Thanks to surendar, his answer on capistrano 3 + rvm1-capistrano3 rails 4.1 secrets.yml environmental variables issue was the solution.

    It still don't seems to be the best solution (but it works), so I'll stay open for better solutions.