0

I am using rails app with angular js for authentication purpose i am using devise gem. When i am submitting sign in form, i am unable to login it is redirecting to same page.

app/views/devise/sessions/new.html.erb :

       <form class="form-horizontal">
                <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
                <fieldset>
                    <div class="form-group">
                      <div class="input-group input-group-lg">
                          <span class="input-group-addon">
                              <span class="glyphicon glyphicon-envelope"></span>
                          </span>
                                <%= f.email_field :email, autofocus: true , class: 'form-control', placeholder: 'Email'%>
                        </div>
                    </div>  

                    <div class="form-group">
                        <div class="input-group input-group-lg">
                            <span class="input-group-addon">
                                <span class="glyphicon glyphicon-lock"></span>
                            </span>
                            <%= f.password_field :password, autocomplete: "off" , class: 'form-control', placeholder: 'Password'%>
                        </div>
                    </div>

                    <div class="form-group">

                        <%= button_tag(type: 'submit', class: "btn btn-primary btn-lg btn-block") do %>
                             Login
                        <% end %>
                   </div>

                </fieldset>
                <% end %>
            </form>

Could any one explain where i am making mistake?

Baldrick
  • 23,882
  • 6
  • 74
  • 79
Anusha Nilapu
  • 1,243
  • 8
  • 24
  • 36
  • are you updating any of devises methods? if you are then can you post them – Mandeep Jun 11 '14 at 09:12
  • I didn't update devise methods. – Anusha Nilapu Jun 11 '14 at 09:13
  • can you post your logs then? It'll give you an idea of what exactly is happening behind the scenes when you click on sign in button – Mandeep Jun 11 '14 at 09:15
  • Started GET "/users/sign_in?utf8=%E2%9C%93&authenticity_token=27TrFYYcMELJ%2F%2Ff1cub1brDww%2B6RDmRtnOgno6q6J0w%3D&user%5Bemail%5D=user1%40gmail.com&user%5Bpassword%5D=[FILTERED]" for 127.0.0.1 at 2014-06-11 14:47:17 +0530 Processing by Devise::SessionsController#new as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"27TrFYYcMELJ//f1cub1brDww+6RDmRtnOgno6q6J0w=", "user"=>{"email"=>"user1@gmail.com", "password"=>"[FILTERED]"}} Rendered layouts/_navbar.html.erb (0.0ms) Rendered devise/sessions/new.html.erb within layouts/application (1.2ms) – Anusha Nilapu Jun 11 '14 at 09:18
  • In console i am getting like this @user2675613 – Anusha Nilapu Jun 11 '14 at 09:22
  • 1
    if you look at your logs devise has redirected back to `Rendered devise/sessions/new.html.erb within layouts/application`. If your password were wrong devise would have notified you but still are you sure your password is correct? – Mandeep Jun 11 '14 at 09:24
  • @AnushaNilapu try: restart server – Nithin Jun 11 '14 at 10:23
  • @nithin that i did so many times – Anusha Nilapu Jun 11 '14 at 10:54
  • @AnushaNilapu https://github.com/plataformatec/devise#getting-started hope you have followed this – Nithin Jun 11 '14 at 11:03
  • `<%=devise_error_messages! %>` in your view after `form_for` does it say anything? – Nithin Jun 11 '14 at 11:05

3 Answers3

1

U must have missed out on adding..

before_filter :authenticate_user!

in your controller

Nithin
  • 3,679
  • 3
  • 30
  • 55
0

Errors

The problem you have will be you've got errors with your form, and as devise uses <%= devise_error_messages! %> with login forms (and doesn't work out of the box).

You'll likely be receiving errors from your login attempts, but because you can't see them in your view, it seems like you're just being redirected back to the login page.

--

Test

A good way to test this will be to put this in your sessions/new.html.erb file:

<% if user_signed_in? %>
 SIGNED IN
<% else %>
 NOT SIGNED IN
<% end %>

This will show you if the session has been created for the user or not (I.E they've been logged in). If not, I would suggest you've got errors which you're unable to see

--

Fix

If the problem is you're receiving errors, the way to fix them will be to use one of the ways to show them in your view. We have used this question before to create this:

#app/views/devise/sessions/new.html.haml
= form_for resource, as: resource_name, url: session_path(resource_name), remote: request.xhr? do |f|
    = devise_error_messages!

#app/helpers/devise_helper.rb
Module DeviseHelper

  def devise_error_messages!
    flash_alerts = []
    error_key = 'errors.messages.not_saved'

    if !flash.empty?
      flash_alerts.push(flash[:error]) if flash[:error]
      flash_alerts.push(flash[:alert]) if flash[:alert]
      flash_alerts.push(flash[:notice]) if flash[:notice]
      error_key = 'devise.failure.invalid'
    end

    return "" if resource.errors.empty? && flash_alerts.empty?
    errors = resource.errors.empty? ? flash_alerts : resource.errors.full_messages

    messages = errors.map { |msg| content_tag(:li, msg) }.join
    sentence = I18n.t(error_key, :count    => errors.count,
                                 :resource => resource.class.model_name.human.downcase)

    html = <<-HTML
    <div id="error_explanation">
      <h2>#{sentence}</h2>
      <ul>#{messages}</ul>
    </div>
    HTML

    html.html_safe
  end
end
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
0

The problem is here we are using form_for and form both. I forget to update the answer. Using one thing fixes my problem. Thank you for trying to help me.*

Anusha Nilapu
  • 1,243
  • 8
  • 24
  • 36