Created
November 26, 2011 22:35
-
-
Save JakubOboza/1396422 to your computer and use it in GitHub Desktop.
facebook authentication with Rails 3.1 and OmniAuth 1.0 and Devise 1.5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <body> | |
| <div id="fb-root"></div> | |
| <script src="http://connect.facebook.net/en_US/all.js"> | |
| </script> | |
| <script type="text/javascript"> | |
| FB.init({ | |
| appId: "271615169549029", | |
| cookie: true, | |
| status: true, | |
| xfbml: true | |
| }); | |
| </script> | |
| <p class="notice"><%= notice %></p> | |
| <p class="alert"><%= alert %></p> | |
| <% if current_user -%> | |
| <%= link_to_function "logout [#{current_user.name}]", 'App.logout()' %> | |
| <% else -%> | |
| <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %> | |
| <% end %> | |
| <%= yield %> | |
| </body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rails generate devise:install | |
| rails generate devise User | |
| rails generate rspec:install | |
| rails generate cucumber:install --capybara --rspec |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| config.sign_out_via = :get | |
| config.omniauth :facebook, 'APP_ID', 'APP_SECRET' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #spec/support/devise_helpers.rb | |
| RSpec.configure do |config| | |
| config.include Devise::TestHelpers, :type => :controller | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gem 'devise', '1.5.0' | |
| gem 'omniauth-facebook', :git => "git://github.com/mkdynamic/omniauth-facebook.git" | |
| gem 'rspec-rails', :group => [:development, :test] | |
| group :test do | |
| # Pretty printed test output | |
| gem 'turn', '< 0.8.3', :require => false | |
| gem 'cucumber-rails' | |
| gem 'capybara' | |
| gem 'database_cleaner' | |
| gem 'factory_girl_rails' | |
| gem 'faker' | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Place all the behaviors and hooks related to the matching controller here. | |
| # All this logic will automatically be available in application.js. | |
| # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ | |
| @App = | |
| subscribeToFBSessionChange: -> | |
| FB.Event.subscribe('auth.sessionChange', (response) -> | |
| if response.session | |
| # A user has logged in, and a new cookie has been saved | |
| else | |
| # The user has logged out, and the cookie has been cleared | |
| App.clearSession() | |
| ) | |
| clearSession: -> | |
| window.location = '/users/sign_out' | |
| logout: -> | |
| FB.getLoginStatus((response) -> | |
| FB.logout() if response.status == 'connected' | |
| ) | |
| App.clearSession() | |
| $ -> | |
| # Initialization code goes here | |
| App.subscribeToFBSessionChange() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class OmniauthCallbacksController < Devise::OmniauthCallbacksController | |
| def facebook | |
| auth = request.env["omniauth.auth"] | |
| user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth) | |
| flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook" | |
| sign_in_and_redirect user, :event => :authentication | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User < ActiveRecord::Base | |
| # Include default devise modules. Others available are: | |
| # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable | |
| devise :database_authenticatable, :omniauthable | |
| # Setup accessible (or protected) attributes for your model | |
| attr_accessible :email, :password, :password_confirmation, :remember_me | |
| def self.create_with_omniauth(auth) | |
| create! do |user| | |
| user.provider = auth["provider"] | |
| user.uid = auth["uid"] | |
| user.name = auth["info"]["name"] | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CreateUsers < ActiveRecord::Migration | |
| def change | |
| create_table :users do |t| | |
| t.string :provider, :null => false | |
| t.string :uid, :null => false | |
| t.string :name, :null => false | |
| t.database_authenticatable | |
| t.timestamps | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment