Rails: Google authentication with Authlogic

267 views Asked by At

I'm using Authlogic and I would like to implement google authentication into the existing project along with Authlogic. I could not find any good sources. I tried with openid and authlogic-oauth, they do not seem working. Is there any alternative gems/methods that would work with Authlogic for Google authentication? Thank you

1

There are 1 answers

0
Rust Gold On BEST ANSWER

I'm bit late to post answer for this. Here, we have to find that specific e-mail was presented or not in omniauth info. If it is present, then follow normal steps that we followed for authlogic authentication. This is little tricky but easy. For this we don't need any third party library. So we can complete this with following gem omniauth-google-oauth2 and authlogic

 class AuthenticationsController < ApplicationController
     def create
       omniauth = request.env['omniauth.auth']
       if omniauth["provider"] == "google_oauth2" 
         user = User.find_by_email(email)
         @user_session = UserSession.new(user)
         if @user_session.save
           flash[:success] = "Login successful!"
           redirect_to session[:return_to] ||= markets_url
         else
           flash[:error] = "Your Account doesn't exists. Please contact 
                            Xxxxx support."
           redirect_to :login
         end
       end
     end