Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate error messages #892

Closed
moray95 opened this issue May 21, 2017 · 12 comments
Closed

Duplicate error messages #892

moray95 opened this issue May 21, 2017 · 12 comments

Comments

@moray95
Copy link
Contributor

moray95 commented May 21, 2017

I have integrated devise_token_auth to my Ruby on Rails application and have encountered a strange issue with errors. Sometimes, the requests managed by devise_token_auth returns the same error multiple times in the errors field. Here is an example of such responses:

{
  "status": "error",
  "data": {
    "id": null,
    "provider": "email",
    "uid": "",
    "name": null,
    "nickname": null,
    "image": null,
    "email": "[email protected]",
    "created_at": null,
    "updated_at": null
  },
  "errors": {
    "email": [
      "has already been taken",
      "has already been taken"
    ],
    "full_messages": [
      "Email has already been taken",
      "Email has already been taken"
    ]
  }
}

To reproduce the issue just clone this repository, containing a basic setup for devise_token_auth, and once the server is running, send the request below twice. The first one will succeed and the second one will fail with duplicate errors, like the one above.

POST /auth HTTP/1.1
Content-Type: application/json
Host: localhost:3000
Connection: close
Content-Length: 115

{"email":"[email protected]","password":"my_super_strong_password","confirm_success_url":"http://localhost:3000"}
@stephanebruckert
Copy link

stephanebruckert commented Jun 19, 2017

How did you fix it @moray95 ?


Meanwhile...

validatable validates both the email and the password, which is why we shouldn't do any of those in models, otherwise it would be validated twice. In my case, only the email was validated twice, whereas I wasn't adding any extra validation in my model. So I ended up removing validatable and made my own validation on password and password_confirmation:

  validates_presence_of :password, on: :create
  validates :password,
    length: { minimum: 8 },
    allow_blank: true
  validate  :password_complexity
  validates_confirmation_of :password
  validates_presence_of :password_confirmation, if: lambda {| u| u.password.present? }

  private

    def password_complexity
      return unless password
      if password.include?(username) || !password.match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)./)
        errors.add(:password, "Must include at least one lowercase letter, one uppercase letter and one digit")
      end
    end

@maknz
Copy link

maknz commented Jul 8, 2017

I'm seeing the same problem here, if you attempt to sign up with an email already in use, you get multiple validation errors.

    "errors": {
        "email": [
            "has already been taken",
            "has already been taken"
        ],
        "full_messages": [
            "Email has already been taken",
            "Email has already been taken"
        ]
    }

devise (4.3.0)
devise_token_auth (0.1.42)

Ruby 2.4.1, Rails 5.1.1, using Postgres (pg 0.21.0)

@roseliux
Copy link

I have the same problem.

@ethagnawl
Copy link
Contributor

ethagnawl commented Sep 15, 2017

It seems like this issue could potentially be caused by the following piece of code in DeviseTokenAuth::Concerns::User:

# Hack to check if devise is already enabled
unless self.method_defined?(:devise_modules)
  devise :database_authenticatable, :registerable,
              :recoverable, :trackable, :validatable, :confirmable
else
  self.devise_modules.delete(:omniauthable)
end

I've also noticed that if you place the include before your own call to devise, the error message will appear three times.

@roseliux
Copy link

I hope they fix this.

@rmcsharry
Copy link

Same issue here. Posted a stack overflow question here.

@lynndylanhurley
Copy link
Owner

I'll take a look at this tomorrow. Does anyone have the time to submit a PR with a failing test?

@ethagnawl
Copy link
Contributor

ethagnawl commented Oct 5, 2017

@lynndylanhurley In attempting to write a failing test, I've realized this issue seems to only affect Rails >= 5.1.0. So, I've got a failing test, but can't submit a PR because (I'm pretty sure) it would involve changing the gemspec.

screenshot 2017-10-04 23 11 26

Hopefully that information is of some use to you!

@jduff
Copy link

jduff commented Oct 30, 2017

I ran into this on the released version of the gem, seems to be fixed on master since #981 was merged though.

@zachfeldman
Copy link
Contributor

Good to know. We should be cutting a release soon per @lynndylanhurley !

@MaicolBen
Copy link
Collaborator

I checked and is fixed in master as @jduff said, so I'm closing this

@Hoss3770
Copy link

Hoss3770 commented Apr 25, 2021

A similar issue showed up again in 1.1.5 and 1.1.4
If I use :validatable or not it does not make a difference.
I always get 422 Unprocessable Entity instead of "Email already taken" error.
Any clues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests