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

  • Home
  • SEARCH
  • 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 8185081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:43:17+00:00 2026-06-07T01:43:17+00:00

I have a basic authentication system just like in Michael Hartl’s Ruby on Rails

  • 0

I have a basic authentication system just like in Michael Hartl’s Ruby on Rails Tutorial. Basically, a remember token is stored in a cookie. I implemented Ryan Bate’s Beta-Invitations from Railscast #124, where you can send a limited number of invitations. While doing that, I ran into the problem that the current user got logged out after sending an invitation. This was caused by this code in the invitation model:

invitation.rb

belongs_to :sender, :class_name => 'User'
[...]
before_create :decrement_sender_count, :if => :sender
[...]
def decrement_sender_count
  sender.decrement! :invitation_limit
end

In the logs I saw that sender.decrement! not only updated the invitation_limit but the remember_token as well:

UPDATE "users" SET "invitation_limit" = 9982, "remember_token" = 'PYEWo_om0iaMjwltU4iRBg', "updated_at" = '2012-07-06 09:57:43.354922' WHERE "users"."id" = 1

I found an ugly workaround but I would love to know what the problem really is. Since I don’t know where to start, I’ll show you the update method from the users controller. What else could be relevant?

users_controller.rb

def update
  @user = User.find(params[:id])
  if @user.update_attributes(params[:user])
    flash[:success] = t('success.profile_save')
    sign_in @user
    redirect_to @user
  else
    flash.now[:error] = t('error.profile_save')
    render 'edit'
  end
end
  • 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-07T01:43:19+00:00Added an answer on June 7, 2026 at 1:43 am

    decrement! calls save which of course fires save callbacks. It looks like the book directs you to do

    before_save :create_remember_token
    def create_remember_token
      self.remember_token = SecureRandom.urlsafe_base64
    end
    

    which means that saving a user will always invalidate the remember token. I assume this is so that when a user changes their password the remember token changes too, but it means that there is obviously some collateral damage.

    You could use the decrement_counter which in essence does

    update users set counter_name = counter_name - 1 where id =12345
    

    without running any callbacks. This also avoids some race condition scenarios. However changing the token whenever the user changes is bound to change the token at times when you don’t expect it – you might want to only change it when relevant (perhaps when credentials have changed)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few more basic questions about Ruby on Rails. 1) When I
I have been trying to write an basic authentication system as part of a
I have a node.js Express application with a basic user authentication system where the
I have a WCF web service, and I want to use Basic authentication. I
I have a website that uses basic ASP.Net forms authentication. In the web.config file
For development purposes we have a site setup with BASIC authentication in IIS7. One
I have a WSS installation that's behind basic authentication/SSL (it's hosted at a public
I built an authentication system for my app following these Railscasts: http://railscasts.com/episodes/250-authentication-from-scratch and http://railscasts.com/episodes/274-remember-me-reset-password
I have been following an article on how to implement a custom Basic authentication
I have followed the article here http://chrisdail.com/2008/08/13/http-basic-authentication-with-apache-cxf-revisited/ I now have a working interceptor for

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.