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

Updating user fails with error 400 "Email is already taken" #2446

Closed
pwkrz opened this issue Dec 7, 2018 · 5 comments · Fixed by #2669
Closed

Updating user fails with error 400 "Email is already taken" #2446

pwkrz opened this issue Dec 7, 2018 · 5 comments · Fixed by #2669
Assignees
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around

Comments

@pwkrz
Copy link

pwkrz commented Dec 7, 2018

Informations

  • Node.js version: 10.12.0
  • NPM version: 6.4.1
  • Strapi version: 3.0.0-alpha.16
  • Database: MongoDB (4.0.4)
  • Operating system: Windows 10

What is the current behavior?

Updating a user model fails with error 400 "Email is already taken".

Steps to reproduce the problem

Send PUT request to /users/:_id when logged in as global Strapi admin.

What is the expected behavior?

The user object with the matching ID should be correctly updated and the server should return a 200 response.

Suggested solutions

It looks like the in the route configuration for User.update the _id param is used (line 250 of <project>\plugins\users-permissions\config\routes.json):

{
      "method": "PUT",
      "path": "/users/:_id",
      "handler": "User.update",
      "config": {
        "policies": [],
        "prefix": ""
      }
    }

but the handler checks for id (without underscore) in ctx.params, eg. over here (line 123 of <project>\plugins\users-permissions\controllers\User.js):

if (users && _.find(users, user => (user.id || user._id).toString() !== ctx.params.id)) {
          return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Auth.form.error.email.taken', field: ['email'] }] }] : 'Email is already taken.');
        }

Possible solution - either change path configuration to '/users/:id' or check for '_id' in ctx.params

@lauriejim lauriejim self-assigned this Dec 7, 2018
@lauriejim lauriejim added issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product status: have to reproduce labels Dec 7, 2018
@lauriejim
Copy link
Contributor

Hello @pwkrz can you precise your steps (body of your request)
And configuration of your app please

I'm able to update the email of one of my user.

@lauriejim lauriejim added status: can not reproduce Not enough information to reproduce status: need informations and removed status: have to reproduce severity: high If it breaks the basic use of the product labels Dec 7, 2018
@pwkrz
Copy link
Author

pwkrz commented Dec 7, 2018

Hi @lauriejim ,

I have Strapi 3.0.0-alpha.16 installed globally and I created a project using strapi new ....

Using the global admin account I send a PUT request to http://localhost:1337/users/5c07db0fbf19483b18f1c412 with the body:

{"_id":"5c07db0fbf19483b18f1c412","wid":"5c07db0fbf19483b18f1c412","userRole":"EDITOR","firstName":"Ivan","lastName":"Johnson","description":"// test","email":"asdladj@o2.pl","telephone":"234234234243","password":null,"someNewProp":null,"dateOfBirth":1228953600000,"registeredAt":1544018702907,"profileImg":null,"fullName":null,"isMale":true,"media":null}

And I get the response {"statusCode":400,"error":"Bad Request","message":"Email is already taken."}.

If I change the route configuration in <myproject>\plugins\users-permissions\config\routes.json to /users/:id, the problem goes away.

If you need more info, please let me know!

@Blacknosaur
Copy link

I have stumbled into this problem too. If you try to update an user but you are not going to change the email of this user, if the email is in the body of the PUT request.

Obviously it should not fail but as a temporary fix try not including the email in the body of the request if you aren't changing it.

@lauriejim lauriejim added severity: medium If it breaks the basic use of the product but can be worked around status: have to reproduce and removed status: can not reproduce Not enough information to reproduce status: need informations labels Dec 20, 2018
@toksdotdev
Copy link

toksdotdev commented Dec 29, 2018

As regards this issue, I discovered that the error was as a result of a mandatory firstname field being required during signup. I don't know why.

As a quick fix, I performed a manual POST request (curl/fetch/etc), while including the firstname field required.

TO REPRODUCE THIS ERROR:

  • Create a strapi project, connect to the db, signup as admin
  • Stop the project (Ctrl + C)
  • Delete the current project's database (in my case, I was using mongodb)
  • Start the project again (strapi start): It should now take you to the admin registration page
  • Try signing up, with or without the email previously used, and the above error would occur.

@merijnponzo
Copy link

I'm experiencing this problem as well with the graphql plugin
"strapi": "3.0.0-alpha.16"
nodejs v10.6.0

I can only update a user, when I change the emailaddress, but I think it should be possible to only change the username, for example

schermafbeelding 2018-12-30 om 17 00 30

@strapi strapi locked as too heated and limited conversation to collaborators Dec 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around
Projects
None yet
5 participants