2

I'm setting up user auth in my Rails app using Postgres and calling User.new() continuously keeps telling me User Exists despite the user definitely NOT being in my database.

app/controllers/api/user_controller.rb

class Api::UserController < ApplicationController
    respond_to :json
    protect_from_forgery with: :null_session


def create
    @user = User.new(
        email: params[:email],
        password: params[:password],
        password_confirmation: params[:password_confirmation],
        first_name: params[:first_name],
        last_name: params[:last_name],
        groupname: params[:groupname],
        admin: params[:admin]
    )

    if @user.save
        puts "it worked"
    else
        puts "Didn't work"
    end
end
end

app/models/user.rb

class User < ApplicationRecord
    before_save { self.email = email.downcase }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
    validates :first_name, presence: true, length: { maximum: 51 }
    validates :groupname, presence: true, length: { maximum: 51 }
    has_secure_password
    validates :password, presence: true, length: { minimum: 6 }
end

app/db.schema.rb

create_table "users", force: :cascade do |t|
    t.string "email"
    t.string "password_digest"
    t.string "first_name"
    t.string "last_name"
    t.string "groupname"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "admin"
    t.index ["email"], name: "index_users_on_email", unique: true
  end

terminal response

app/controllers/api/user_controller.rb:31:in `create'
Started POST "/api/user" for 127.0.0.1 at 2018-01-30 19:03:55 -0800
Processing by Api::UserController#create as JSON
  Parameters: {"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "first_name"=>"asdfasfd", "last_name"=>"asdfasddfs", "groupname"=>"fdsafdasf", "admin"=>"true", "user"=>{"email"=>"[email protected]", "first_name"=>"asdfasfd", "last_name"=>"asdfasddfs", "groupname"=>"fdsafdasf", "admin"=>"true"}}
Can't verify CSRF token authenticity.
   (0.2ms)  BEGIN
  User Exists (0.4ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER($1) LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
   (0.2ms)  ROLLBACK
Didn't work
No template found for Api::UserController#create, rendering head :no_content
Completed 204 No Content in 118ms (ActiveRecord: 6.3ms)
3
  • 2
    change puts "Didn't work" to p @user.errors.full_messages to see why... Commented Jan 31, 2018 at 3:22
  • Yeah, the save-failed message doesn't help at all here Commented Jan 31, 2018 at 3:28
  • @BradWerth thank you! Credited in answer Commented Jan 31, 2018 at 4:01

3 Answers 3

2

User Exists in the log mention to an operation that Rails do to check uniqueness validation for email of user. You can try to remove the uniqueness validation and check the log.

More details about how Rails check uniqueness validation: https://robots.thoughtbot.com/the-perils-of-uniqueness-validations

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the resource! Will look into it
Yep. Basically it is just an operation of Rails to check validation, it doesn't said that you can't save user to database because of its validation is violated. As you mention in the log that is because password confirmation is not match.
1

Thanks @BradWerth for the debugging help. Turns out it wasn't writing to the database because the password confirmation didn't match the password. I'm not sure what the User Exists thing is all about (will look in to it @Tai) but this is what's happening now...

using @user.errors.full_messages

Started POST "/api/user" for 127.0.0.1 at 2018-01-30 19:55:27 -0800
Processing by Api::UserController#create as JSON
  Parameters: {"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "first_name"=>"asdfasfd", "last_name"=>"asdfasddfs", "groupname"=>"fdsafdasf", "admin"=>"true", "user"=>{"email"=>"[email protected]", "first_name"=>"asdfasfd", "last_name"=>"asdfasddfs", "groupname"=>"fdsafdasf", "admin"=>"true"}}
Can't verify CSRF token authenticity.
   (0.3ms)  BEGIN
  User Exists (0.5ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER($1) LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
   (0.2ms)  ROLLBACK
["Password confirmation doesn't match Password"]
No template found for Api::UserController#create, rendering head :no_content
Completed 204 No Content in 117ms (ActiveRecord: 6.4ms)

using @user.errors.full_messages with correct password confirmation

Started POST "/api/user" for 127.0.0.1 at 2018-01-30 19:55:52 -0800
Processing by Api::UserController#create as JSON
  Parameters: {"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "first_name"=>"asdfasfd", "last_name"=>"asdfasddfs", "groupname"=>"fdsafdasf", "admin"=>"true", "user"=>{"email"=>"[email protected]", "first_name"=>"asdfasfd", "last_name"=>"asdfasddfs", "groupname"=>"fdsafdasf", "admin"=>"true"}}
Can't verify CSRF token authenticity.
   (0.2ms)  BEGIN
  User Exists (0.4ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER($1) LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
  SQL (0.6ms)  INSERT INTO "users" ("email", "password_digest", "first_name", "last_name", "groupname", "created_at", "updated_at", "admin") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["email", "[email protected]"], ["password_digest", "$2a$10$W1wt6arxrP8sW/K1jQPsLuEX8BUFMl7lOSgvCHa6hBTdON/HCoHNa"], ["first_name", "asdfasfd"], ["last_name", "asdfasddfs"], ["groupname", "fdsafdasf"], ["created_at", "2018-01-31 03:55:52.787377"], ["updated_at", "2018-01-31 03:55:52.787377"], ["admin", "t"]]
   (1.0ms)  COMMIT
it worked
No template found for Api::UserController#create, rendering head :no_content
Completed 204 No Content in 112ms (ActiveRecord: 2.2ms)

Comments

1

I see multiple potential problems here:

1/ Can't verify CSRF token authenticity. if this is an API then I assume you are trying to use it outside a rails form which means thats if have to send the csrf token inside the request OR remove de forgery protection from your action. http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html

2/ Rails as a "Strong Params" concept that may prevent you from using params directly to manipulate your model. http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters

"User EXIST" is what appears on the output because of the check done with ruby validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }

1 Comment

1) thank you for the reference, i'll look in to sending the CSRF token with the request (i'm using react so i am outside the rails form) 2) I'm trying to use "Strong Params" but was having issues so this is my workaround for now just to make sure that I can at least write to my database. Here is the link to my previous question using "Strong params" stackoverflow.com/questions/48512933/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.