Swati Yamgar
Swati Yamgar

Reputation: 139

Unable to send devise password as json data

I am using Devise for User table. In the api method I want to send my User record along with the password field as json. My code for that is:

users = User.where(status: "active")

Here in users I get all the fields including encrypted_password. But when I try to send this data with:

render json: { users: users }

the devise columns doesn't appear in the result except email.

How can I get password too in the result?

Upvotes: 0

Views: 350

Answers (1)

Jaehyun Shin
Jaehyun Shin

Reputation: 1602

Devise overrides the serializable_hash method to expose only accessible attributes.

Override as_json method in User class

def as_json(options = {})
  options[:methods] ||= []
  options[:methods] << :encrypted_password
  super options
end

or use

user.to_json(methods: [:encrypted_password])

Upvotes: 1

Related Questions