{"id":648,"date":"2022-08-30T15:04:51","date_gmt":"2022-08-30T15:04:51","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-authenticate-the-user-with-warden-devise-in-a-customized-way-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:51","modified_gmt":"2022-08-30T15:04:51","slug":"how-to-authenticate-the-user-with-warden-devise-in-a-customized-way-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-authenticate-the-user-with-warden-devise-in-a-customized-way-collection-of-common-programming-errors\/","title":{"rendered":"How to authenticate the user with warden\/devise in a customized way?-Collection of common programming errors"},"content":{"rendered":"<p>This is a Rails 3.0 app with Mongoid as ODM. Following is the user model in which the devise is installed.<\/p>\n<pre><code>class User\n  include Mongoid::Document\n\n  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable\n\n  field :email\n  field :username\n  field :loginable_token\n\nend\n<\/code><\/pre>\n<p>I&#8217;m building an API. So, for authentication I wanted to authenticate the users with :email and :password. From the GUI, its fine and working.<\/p>\n<p>Now, I want to implement it via API, so I ve created a separate route <code>\/api_login.json<\/code> that hits the <code>login<\/code> action with params <code>{\"username\"=&gt;\"kapil@gmail.com\", \"password\"=&gt;\"sachin\", \"action\"=&gt;\"login\", \"controller\"=&gt;\"api\/v1\/accounts\", \"format\"=&gt;\"json\"}<\/code><\/p>\n<p>When I try to authenticate with the <code>warden.authenticate!(:scope =&gt; :user)<\/code> method before <code>sign_in<\/code>, it blows up with the following backtrace.<\/p>\n<pre><code>(rdb:1) l\n[3, 12] in \/Users\/sts-151sts-151\/Dev\/cloudfactory\/app\/controllers\/api\/v1\/accounts_controller.rb\n  3      respond_with(@current_account)    \n  4    end\n  5    \n  6    def login\n  7      debugger\n=&gt; 8      resource = warden.authenticate!(:scope =&gt; :user)\n  9       sign_in(:user, resource)\n  10      if user == nil\n  11        \n  12        respond_with(User.account.apps.first)\n(rdb:1) p params\n{\"username\"=&gt;\"kapil@gmail.com\", \"password\"=&gt;\"sachin\", \"action\"=&gt;\"login\", \"controller\"=&gt;\"api\/v1\/accounts\", \"format\"=&gt;\"json\"}\n(rdb:1) rs = warden.authenticate!(:scope =&gt; :user)\n*** Unknown command: \"rs = warden.authenticate!(:scope =&gt; :user)\".  Try \"help\".\n(rdb:1) p rs = warden.authenticate!(:scope =&gt; :user)\n\n 1) \/api\/v1\/lines POST send back the default app\n    Failure\/Error: last_response.status.should eql(200)\n\nexpected 200\n    got 401\n\n(compared using eql?)\n\n    # .\/spec\/api\/v1\/accounts_spec.rb:46:in `block (3 levels) in '\n<\/code><\/pre>\n<p>The test is via default <code>rack-test<\/code> way.<\/p>\n<p>What is correct way to authenticate?<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:10:28. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>This is a Rails 3.0 app with Mongoid as ODM. Following is the user model in which the devise is installed. class User include Mongoid::Document devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable field :email field :username field :loginable_token end I&#8217;m building an API. So, for authentication I wanted to authenticate the users with :email and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-648","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=648"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/648\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}