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)
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
this is json that im sendng
{"email": "[email protected]", "password": "amer1234"}
2
Answers
the issue was with this line
once i change to this it worked
If you see the screenshot of console you attached, you will notice that params are:
email, password, defaults
anduser
if you look closely, you will see that thisuser
has a hash, which only contains email and no password. This gives us a hint.Now, in your user_params method:
Here,
params.require(:user)
will require a key nameduser
to be present, which is present, otherwise error will be thrown. Next you have:.permit(:email, :password)
which permits these two params in parentuser
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 underuser
key, you must removerequire
from strong params onuser
key.