Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7750513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:18:41+00:00 2026-06-01T11:18:41+00:00

I use the devise’s authenticate_user! method in a controller. This is working fine when

  • 0

I use the devise’s authenticate_user! method in a controller. This is working fine when the auth_token provided in the request is the correct one but if the authentication fails, I end up with:

curl -XGET 'http://localhost:3000/my_obj?auth_token=wrongtoken'

<html><body>You are being <a href="http://localhost:3000/users/sign_in">redirected</a>.</body></html>

As I use rabl, what is the best way to have something like

{'error' : 'authentication error'}

returned intead of the html redirection ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:18:42+00:00Added an answer on June 1, 2026 at 11:18 am

    I do that in avoid the filter with :format => :json response and do my own filter to render my JSON response if no current_user pass

    class MyController < ApplicationController
      before_filter :authenticate_user!, :unless => { request.format == :json }
      before_filter :user_needed, :if => { request.format == :json }
    
      def user_needed
        unless current_user
          render :json => {'error' => 'authentication error'}, :status => 401
        end
      end
    end
    

    An other way, can be cleaner is to define your own FailureApp ( https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb )

    class MyFailureApp < Devise::FailureApp
      def respond
        if request.format == :json
          json_failure
        else
          super
        end
      end
    
      def json_failure
        self.status = 401
        self.content_type = 'application/json'
        self.response_body = "{'error' : 'authentication error'}"
      end
    end
    

    In your Devise config file add :

    config.warden do |manager| 
      manager.failure_app = MyFailureApp 
    end 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have use devise_openid_authenticatable to support OpenID with devise in rails3 beta4. But when
Hey, I want to use Devise and acts_as_audited together but when I try and
I use this link to put the login links on my application : https://github.com/plataformatec/devise/wiki/How-To:-Add-sign_in,-sign_out,-and-sign_up-links-to-your-layout-template
I have this application where I use Devise and, after login the user is
I'm trying to use the MongoID-Devise generator template by doing this under Ubuntu and
I use Devise in my new project,but I have some problems in roles. There
I'm currently trying to use Devise 1.1.pre3 as authentication in my upcoming project, but
I don't know when it happened but I'm getting this error NoMethodError (undefined method
config/initializers/devise.rb=> # Use this hook to configure devise mailer, warden hooks and so forth.
I want to use devise's tokens to support logging in/out in multiple clients at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.