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

500 error when user not exist #85

Open
DevArenaCN opened this issue Feb 15, 2017 · 5 comments
Open

500 error when user not exist #85

DevArenaCN opened this issue Feb 15, 2017 · 5 comments

Comments

@DevArenaCN
Copy link

DevArenaCN commented Feb 15, 2017

I'm working on the log in with sessions_controller, so my create function looks like this:

def create
    user_password = params[:session][:password]
    user_email = params[:session][:email]
    user = user_email.present? && User.find_by(email: user_email)
    if user and user.valid_password? user_password
      sign_in user, store: false
      user.generate_authentication_token!
      user.save
      render json: user, status: 200, location: [:api, user]
    else
      render json: { errors: "Invalid email or password" }, status: 422
    end
  end

So when the user does not exist, I got a 500 error instead of a 422. Any ideas? I tried rescue ActiveRecord::RecordNotFound but it's not working for me.

@kurenn
Copy link
Owner

kurenn commented Feb 15, 2017

On which part of the code is this failing?, I would say on the if, but what is the user variable value at that point?

@DevArenaCN
Copy link
Author

So when the email does not exist in the ActiveRecord, it will throw out a 500 internal server error instead of a 422 Invalid email or password error.

@kurenn
Copy link
Owner

kurenn commented Feb 16, 2017

I think the user variable is nil, somehow, can you confirm this?

@DevArenaCN
Copy link
Author

I can verify the login if the user exist in the database, which I don't think it's nil, right?

@kurenn
Copy link
Owner

kurenn commented Feb 17, 2017

I think you are getting the 500 because of this user.valid_password? user_password I'm not sure but it can be that the user is nil, so is trying to execute valid_password? over nil...it should not happen though...

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

2 participants