Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is how I improve this rails action
- # authentication API
- def auth
- # find is a method to search by id use find_by if we use other field as parameter
- # declare user as a class method
- @user = User.find_by(username: params[:username])
- # Before we check the password,
- # We need to check if the user with that username is present
- if @user.present? && @user.check_password(params[:password])
- # json response should always including status
- # give status ok (200) for successful auth
- render json: @user, status: :ok
- else
- # The errors message is not needed to be in an array
- # Use single-quoted if we don't use string interpolation.
- render json: { errors: 'wrong username or password' }, status: :unauthorized
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement