Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Ajax login and detecting when an account's locked
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Nick Hoffman  
View profile  
 More options Sep 26 2011, 7:03 pm
From: Nick Hoffman <n...@deadorange.com>
Date: Mon, 26 Sep 2011 16:03:42 -0700 (PDT)
Local: Mon, Sep 26 2011 7:03 pm
Subject: Ajax login and detecting when an account's locked

Hi guys. How can we determine that authentication failed because the user's
account is locked?

I've created a custom failure app and session controller, and configured
Warden to use my custom failure app. Logging-in via Ajax works beautifully.
However, if the user's account is (or becomes) locked, how can I detect
that, and send a different JSON response?

Here's the custom failure app and custom session controller:
https://gist.github.com/1243688

Thanks for your help with this. I really appreciate it.
Nick


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Hoffman  
View profile  
 More options Sep 27 2011, 8:02 am
From: Nick Hoffman <n...@deadorange.com>
Date: Tue, 27 Sep 2011 05:02:55 -0700 (PDT)
Local: Tues, Sep 27 2011 8:02 am
Subject: Re: Ajax login and detecting when an account's locked

So I dug through Devise's and Warden's code, followed the output from using
"caller" at the beginning of my custom failure app, and figured it out.

Devise asks the Lockable strategy if the resource is valid for
authentication (via Lockable#valid_for_authentication?). Lockable returns
:locked if the resource is locked. Devise stores that result in Warden. To
access that result, you just need to check the value of warden.message .

Here's how I did it:

class SessionFailure < Devise::FailureApp
  def respond
    return super unless request.xhr?

    message = I18n.t 'devise.failure.invalid'
    cause   = 'invalid'

    if warden.message == :locked
      message = I18n.t 'devise.failure.locked'
      cause   = 'account_locked'
    end

    self.status         = 200
    self.content_type   = 'json'
    self.response_body  = {
      :status => 'fail',
      :data   => { :message  => message, :cause => cause },
    }.to_json
  end
end


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Hoffman  
View profile  
 More options May 10 2012, 2:57 pm
From: Nick Hoffman <n...@deadorange.com>
Date: Thu, 10 May 2012 11:57:10 -0700 (PDT)
Local: Thurs, May 10 2012 2:57 pm
Subject: Re: Ajax login and detecting when an account's locked

On Thu, May 10, 2012 at 1:00 AM, webguy5 <mastermik...@gmail.com> wrote:
> this is very nice. Great job! What does you javascript look like to
> send the data from the sessions controller to the form?

Hi Mike. My SessionsController responds with JSON, not JavaScript:

  def create
    resource = warden.authenticate :scope => resource_name

    if resource.is_a? User
      sign_in resource

      return render :json => {
        :status => 'success',
        :data   => {:message => I18n.t('devise.sessions.signed_in')},
      }
    end

Cheers,
Nick


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
webguy5  
View profile  
 More options May 10 2012, 10:58 pm
From: webguy5 <mastermik...@gmail.com>
Date: Thu, 10 May 2012 19:58:43 -0700 (PDT)
Local: Thurs, May 10 2012 10:58 pm
Subject: Re: Ajax login and detecting when an account's locked

Thanks. When the user account is locked warden returns the user is invalid
and triggers the failed_attempts strategy I have on the user. How do I call
on the user object in the custom failure app to avoid it doing this.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »