Я использую рельсы литой omniauth и я получаю эту ошибку
Я использую Mongodb в качестве базы данных в рельсах, и я получил ошибку при использовании / auth / linkedin / callback
NoMethodError в AuthenticationsController # создать неопределенный метод [] 'для nil: NilClass Rails.root: / home / prem / Music / heronhrm Трассировка приложения | Framework Trace | Full Trace app / models / user.rb: 57: в apply_omniauth 'app / controllers / authentications_controller.rb: 19: в `create'
Также, когда я удаляюself.email = omniauth['user_info']['email'] if email.blank?
от usermodel, то ошибки проверки возникают в
users / sign_up E-mail не может быть пустым Я хочу реализовать для Twitter, LinkDin и Facebook.
моя аутентификация.рб
class Authentication
include Mongoid::Document
belongs_to :user
field :user_id, :type => String
field :provider, :type => String
field :uid, :type => String
def self.find_by_provider_and_uid(provider, uid)
where(provider: provider, uid: uid).first
end
end
моя модель пользователя такая
def apply_omniauth(omniauth)
self.email = omniauth['user_info']['email'] if email.blank?
authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
end
def password_required?
(authentications.empty? || !password.blank?) && super
end
Мой контроллер аутентификации похож на этот
class AuthenticationsController < ApplicationController
def index
@authentications = current_user.authentications if current_user
end
def create
omniauth = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if authentication
flash[:notice] = "Signed in successfully."
sign_in_and_redirect(:user, authentication.user)
elsif current_user
current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
flash[:notice] = "Authentication successful."
redirect_to authentications_url
else
user = User.new
user.apply_omniauth(omniauth)
if user.save
flash[:notice] = "Signed in successfully."
sign_in_and_redirect(:user, user)
else
session[:omniauth] = omniauth.except('extra')
redirect_to new_user_registration_url
end
end
end
def destroy
@authentication = current_user.authentications.find(params[:id])
@authentication.destroy
flash[:notice] = "Successfully destroyed authentication."
redirect_to authentications_url
end
protected
# This is necessary since Rails 3.0.4
# See https://github.com/intridea/omniauth/issues/185
# and http://www.arailsdemo.com/posts/44
def handle_unverified_request
true
end
end
Мой контроллер регистрации такой
class RegistrationsController < Devise::RegistrationsController
def create
super
session[:omniauth] = nil unless @user.new_record?
end
private
def build_resource(*args)
super
if session[:omniauth]
@user.apply_omniauth(session[:omniauth])
@user.valid?
end
end
end