RailsCasts Pro episodes are now free!

Learn more or hide this

Mamadou Toure's Profile

GitHub User: mamadoutoure

Comments by Mamadou Toure

Avatar

How can the else case here would not fail, if we dont find and authentication with the provided params, and as there is no logged in user in this case so the current_user is Nil, so it should fail with Nil class exception when trying to call current_user.authentications.create(....)

ruby
def create
  omniauth = request.env["omniauth.auth"]
  authentication = Authentication.find_by_provder_and_uid(omniauth['provider'], omniauth['uid'])
  if authentication
    flash[:notice] = "Signed in successfully."
    sign_in_and_redirect(:user, authentication.user)
  else
    current_user.authentications.create(:provider => omniauth['provider'], :uid => omniauth['uid'])
    flash[:notice] = "Authentication successful."
    redirect_to authentications_url
  end
end