user1502223
user1502223

Reputation: 645

omniauth without devise rails cast 236

Hey im trying to integrate omniauth into my application but i am not using devise. Im following Ryan Bates screen cast OmniAuth Part 2 #236 and he assumes everyone is using devise. In the authentication_controller.rb there is a code that is devise specific

   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

its the sign_in_and_redirect

when i refresh my page i get a

 undefined method `sign_in_and_redirect'

does anyone know a work around for this...im pretty new to rails so step by step would be ideal.

Thanks guys also if anyone knows of a good tutorial that covers integrating OmniAuth without DEVISE would be great too.

Upvotes: 1

Views: 3017

Answers (1)

Andrew Kuklewicz
Andrew Kuklewicz

Reputation: 10701

I would recommend looking at these rails/ascii cast:

http://railscasts.com/episodes/241-simple-omniauth

http://asciicasts.com/episodes/304-omniauth-identity

sign_in_and_redirect is going to set the current user in session, and whatever else you want to do on sign in, then redirect to the homepage, or whatever you set as the page after successful login.

Instead, do that yourself here, like this perhaps:

  def create
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    if authentication
      flash[:notice] = "Signed in successfully."
      session[:user_id] = authentication.user.id  
      redirect_to root_url, notice: "Signed in!"
    end
  end  

Then, in application controller, something like:

def current_user
    User.find(session[:user_id]) if logged_in?
end

def logged_in?
    !!session[:user_id]
end

Upvotes: 2

Related Questions