5

Tôi đang xây dựng một Rails-API sử dụng Omniauth-facebook và Devise-token-auth với Angular và ng-token-auth cho giao diện người dùng. Tuy nhiên khi đăng nhập với facebook Tôi trình bày với các lỗi:biến không xác định cục bộ hoặc phương thức `flash 'cho # <Devise :: OmniauthCallbacksController: 0x007fb5d1741e48>

undefined local variable or method `flash' for #<Devise::OmniauthCallbacksController:0x007fd027a51e10> 

Dường omniauth tự động sử dụng đèn flash middleware tuy nhiên các đường ray-api không bao gồm này và tôi đã không thành công vô hiệu hóa việc sử dụng đèn flash với omniauth . cấu hình của tôi là như sau:

application.rb:

require File.expand_path('../boot', __FILE__) 

require "rails" 
# Pick the frameworks you want: 
require "active_model/railtie" 
require "active_job/railtie" 
require "active_record/railtie" 
require "action_controller/railtie" 
require "action_mailer/railtie" 
require "action_view/railtie" 
require "sprockets/railtie" 
# require "rails/test_unit/railtie" 

# Require the gems listed in Gemfile, including any gems 
# you've limited to :test, :development, or :production. 
Bundler.require(*Rails.groups) 

module PathfinderApi 
    class Application < Rails::Application 
    config.active_record.raise_in_transactional_callbacks = true 


    config.middleware.insert_before 0, "Rack::Cors" do 
     allow do 
     origins '*' 
     resource '*', :headers => :any, :methods => [:get, :post, :options] 
     end 
    end 

    config.api_only = true 
    config.middleware.use ActionDispatch::Flash 
    config.middleware.use ActionDispatch::Cookies 
    config.middleware.use ActionDispatch::Session::CookieStore 
    end 
end 

devise_token_auth.rb:

DeviseTokenAuth.setup do |config| 
    Rails.application.secrets.facebook_app_secret 
    config.change_headers_on_each_request = true 
end 

devise.rb:

Devise.setup do |config| 
    config.navigational_formats = [:json] 
end 

omniauth.rb:

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook,  ENV['APP_KEY'], ENV['APP_SECRET'] 
end 

tôi đã không quản lý để vô hiệu hóa các lỗi đèn flash với:

config.navigational_formats = [:json] 

và đưa ra/omniauth vẫn sử dụng đèn flash middleware và ném lỗi, bất kỳ sự giúp đỡ đánh giá cao!

Trả lời

3

Đã xảy ra sự cố tương tự. Đã tìm kiếm mã nguồn gốc cho 'flash'. Tìm thấy khoảng 17 kết quả phù hợp, tất cả sử dụng set_flash_message! (có dấu chấm than), ngoại trừ phương pháp failure trong OmniauthCallbacksController, sử dụng set_flash_message (không có dấu chấm than). Nhìn vào định nghĩa chúng ta thấy:

\ ứng dụng \ controllers \ devise \ omniauth_callbacks_controller.rb

# Sets flash message if is_flashing_format? equals true 
def set_flash_message!(key, kind, options = {}) 
    if is_flashing_format? 
    set_flash_message(key, kind, options) 
    end 
end 

\ lib \ devise \ controllers \ helpers.rb

def is_flashing_format? 
    is_navigational_format? 
end 

def is_navigational_format? 
    Devise.navigational_formats.include?(request_format) 
end 

Các thông điệp flash thực tế được tạo ra trong phương thức mà không có dấu chấm than (tôi đã đề xuất nó theo cách khác xung quanh ...). Dấu chấm than còn thiếu là lý do tại sao đặt navigational_formats như được đề cập trong các giải pháp khác không hoạt động ở đây.

Kết luận: họ quên dấu ấn.

Sửa lỗi: khỉ vá failure phương thức từ OmniauthCallbacksController. Làm điều này trong một initializer, ví dụ như trong

\ config \ initializers \ devise.rb

Rails.application.config.to_prepare do    # to_prepare ensures that the monkey patching happens before the first request 
    Devise::OmniauthCallbacksController.class_eval do # reopen the class 
    def failure          # redefine the failure method 
     set_flash_message! :alert, :failure, kind: OmniAuth::Utils.camelize(failed_strategy.name), reason: failure_message 
     redirect_to after_omniauth_failure_path_for(resource_name) 
    end 
    end 
end 
0

Đã cùng một vấn đề trong Rails (5.0.0.1) + devise_token_auth (0.1.39).

Bên cạnh việc ghi đè trong @ Koen của câu trả lời, việc bổ sung sau đây cũng là cần thiết trong trường hợp của tôi:

# in config/application.rb 
    config.middleware.use ActionDispatch::Cookies 
+0

Chắc chắn đúng, đã cho tôi một thời gian để tìm ra rằng quá! Thnx cho phụ lục. –

+0

Bạn đang thiếu một số phần. Đây là cấu hình lưu trữ phiên với Omniauth. Những gì bạn thực sự muốn là làm theo hướng dẫn chính thức của họ: https://github.com/omniauth/omniauth#integrating-omniauth-into-your-rails-api – sarink

Các vấn đề liên quan