0

I recently created a way to change the current user password, however after saving the record, my UserSession.find returns nil, I tried by writing UserSession.new({...}).save with no luck, any suggestion on how to resolve this issue?

Here is my code, notice that is run through an AJAX request (this is a method under UserSessionController):

def

 change_my_password
    #print "--------------RECORD: #{current_user_session.record.as_json}-------- #{current_user_session.user.as_json}"
    user = current_user
    user_email = user.email
    user_remember_me = user.remember_created_at
    response = {
      :success => false,
      :message_code => Extjs::MessageCodes::ERROR,
      :message => 'Si è verificato un errore',
      :total => 0,
      :root => []
    }
    if user.valid_password?(params[:old_password], true)
      user.password = params[:new_password]
      user.password_confirmation = params[:confirm_password]
      response[:message] = 'La nuova password e la conferma non coincidono o sono troppo brevi'
      if user.save
        response[:success] = true
        response[:message_code] = Extjs::MessageCodes::SUCCESS
        response[:message] = 'Password modificata con successo'
      end
    else
      response[:message] = 'La password precedente non coincide con quella attualmente in uso'
    end

    respond_to do |format|
      format.extjson { render :json => response }
    end
  end
Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
  • `record.save` only returns true or false in order to indicate whether the record was persisted or not. Consequently `@user_session` wont hold your newly created record. – krichard Jun 21 '12 at 17:47
  • @KaiKönig: Sorry I didn't get this: isn't record my User Model record? save actually returns true, so it should change the data on the database (and for subsequent login/logout, password is in fact changed). Any suggestion on how to regenerate my session in some way? – Francesco Belladonna Jun 21 '12 at 18:04
  • When does `UserSession.find` return nil? In subsequent calls? – Harish Shetty Jun 21 '12 at 18:28
  • @KandadaBoggu: Across 2 AJAX requests to the same route (change_my_password). First time I call it it returns the ser, the second time it returns nil :\ (only if I actually change the password, otherwise it's ok). Notice that I updated the code. – Francesco Belladonna Jun 21 '12 at 18:31

2 Answers2

3

Ensure that you haven't set the maintain_sessions parameter to false, i.e.:

acts_as_authentic do |c|
  c.maintain_sessions = false # change this to true.
end

OR

Update the session manually after save:

user.send(:update_sessions)

OR

Recreate the session after the save:

UserSession.create(user)
Harish Shetty
  • 64,083
  • 21
  • 152
  • 198
  • I'm testing them but it seems that they aren't solving the issue: UserSession.create I think it destroys my session in some way, in fact the next time I call it I get user = nil. user.send(:update_sessions) same situation and with maintain_sessions explicitly set to true (in my config is not set usually), user is nil even on the first call, dunno why. I imagine I'm in a bad situation :\ – Francesco Belladonna Jun 21 '12 at 19:21
  • Tried in that way, it's not working. Notice that my first login happens through AJAX, maybe this can create a sort of issue? – Francesco Belladonna Jun 21 '12 at 20:09
  • 1
    I don't think it is related to AJAX as AJAX is a regular HTTP request. You should debug this at the client side. Also try to submit a regular request after `reset_password`. Print the session object before and after reset and in the new call. – Harish Shetty Jun 21 '12 at 21:56
0

Actually I didn't find a solution to this issue: I solved by forcing a login again after changing password (whcih is even added security, so not that bad).

I think KandadaBoggu is right with his answer, but I tested all of them and they are not working for me.

Maybe it's an issue created by something wrong in my code, I really don't know. At the moment I just consider this as the solution because it's actually working for my software.

If a better answer is found, I would be happy to mark it.

Thanks to everyone.

Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147