简体   繁体   English

Ember / Rails CORS Simple-Auth 405(不允许)

[英]Ember/Rails CORS Simple-Auth 405 (Not Allowed)

I have an ember-cli app with a ActiveModelAdapter to a Rails API using rack-cors . 我有一个带有ActiveModelAdapter的ember-cli应用程序,使用rack-cors到Rails API。 I have configured both to use ember-cli-simple-auth-devise . 我已将两者配置为使用ember-cli-simple-auth-devise

Locally in development everything works perfectly. 在本地开发中,一切都很完美。 But once I deploy the ember-cli app to Heroku I am unable to authenticate my signin, yet am able to retrieve other records. 但是一旦我将ember-cli应用程序部署到Heroku,我无法验证我的登录,但能够检索其他记录。 I receive the following 405 Error: 我收到以下405错误:

POST http://example.herokuapp.com/businesses/sign_in 405 (Not Allowed) POST http://example.herokuapp.com/businesses/sign_in 405(不允许)

Maybe it has to do with the fact that I am using Business instead of User for my Devise model, but I change User to Business in the application_controller (plus it wouldn't work locally otherwise): 也许这与我使用Business而不是User for Devise模型这一事实有关,但是我在application_controller中将User更改为Business(另外它不会在本地工作):

## /backend/app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
 before_filter :authenticate_user_from_token!

 private

 def authenticate_user_from_token!
   authenticate_with_http_token do |token, options|
     user_email = options[:user_email].presence
     user       = user_email && Business.find_by_email(user_email) 
     ## /\ Changed User to Business /\

     if user && Devise.secure_compare(user.authentication_token, token)
       sign_in user, store: false
     end
   end
 end
end

Rack-cors config: Rack-cors配置:

## /backend/config.ru

require ::File.expand_path('../config/environment',  __FILE__)
run Rails.application

require 'rack/cors'
use Rack::Cors do

  # allow all origins in development
  allow do
    origins '*'
    resource '*', 
        :headers => :any, 
        :methods => [:get, :post, :delete, :put, :options]
  end
end

I have configured simple-auth-devise like so: 我已经配置了simple-auth-devise,如下所示:

// frontend/config/environment.js

ENV['simple-auth-devise'] = {
    serverTokenEndpoint: 'businesses/sign_in',
    resourceName: 'business',
    crossOriginWhitelist: ['http://example-backend.herokuapp.com/']
};

Any insight into this would be greatly appreciated. 任何对此的见解将不胜感激。

Thanks! 谢谢!

** Update ** I have narrowed it down to the fact that it is a POST to example.herokuapp.com instead of to my rails backend URL of example-backend.herokuapp.com. ** 更新 **我已将其缩小到这样一个事实,即它是example.herokuapp.com的POST,而不是我的rails-rearend.herokuapp.com的后端URL。 So I think this has to do with ember-cli-simple-auth not using the proxy I have set with heroku, as the store is doing. 所以我认为这与ember-cli-simple-auth有关,不使用我用heroku设置的代理,就像商店正在做的那样。

You need to configure the serverTokenEndpoint including the host when the host is not the one that the Ember app is served from: 当主机不是服务Ember应用程序的主机时,您需要配置serverTokenEndpoint包括主机:

ENV['simple-auth-devise'] = {
  serverTokenEndpoint:  'http://example-backend.herokuapp.com/businesses/sign_in',
  resourceName:         'business',
  crossOriginWhitelist: ['http://example-backend.herokuapp.com/']
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM