Sha256: 52a95d7729768766bf608a4b490419c9e5391d1e095aa1d0451dd87f43502431

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

module LsOmniauth
  class OmniauthController < ActionController::Base
    include Rails.application.routes.url_helpers

    before_filter :set_return_uri

    def authenticate
      auth_response = request.env['omniauth.auth']
      email = auth_response.info.email

      LsOmniauth::AuthSessions.new(session).tap {|session|
        session.auth.set(email)
        origin = session.origin.getOrElse("/")
        Rails.logger.warn "origin = #{origin}"
        session.origin.clear
        redirect_to origin
      }
    end

    def new_authentication
      LsOmniauth::AuthSessions.new(session).origin.set(params[:return_uri]) if params[:return_uri]
      LsOmniauth::AuthSessions.new(session).auth.clear
      url = oauth_url(params)
      redirect_to url
    end

    def revoke_authentication
      return_uri = return_uri(session)
      clear_authentication_sessions(session)
      render :text => "<div class='notification'>your have been logged out click <a href='#{ls_omniauth.sign_in_url}?return_uri=#{return_uri}' target='_self'>here</a> to log back in</div>"
    end

    private
    def set_return_uri
      session[:return_uri] = request.referrer if (request.referrer !~ /sign_out/)
    end

    def return_uri(session)
      session.delete(:return_uri) || (defined?(root_url) && root_url) || '/'
    end

    def clear_authentication_sessions(session)
      LsOmniauth::AuthSessions.new(session).tap {|s|
        s.auth.clear
        s.origin.clear
      }
    end

    def oauth_url(params)
      params = params.dup
      provider = params.delete(:provider) || 'google_oauth2'
      url = route_for_provider(provider)
      params.empty? ? url : "#{url}?#{params.to_query}"
    end

    def route_for_provider(provider)
      "/auth/#{provider}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ls-omniauth-3.0.3 app/controllers/ls_omniauth/omniauth_controller.rb