module IntercomApp::LoginProtection

Public Instance Methods

app_session() click to toggle source
# File lib/intercom-app/login_protection.rb, line 19
def app_session
  return unless session[:intercom]
  @app_session ||= IntercomApp::SessionRepository.retrieve(session[:intercom])
end
intercom_session() { || ... } click to toggle source

included do

rescue_from ActiveResource::UnauthorizedAccess, :with => :close_session

end

# File lib/intercom-app/login_protection.rb, line 9
def intercom_session
  if app_session
    begin
      yield
    end
  else
    redirect_to_login
  end
end

Protected Instance Methods

close_session() click to toggle source
# File lib/intercom-app/login_protection.rb, line 39
def close_session
  session[:intercom] = nil
  session[:intercom_app_id] = nil
  redirect_to_with_fallback main_or_engine_login_url
end
fullpage_redirect_to(url) click to toggle source
# File lib/intercom-app/login_protection.rb, line 71
def fullpage_redirect_to(url)
  url_json = url.to_json
  url_json_no_quotes = url_json.gsub(/\A"|"\Z/, '')
  redirect_to_with_fallback url
end
intercom_client() click to toggle source
# File lib/intercom-app/login_protection.rb, line 26
def intercom_client
  @intercom_client = Intercom::Client.new(token: app_session[:intercom_token])
end
main_or_engine_login_url(params = {}) click to toggle source
# File lib/intercom-app/login_protection.rb, line 45
def main_or_engine_login_url(params = {})
  main_app.login_url(params)
rescue NoMethodError => e
  intercom_app.login_url(params)
end
redirect_to_login() click to toggle source
# File lib/intercom-app/login_protection.rb, line 30
def redirect_to_login
  if request.xhr?
    head :unauthorized
  else
    session[:return_to] = request.fullpath if request.get?
    redirect_to_with_fallback main_or_engine_login_url
  end
end
redirect_to_with_fallback(url) click to toggle source
# File lib/intercom-app/login_protection.rb, line 51
def redirect_to_with_fallback(url)
  url_json = url.to_json
  url_json_no_quotes = url_json.gsub(/\A"|"\Z/, '')

  render inline: %Q(
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <title>Redirecting…</title>
        <script type="text/javascript">
          window.location.href = #{url_json};
        </script>
      </head>
      <body>
      </body>
    </html>
  ), status: 302, location: url
end