3

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...

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189
  • 1
    I think you know this, this is the documentation about facebook-omniauth. I have not found any info about this on it, but maybe on facebook if you search oauth you can find some more info https://github.com/mkdynamic/omniauth-facebook – Fabrizio Bertoglio Mar 09 '17 at 08:04
  • 1
    Thanks @FabrizioBertoglio, I think I have found a solution, just about to write the answer. – stephenmurdoch Mar 09 '17 at 08:07

1 Answers1

1

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.

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189
  • 1
    I used the same trick to improve the flow of authentication with a popup :D http://stackoverflow.com/questions/4491433/turn-omniauth-facebook-login-into-a-popup/9592204#9592204 – Ashitaka Mar 09 '17 at 10:49
  • 1
    Don't forget to do the same for your failure action, which is called when the user does not accept logging in. – Ashitaka Mar 09 '17 at 10:51