I am using the omniauth-stripe-connect
gem and get the following error when authenticating users:
NoMethodError in Users::OmniauthCallbacksController#stripe_connect undefined method `persisted?' for nil:NilClass
My Callbacks Controller Action:
def stripe_connect
@user = UserProvider.find_for_stripe_oauth(request.env["omniauth.auth"])
if @user.persisted? # here is the issue !!!
sign_in @user, :event => :authentication
redirect_to root_path
end
end
And my User Method:
def self.find_for_stripe_oauth(auth)
user = UserProvider.where(:provider => auth.provider, :uid => auth.uid).first
unless user.nil?
user.user
else
registered_user = User.where(:email => auth.info.email).first
unless registered_user.nil?
user_provider = UserProvider.new
user_provider.provider = auth.provider
user_provider.uid = auth.uid
user_provider.user_id = registered_user.id
user_provider.save!
registered_user.stripe_access_code = auth.credentials.token
registered_user.stripe_publishable_key = auth.info.stripe_publishable_key
registered_user.stripe_id = auth.extra.raw_info.stripe_user_id
registered_user.skip_confirmation!
registered_user.save!
registered_user
end
end
end
What am I doing wrong?
find_for_stripe_oauth
returns nil
if registered_user
is not exists. In this case unless registered_user.nil?
returns false
and inner block is not executed.
Move out registered_user
, like this:
registered_user = User.where(:email => auth.info.email).first
unless registered_user.nil?
user_provider = UserProvider.new
user_provider.provider = auth.provider
user_provider.uid = auth.uid
user_provider.user_id = registered_user.id
user_provider.save!
registered_user.stripe_access_code = auth.credentials.token
registered_user.stripe_publishable_key = auth.info.stripe_publishable_key
registered_user.stripe_id = auth.extra.raw_info.stripe_user_id
registered_user.skip_confirmation!
registered_user.save!
end
registered_user