0

This is the functionality i used for "when a user fails to attempt login twice r thrice".

class Users::SessionsController < Devise::SessionsController

prepend_before_filter :require_no_authentication, :only => [ :new, :create ]

prepend_before_filter :allow_params_authentication!, :only => :create

prepend_before_filter { request.env["devise.skip_timeout"] = true }

def new

cookies[:login_attempts] = cookies[:login_attempts] || 0

   **unless params[:user][:email].blank?**

  if  cookies[:email].eql?(params[:user][:email])

    cookies[:login_attempts] = cookies[:login_attempts].to_i + 1

  else

    cookies[:login_attempts] = 0

  end

end

self.resource = resource_class.new(sign_in_params)

clean_up_passwords(resource)

respond_with(resource, serialize_options(resource))

end

# POST /resource/sign_in

def create

cookies[:email] =   params[:user][:email]

self.resource =  warden.authenticate!(auth_options)

if cookies[:email]

  cookies[:login_attempts] = 0

  set_flash_message(:notice, :signed_in) if is_navigational_format?

  sign_in(resource_name, resource)

  respond_with resource, :location => after_sign_in_path_for(resource)

else

  unless params[:user][:email].blank?

    if   cookies[:email].eql?(params[:user][:email])

      cookies[:login_attempts] = cookies[:login_attempts].to_i + 1

    else

      cookies[:login_attempts] = 0

    end

  end

  render :new

end

end end

In sessions/new:

   <% if cookies[:login_attempts].to_i >= 3 %>
                  <div><%= raw recaptcha_tags %></div>
              <% end %>

Entire functionality is working fine but when redirected to new,

Im getting the following error:

*NoMethodError in Users::SessionsController#new

undefined method `[]' for nil:NilClass

The error is shown in "unless params[:user][:email].blank?" line in above controller new method.*

Please try to help me out..

1 Answer 1

0

Your params[:user] returns nil object, a.k.a it does not exist. You shoudl check it first:

if params[:user] && cookies[:email].eql?(params[:user][:email])
Sign up to request clarification or add additional context in comments.

2 Comments

And if user fails to login twice, i am able to show recaptcha. So at which step can i add "if valid_captcha" condition and captcha error message in my code. Its quite confusing..
That's probably new question. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.