1

I am developing rails 4 application.

I required two facebook buttons on index page where 1 for facebook sign up and other for facebook login.

Scenario 1: If user click on FB sign up button, the user register's using omniauth and gets redirected to a form for further signup details as per the project flow.

Scenario 2: If user click on FB login button, User is authenticated through omniauth and is redirected to home page

I can manage 1 button using omniauth but how to manage second one. How i know which button user click.

Any way to i can pass any parameter to facebook and FB return it when response us.

Any thing else

Thanks

Saurabh
  • 1,086
  • 1
  • 15
  • 27
harsh4u
  • 2,550
  • 4
  • 24
  • 39

2 Answers2

1

Yes, I got solutions for this particualar question.

I pass params in facebook.js.coffee.erb

jQuery -> $('body').prepend('')

$.ajax url: "#{window.location.protocol}//connect.facebook.net/en_US/all.js" dataType: 'script' cache: true

window.fbAsyncInit = -> FB.init(appId: '<%= ENV["FACEBOOK_APP_ID"] %>', cookie: true)

 $('#sign_up').click (e) ->
    e.preventDefault()
    FB.login (response) ->
      window.location = '/auth/facebook/callback?UserClk=signup' if response.authResponse

  $('#sign_in').click (e) ->
    e.preventDefault()
    FB.login (response) ->
      window.location = '/auth/facebook/callback?UserClk=signin' if response.authResponse

$('#sign_out').click (e) -> FB.getLoginStatus (response) -> FB.logout() if response.authResponse true

You can check on above code which i highlight.

For 2 buttons i set 2 ids where sign_in & sign_out

Pass just params in callback url which i got when response back from facebook.

Now you can do your process using params in controller.

Many thanks to all whose try for it.

Thanks

harsh4u
  • 2,550
  • 4
  • 24
  • 39
0

Facebook provides only one callback URL. So no matter what how many buttons you have, you will get the redirection to one specific action. And also you don't need two separate buttons though. You could have the sign-in and sign-up done through Facebook with one button. When you get the callback you could check the email attribute in the request hash and find out if the user is registered user or a new user and act with your logic accordingly. Here's a sample code. The authorisation table is used to store Facebook details.

auth_hash = request.env['omniauth.auth']
@authorization = Authorization.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"])
if @authorization
  @authorization.token = auth_hash['credentials']['token']
  @authorization.save!
  sign_in_and_redirect User.find(@authorization.user_id)
  #render :text => "Welcome back #{@authorization.user.name}! You have already signed up."
else
  @user = User.find_by_email(auth_hash["info"]["email"])
  if @user
    @user.authorizations.create!(:provider => auth_hash['provider'], :uid => auth_hash['uid'], :token => auth_hash['credentials']['token'])
    @user.save
  else
    @user = User.new :first_name => auth_hash["info"]["first_name"], :last_name => auth_hash["info"]["last_name"], :email => auth_hash["info"]["email"] , :fb_profile_url => auth_hash["info"]["image"], :validated => true
    @user.authorizations.build :provider => auth_hash["provider"], :uid => auth_hash["uid"], :token => auth_hash['credentials']['token']
    @user.save
  end
  sign_in_and_redirect @user
end
Saurabh
  • 1,086
  • 1
  • 15
  • 27
  • Yes code is right but How i can identifier that user click on which button. Because when user click on FB login in button then only want to check that user register through FB or not. Not want to register when click on sign in button. User register only when they click on FB sign up button if not exists. Make sense. – harsh4u Sep 26 '14 at 08:41
  • Do you really want two buttons when you can do the registration as well as Sign-in using a single button. You can check out popular sites which provides fb sign-in and they don't have a different buttons. – Saurabh Sep 26 '14 at 08:48
  • Yes but its a client requirements. – harsh4u Sep 26 '14 at 09:53
  • May be its possible through pass parameter and get that back and identify on which button user did a click – harsh4u Sep 26 '14 at 09:54
  • I don't think you can pass parameters with the Facebook oauth link. The link takes you outside your domain and after authentications brings you back. – Saurabh Sep 26 '14 at 10:03
  • Check it: http://stackoverflow.com/questions/9890985/passing-parameters-through-omniauth – harsh4u Sep 26 '14 at 10:05
  • Great!! I did not know this. Hopefully should solve your problem :-) – Saurabh Sep 26 '14 at 10:08