Note that there are some explanatory texts on larger screens.

plurals
  1. PORails api gem and devise token authentication
    primarykey
    data
    text
    <p>If someone could shed some light It would be greatly appreciated as I'm bit clueless on this issue. Basically I'm trying to get the devise authentication done with a JSON only rails rest api app built on top of <a href="https://github.com/rails-api/rails-api">rails-api</a> gem.</p> <p>I've already implemented the <code>Sessions</code> and <code>Registrations</code> handling as described below. </p> <pre><code>class ApplicationController &lt; ActionController::API include ActionController::MimeResponds end </code></pre> <h3>sessions_controller.rb</h3> <pre><code> class SessionsController &lt; Devise::SessionsController prepend_before_filter :require_no_authentication, :only =&gt; [:create ] before_filter :ensure_params_exist def create build_resource resource = User.find_for_database_authentication(:email =&gt; params[:user][:email]) return invalid_login_attempt unless resource if resource.valid_password?(params[:user][:password]) sign_in("user", resource) render :json=&gt; {:success=&gt;true, :auth_token=&gt;resource.authentication_token, :email=&gt;resource.email} return end invalid_login_attempt end def destroy sign_out(resource_name) end protected def ensure_params_exist return unless params[:user][:email].blank? render :json=&gt;{:success=&gt;false, :message=&gt;"missing login email parameter"}, :status=&gt;422 end def invalid_login_attempt warden.custom_failure! render :json=&gt; {:success=&gt;false, :message=&gt;"Error with your login or password"}, :status=&gt;401 end end </code></pre> <h3>registrations_controller.rb</h3> <pre><code> class RegistrationsController &lt; Devise::RegistrationsController skip_before_filter :verify_authenticity_token, :only =&gt; :create def create user = User.new(params[:user]) if user.save render :json=&gt; {:user =&gt; [:email =&gt; user.email, :auth_token =&gt; user.authentication_token]}, :status =&gt; 201 return else warden.custom_failure! render :json=&gt; user.errors, :status=&gt;422 end end end </code></pre> <p>Everything works fine so far. In my other controllers I've added this line to secure them. <code>before_filter :authenticate_user!</code> but I get this error when calling with auth_token. ?auth_token=xxxxxxxxxxx</p> <p><code>undefined method authenticate_user!</code></p> controllers <pre><code>class RestaurantsController &lt; ApplicationController before_filter :authenticate_user! def index @restaurants = Restaurant.all end def show @restaurant = Restaurant.find(params[:id]) end end </code></pre> <p>Still not sure what might be the issue here - I'm assuming this is happening because something is missing to work devise properly as its using <code>ActionController::API</code> </p> <p><strong>UPDATE</strong> It seems the issue is with my <code>routes.rb</code> itself - This is how the routes are done.</p> <pre><code>require 'api_constraints' MyApi::Application.routes.draw do namespace :api, defaults: {format: 'json'} do scope module: :v1, constraints: ApiConstraints.new(version: 1,default: true) do devise_for :users resources :friends resources :locations resources :profiles resources :users end scope module: :v2, constraints: ApiConstraints.new(version: 2) do # Future releases of the API can go here end end end </code></pre> <p>Now If repeat the <code>devise_for :users</code> outside the scope everything started working.</p> <pre><code>require 'api_constraints' MyApi::Application.routes.draw do namespace :api, defaults: {format: 'json'} do scope module: :v1, constraints: ApiConstraints.new(version: 1,default: true) do devise_for :users resources :friends resources :locations resources :profiles resources :users end scope module: :v2, constraints: ApiConstraints.new(version: 2) do # Future releases of the API can go here end end devise_for :users end </code></pre> <p>Does any one has an explanation why? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload