2
我有一個反應前端和一個rails 5 api只後端,我試圖讓simple_token_authentication gem工作。我在嘗試登錄時遇到此錯誤:Rails 5 Api Only - Devise - react - simple_token_authentication
Started POST "https://stackoverflow.com/users/sign_in.json" for 100.38.166.199 at 2017-05-05 02:31:24 +0000
Processing by Users::SessionsController#create as JSON
Parameters: {"email"=>"[email protected]", "password"=>"[FILTERED]", "session"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}}
Completed 401 Unauthorized in 2ms (ActiveRecord: 0.0ms)
我懷疑它與CSRF有關。我有時會在CSRF真實性錯誤消息,但奇怪的是並不總是
這裏是我的路線:
Rails.application.routes.draw do
devise_for :users, controllers: {
sessions: 'users/sessions',
registrations: 'users/registrations'
}
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
scope module: 'api' do
namespace :v1 do
resources :properties
end
end
end
我的應用程序控制器(我有,因爲我也使用ActiveAdmin)
class ApplicationController < ActionController::Base
respond_to :json
acts_as_token_authentication_handler_for User, fallback_to_devise: false
end
這是發佈請求的樣子在客戶端
export function signinUser({ email, password }) {
return function(dispatch){
//submit email password to the server
axios.post(`${ROOT_URL}/users/sign_in.json`, { email, password })
.then(response => {
//if request is good, update state to indicate user is authenticated
dispatch({ type: AUTH_USER});
//-save the jwt token
localStorage.setItem('authentication_token', response.data.token);
//-redirect route somewhere
browserHistory.push('/test');
})
.catch(error => {
//if request is bad, show an error to the user
dispatch(authError(error));
})
};
}