Devise token auth: what format for update user json? - ruby-on-rails

I am trying to update a user via PUT to /auth
via the docs:
Account updates. This route will update an existing user's account settings. The default accepted params are password and password_confirmation, but this can be customized using the devise_parameter_sanitizer system. If config.check_current_password_before_update is set to :attributes the current_password param is checked before any update, if it is set to :password the current_password param is checked only if the request updates user password.
It clearly needs something beyond just password and password_params as an identifier but neither email, id, or uid work. Also tried submitting with root user ie:
{
"user": {
"email": ""
}
}
but this also does not work.
I cannot figure out what this is expecting. Not sure if I am missing something obvious. Could anyone give me an example of the json format which this is expecting?

Related

custom validation on django-allauth

I'm using dj-allauth on my django app. When people sign up to my django project/app, I want to check that the domain on their email matches mydomain.com. Is this possible? I think I can do it by changing the signup form. I've tried the following which doesn't seem to do anything when I submit the signup form. There aren't any errors. It isn't checking for the domain, it's prompting for the user's nickname and then appending the domain automatically.
forms.py
class VSBSignupForm(SignupForm):
def save(self, request):
# Ensure you call the parent classes save.
# .save() returns a User object.
user = super(VSBSignupForm, self).save(request)
user.email_address = user.username + "#mydomain.com"
user.save()
# You must return the original result.
return user
settings.py
ACCOUNT_FORMS = {'signup': 'myapp.forms.VSBSignupForm'}
I changed the template for the signup.html such that it asks for the nickname and password, but not the email address.
edit I tried putting a print statement in the form save method and it didn't print anything in the shell, which makes me think the form isn't being used.

Devise - password update via recoverable email causes email has already been taken

Devise version 4.3.0
Rails 4.2.8
I am having trouble letting a user update their password after they are sent a password reset email.
Steps to reproduce
When a user clicks Forgot Your Password from the sign in page.
They enter in their email on the forgot password page
They are then sent a password reset email
In the email they click the link
They are taken to the Change your password page
They enter in an updated password and password confirmation
An error is shown that the email is already taken
The email is already taken because they have an account already, I would like to update their password not create a new account.
I do not have any devise controllers that I have overridden.
After checking the user logs when I make the request it is a post request not a put. Even though the form says method: put
This is how you are trying making your form send PUT request: html: {method: :put}.
html option adds optional HTML attributes for the form tag, so what is happening here is that you are simply adding method='PUT' attribute to the form.
However, most browsers don't support methods other than "GET" and "POST" when it comes to submitting forms.
To make this work you need some Rails magic. Rails magic will work if you pass method option another way, not inside html option:
form_for(resource, as: ..., url: ..., method: :put )
http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark

Fetch the Password and confirmation field of a Model object in Rails

Is there any way to fetch the password and password_confirmation field of a Model object in Rails.? Say for example, I have an user object
user = User.find(1)
Now, I want to fetch the value of the password field like this.
password = user.password
confirmation = user.password_confirmation.
I tried this, but it is returning nil.
I know, this is a security issue if they allow these fields to be accessible., But, I want to fetch these values for a feature to be implemented in our application.
Assuming you are using Devise, the only attribute related to the user's password is encrypted_password, it will return the BCrypt encrypted password. You can also read the password_salt User attribute.
You can see all Devise User Model attributes with: User.column_names

Rails3 User Changed Password, how do I change the current session password?

I have the need to let the user change their password. I'd like to switch the password in the session to reflect this new password, without logging them out.
def set_auth username, password
# test username and password here?
auth_object = AuthCookie.new
auth_object.set_username username
auth_object.set_password password
session[:user_login] = auth_object
end
I use something like the above, but it doesn't seem to work in changing the current session's password to the new one the use just entered.
What am I doing wrong?
Don't save your whole auth object in the session, the most important thing is you should not save password info in the session. Rails default session storage is cookie based, just base64 encode string. So if you save user password info in the session, there is security problem.
Just put the user identify in the session, for example, the user_id. session[:user_id] = user_id

Devise, validate current_password if any important fields have been changed

Specifically, I'm using Devise with Typus. But, I think my misunderstanding resides in my knowledge of Devise.
I'm trying to achieve the functionality of when you want to change an important model via form, you have to provide your current password to confirm you can change it, a la google.
Right now, I can log in and change any of the fields of my User model. Including the password, without having to confirm my password prior. Not good. So, I've added current_password to the form. But that didn't do anything. Then I tried to validate presence on current_password. Then it doesn't seem to accept any value for it.
Google didn't help me. All of the relevant posts were about removing current_password instead of confirming it. Which makes me think I'm misunderstanding the use of current_password.
Anyone care to share some insight? Thanks.
You should add the password field to the form, and then in your controller's action you can validate the password using:
user.valid_password?(params[:user][:password])
Note that probably you should change params[:user][:password] to the the name of the param for your password's field in the form (perhaps just params[:password]).
Hope it helps.
Actually, devise has a builtin method for this:
user.update_with_password(params, *options) you can read the rdoc here.
Update record attributes when :current_password matches, otherwise returns error on :current_password. It also automatically rejects :password and :password_confirmation if they are blank.

Resources