devise - Rails http post method returning a 401 unauthorized error -
so i'm developing simple web app, have devise set user authentication. child of user class property. both registration , login work user. however, when try create new property or edit property 401 error:
started post "/api/v1/properties" ::1 @ 2015-06-25 11:00:09 -0400 .... completed 401 unauthorized in 0ms (activerecord: 0.0ms)
it seems if current_user devise not being passed properties controller suspect association problem or token issue. appreciated.
propertiescontroller:
class api::v1::propertiescontroller < applicationcontroller skip_before_filter :verify_authenticity_token, :only => :create respond_to :json def index properties = property.all render json: properties end def show respond_with property.find(params[:id]) end def create property = current_user.property.build(product_params) if property.save render json: property, status: 201, location: [:api, property] else render json: { errors: property.errors }, status: 422 end end ...( code below ) end
property migration:
class createproperties < activerecord::migration def change enable_extension :postgis create_table :properties |t| t.string :name t.integer :user_id t.timestamps end add_index :properties, :user_id end
applicationcontroller:
class applicationcontroller < actioncontroller::base before_filter :authenticate_user_from_token! before_action :authenticate_user! private def authenticate_user_from_token! authenticate_with_http_token |token, options| user_email = options[:email].presence user = user_email && user.find_by_email(user_email) if user && devise.secure_compare(user.authentication_token, token) sign_in user, store: false end end end end
routes:
...(code above) devise_for :users, path: '/api/v1/users', controllers: { sessions: 'api/v1/sessions', :registration => "registrations" } "api/v1/users/sign_up" => "users/registrations#new", :as => :user_signup "api/v1/users/sign_in" => "api/v1/sessions", :as => :user_signin end namespace :api, defaults: { format: :json } namespace :v1 resources :properties ...(code below)
Comments
Post a Comment