Search code examples
ruby-on-railsomniauthomniauth-facebook

Indicating intent with omniauth facebook


I'm using the omniauth-facebook gem and would like to pass Facebook a string indicating whether the user is trying to join or login, so that I can use the information when they return to my site.

It would be great if I could do something like this:

= link_to 'Join with facebook',  '/auth/facebook?intent=join'
= link_to 'Login with facebook', '/auth/facebook?intent=login'

In case anyone wants to know why I want to do this, here's what I plan to do with the information:

# pseudo code
- if authentication fails
  - if user was trying to join using facebook
    = return them to the signup url 
  - elsif user was trying to login using facebook
    = return them to the login url

So is it possible to signal my intent to facebook, and have them return that intent with their response so that I can use it if I need to?

I'm not using Devise, and would prefer to avoid having to set session variables to do this, Facebook must surely have a solution for this need...


Solution

  • Looks like this is possible and my code does work. When I inspect the response from facebook, I see the following:

    >> request.env['omniauth.auth']['intent']
    => nil 
    # no dice
    
    >> params
    => returns lots of stuff, but not the thing I want
    # worth a try
    
    >>  request.env['omniauth.params']
    => {"intent"=>"login"}
    # bingo
    

    So the params hash that rails puts together does not contain the desired info, but the response does contain a bunch of unsigned details, and this is where the information I want can be found.

    Worth noting you can also call:

    >> request.env['omniauth.origin']
    => "http://localhost:3000/join"
    

    Pretty cool.