skip to Main Content

I have react app is making calls to ruby on rails. I’m building login page i got stuck on the password authenticate part. getting error

 "#<ArgumentError: wrong number of arguments (given 0, expected 1)>"

the error is happening on this line

  if user && user.authenticate(password)

here is from the comman line
enter image description here

this is users_controller.rb

class Api::V1::UsersController < ApplicationController
before_action :authenticate_with_token!, only: [:logout]

def facebook
 enter code hereif params[:facebook_access_token]
  graph = Koala::Facebook::API.new(params[:facebook_access_token])
  user_data = graph.get_object("me?fields=name,email,id,picture")

  user = User.find_by(email: user_data['email'])
  if user
    user.generate_authentication_token
    user.save
    render json: user, status: :ok
  else
    user = User.new(
                fullname: user_data['fullname'],
                email: user_data['email'],
                uid: user_data['id'],
                provider: 'Facebook',
                image: user_data['picture']['data']['url']
            )
    user.generate_authentication_token

    if user.save
      render json: user, status: :ok
    else
      render json: { error: user.errors, is_success: false}, status: 422
    end
  end
  else
  render json: { error: "Invalid Facebook Token", is_success: false}, status: :unprocessable_entity
  end
end

def logout
 user = User.find_by(access_token: params[:access_token])
 user.generate_authentication_token
 user.save
 render json: { is_success: true}, status: :ok
end

def login
 email = user_params[:email]
 password = user_params[:password]
 user = User.find_by(email: email)
 if user && user.authenticate(password)
    session[:user_id] = user.id
    render json: user, status: :ok
 else
 render json: { error: user.errors, is_success: false}, status: 422
 end
end

def add_card
user = User.find(current_user.id)
if user.stripe_id.blank?
  customer = Stripe::Customer.create(
    email: user.email
  )
  user.stripe_id = customer.id
  user.save
else
  customer = Stripe::Customer.retrieve(user.stripe_id)
end

customer.sources.create(source: params[:stripe_token])
render json: { is_success: true}, status: :ok
rescue Stripe::CardError => e
render json: { error: e.message, is_success: false}, status: :not_found
end

def user_params
  params.require(:user).permit(:email, :password)
end

end

here is routes.rb

Rails.application.routes.draw do
namespace :api, defautls: {format: :json} do
namespace :v1 do
  get '/logout' => 'users#logout'
  get '/login' => 'users#login'
  post '/login' => 'users#login'
  post '/signup' => 'users#signup'
  end
 end
end

here is user.rb model

class User < ApplicationRecord
 has_secure_password
 before_create :generate_authentication_token
 devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, 
 :validatable,
     :confirmable, :omniauthable
 validates :fullname, presence: true, length: {maximum: 
 50}
end
def generate_authentication_token
 begin
  self.access_token = Devise.friendly_token
 end while self.class.exists?(access_token: access_token)
end

this is the api call im making
enter image description here

this is json that im sendng

{"email": "[email protected]", "password": "amer1234"}

2

Answers


  1. Chosen as BEST ANSWER

    the issue was with this line

     if user && user.authenticate(password)
    

    once i change to this it worked

     if user && user.valid_password?(params[:password])
    

  2. If you see the screenshot of console you attached, you will notice that params are: email, password, defaults and user if you look closely, you will see that this user has a hash, which only contains email and no password. This gives us a hint.

    Now, in your user_params method:

    def user_params
      params.require(:user).permit(:email, :password)
    end
    

    Here, params.require(:user) will require a key named user to be present, which is present, otherwise error will be thrown. Next you have: .permit(:email, :password) which permits these two params in parent user key’s hash value.

    Now, it seems like some issue in the JSON being sent, either you didn’t posted complete json or something is transforming your json before sending, or before handling it to controller(which is highly unlikely if anyone does this kind of customization, but who knows).

    Solution: If i were you, i will double check my json, or infact i will send only this json: {"user": {"email": "[email protected]", "password": "amer1234"}} or if you don’t want the nesting under user key, you must remove require from strong params on user key.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search