I'm working on a Rails API which will authenticate by means of a jwt, and would like the user accounts to be recoverable. Presumably, after receiving their reset token by email, users would supply their new password and the token with a PUT to https://example.com/users/password
, to be handled by /app/controllers/users/passwords_controller#update
. At present I've got this in passwords_controller.rb
(mostly as in the default Devise code):
# PUT /resource/password
def update
# {"user": {"password": "secret", "password_confirmation": "secret", "reset_password_token": "token_goes_here"}}
self.resource = resource_class.reset_password_by_token(resource_params) # problem here (line 33)...
yield resource if block_given?
if resource.errors.empty?
resource.unlock_access! if unlockable?(resource)
if Devise.sign_in_after_reset_password
resource.after_database_authentication
auth_options = {user: {login: resource.username, password: params['user']['password']}}
warden.authenticate!(auth_options)
render json: {success: true, jwt: current_token, response: "Authentication successful" }
else
render json: {success: false, response: "Authentication failed" }
end
else
set_minimum_password_length
respond_with resource
end
end
The problem is that reset_password_by_token
is requesting authorization:
app/controllers/users/passwords_controller.rb:33:in `update'
Completed 401 Unauthorized in 220ms (ActiveRecord: 60.8ms | Allocations: 6810)
This seems odd to me as I'd expect the user to be able to log in once with the token (unless expired) for the password change. At that point I'd like to return the jwt (hence using warden.authenticate
) so that the front-end application may immediately use it to sign the user in.
Would anyone be able to point me in the correct direction here?