使用失败应用

时间:2017-10-26 18:49:59

标签: ruby-on-rails authentication warden

当我使用失败应用程序时,我无法使用Warden(仅限Warden,没有设计)进行身份验证。

当我使用Warden配置为不使用失败应用程序时,不会发生此错误。

帮助我!

我的warden.rb config / initializers / warden.rb

Rails.application.config.middleware.use Warden::Manager do |manager|
  manager.default_strategies :password
  manager.failure_app = -> (env) { 
  SessionsController.action(:new).call(env) }
end

Warden::Manager.serialize_into_session do |user|
  user.id
end

Warden::Manager.serialize_from_session do |id|
  User.find(id)
end

Warden::Strategies.add(:password) do
  def authenticate!
    user = User.find_by(email: params['email'])

    if user && user.authenticate(params['password'])
      success! user
    else
      fail 'Invalid email or password'
    end
  end
end

我的 routes.rb

Rails.application.routes.draw do
  root 'home#index'

  get 'sessions/new'
  post 'sessions/create'
  get 'sessions/destroy'

  get 'login', to: 'sessions#new'
  post 'login', to: 'sessions#create'
  get 'logout', to: 'sessions#destroy'

  resources :users
end

我的 SessionsController

class SessionsController < ApplicationController
  def new
    flash.now[:alert] = warden.message if warden.message.present?
  end

  def create
    warden.authenticate!
    redirect_to root_path, notice: 'Logged in!'
  end

  def destroy
    warden.logout
    redirect_to root_path, notice: 'Logged out!'
  end
end

我的 ApplicationController

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

  private

  def current_user
    warden.user
  end

  def warden
    request.env['warden']
  end

  helper_method :current_user
end

错误屏幕:

enter image description here

我的回溯

enter image description here

1 个答案:

答案 0 :(得分:0)

这与您的CSRF真实性令牌无效有关 - 使用两次,可能在每个控制器中一次?

你可能有运气添加

skip_before_action :verify_authenticity_token, only: [:new]

SessionsController