Extending Devise SessionsController to authenticate using JSON

后端 未结 7 1326
面向向阳花
面向向阳花 2020-12-04 05:14

I am trying to build a rails API for an iphone app. Devise works fine for logins through the web interface but I need to be able to create and destroy sessions using REST AP

7条回答
  •  半阙折子戏
    2020-12-04 05:59

    From the rdoc for devise's #devise_scope:

    Sets the devise scope to be used in the controller. If you have custom routes, you are required to call this method (also aliased as :as) in order to specify to which controller it is targetted.

    as :user do
      get "sign_in", :to => "devise/sessions#new"
    end
    

    Notice you cannot have two scopes mapping to the same URL. And remember, if you try to access a devise controller without specifying a scope, it will raise ActionNotFound error.

    It looks like you need to wrap it in a #as block:

    as :user do
      namespace :api do
        namespace :v1 do
          resources :sessions, :only => [:create, :destroy]
        end
      end
    end
    

提交回复
热议问题