Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching between web and touch interfaces on Facebook login using Omniauth and Rails 3

The situation:

Using Rails 3 and OmniAuth, I have an app that authenticates using the Facebook strategy. This app has been built to work equally-well for web and mobile interfaces (ala Jquery-Mobile).

The challenge is to get OmniAuth to provide the mobile version of Facebook's login screen to mobile devices and the web version to desktop devices.

I've hacked together a solution which I'll put as an answer.

like image 557
bengreene Avatar asked Apr 01 '11 19:04

bengreene


1 Answers

Actually, since OmniAuth::Strategies are already Rack middleware, its even simpler. Just override the request_phase method and check the @env instance variable present in the strategy for a mobile user_agent:

module OmniAuth
  module Strategies
    class Facebook < OAuth2

      MOBILE_USER_AGENTS =  'webos|ipod|iphone|mobile'

      def request_phase
        options[:scope] ||= "email,offline_access"
        options[:display] = mobile_request? ? 'touch' : 'page'
        super
      end

      def mobile_request?
        ua = Rack::Request.new(@env).user_agent.to_s
        ua.downcase =~ Regexp.new(MOBILE_USER_AGENTS)
      end

    end
  end
end
like image 92
Evan Owen Avatar answered Sep 19 '22 03:09

Evan Owen