We are new to rails and are looking to use the Javascript SDK “Connect with Facebook” button on our homepage. Using this button in our app, we want to allow users to sign up for our site via Facebook, and be able to use their Facebook profile picture as their profile image for our web app.
What is the best way to implement this Facebook Connection with our Rails 3 app?
devise_for :users
resources :authentications
resources :users do
member do
get :following, :followers
end
end
resources :sessions, :only => [:new, :create, :destroy]
resources :microposts, :only => [:create, :destroy]
resources :relationships, :only => [:create, :destroy]
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/contact', :to => 'pages#contact'
match '/home', :to => 'pages#home'
match '/help', :to => 'pages#help'
match '/feedback', :to => 'pages#feedback'
match '/privacy', :to => 'pages#privacy'
match '/terms', :to => 'pages#terms'
match '/', :to => 'pages#home'
resources :microposts
resources :users
resources :sessions, :only => [:new, :create, :destroy]
root :to => 'pages#home'
match "/auth/twitter/callback" => "sessions#omnicreate"
match "/auth/facebook/callback" => "sessions#omnicreate"
end
SessionsController
class SessionsController < ApplicationController
def new
@title = "Sign in"
end
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
sign_in user
redirect_back_or user
end
end
def destroy
sign_out
redirect_to root_path
end
end
Looks like you need to do a couple things:
Change these lines in your routes file:
To this:
Both Twitter and Facbook are going to the same method so you will need to get that
:providerparam out later on to figure out if its Twitter or Facebook that sent them.Next you need to create a method in your
SessionsControllerthat receives the callback like this:good luck!