2017-04-05 20 views
1

では動作しません、私はhttps://myapp.com/users/sign_inの「サインイン」ボタンを押した後、次のエラーが表示されます。工夫は、HTTPS

ActionController::InvalidAuthenticityToken in Devise::SessionsController#create 

を関係なく、既存のユーザーの資格情報の。

http://myapp.com/users/sign_inにログインするためにボタンを押した場合、ユーザーはサインインしてアプリケーションがスムーズに動作しているように見えますが、ユーザーはHTTPでも投稿を作成することができます。

私はSSLでの認証作業をしたいと思っています。

routes.rbを:

devise_for :users 

application_controller.rb:

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 
end 

user.rb:

class User < ApplicationRecord 
    devise :database_authenticatable, :recoverable, :rememberable, :registerable, :trackable, :validatable 
end 
  1. 私は頭の中で<%= csrf_meta_tags %>を持って
  2. は、私は私は私がルートにdevise_for :users, :controllers => { :sessions => "users/sessions" }を試してみました
  3. 新しいセッションのためにform_for<%= hidden_field_tag :authenticity_token, form_authenticity_token %>を入れてみました
  4. アプリ/コントローラ/ application_controller.rbにprotect_from_forgery with: :null_session代わりのprotect_from_forgery with: :exceptionを試してみました
  5. のブラウザでCookieをクリア
  6. 私は、設定/環境/ production.rb

  7. config.to_prepare { Devise::SessionsController.force_ssl } 
    config.to_prepare { Devise::RegistrationsController.force_ssl } 
    config.to_prepare { Devise::PasswordsController.force_ssl } 
    

    を追加しようとしました

+0

これはあなたの問題http://stackoverflow.com/questions/38331496/rails-5-actioncontrollerinvalidauthenticitytoken-error – bunty

+0

@buntyを解決しますであってもよいし、まあ、それはしませんでした –

+0

this https://github.com/plataformatec/devise/wiki/How-To:-User-SSL-(HTTPS) – bunty

答えて

0

はsessions_controller

skip_before_action :verify_authenticity_token 

にし、1本のラインを入れてroutes.rbを

devise_for :users, controllers: 
        { 
        sessions: 'users/sessions', 
        } 

、そのないためにCSRFの保護を無効にする回避策のユーザーログイン

+1

Mayur、ここではsessions_controllerですか? –

+0

controllers/users/sessionsフォルダにsessions_controller.rbを作成しようとします –

+0

エラー:初期化されていない定数Users :: SessionsController これ以上の説明は必要ありません。 –

0

があるときauthentication_token必要アプリケーションコントローラのsign_inアクション

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 
    .... 
end 

設定し、それはよう:

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 
    skip_before_filter :verify_authenticity_token, if: -> { controller_name == 'sessions' && action_name == 'create' } 
    ... 
end