$ vi Gemfile
...
gem 'devise','2.1.0'...
$ bundle install
create config/initializers/devise.rb
create config/locales/devise.en.yml
===============================================================================Some setup you must do manually if you haven't yet: 1. Ensure you have defined default url options in your environments files. Here is an example of default_url_options appropriate for a development environment in config/environments/development.rb: config.action_mailer.default_url_options = { :host => 'localhost:3000'} In production, :host should be set to the actual host of your application.
2. Ensure you have defined root_url to *something* in your config/routes.rb.
For example:
root :to => "home#index" 3. Ensure you have flash messages in app/views/layouts/application.html.erb.
For example:
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
4. If you are deploying Rails 3.1 on Heroku, you may want to set:
config.assets.initialize_on_precompile =falseOn config/application.rb forcing your application to not access the DB
or load models when precompiling your assets.
===============================================================================
classUserincludeMongoid::Document# Include default devise modules. Others available are:# :token_authenticatable, :confirmable,# :lockable, :timeoutable and :omniauthabledevise:database_authenticatable,:registerable,:recoverable,:rememberable,:trackable,:validatable## Database authenticatablefield:email,:type=>String,:null=>false,:default=>""field:encrypted_password,:type=>String,:null=>false,:default=>""## Recoverablefield:reset_password_token,:type=>Stringfield:reset_password_sent_at,:type=>Time## Rememberablefield:remember_created_at,:type=>Time## Trackablefield:sign_in_count,:type=>Integer,:default=>0field:current_sign_in_at,:type=>Timefield:last_sign_in_at,:type=>Timefield:current_sign_in_ip,:type=>Stringfield:last_sign_in_ip,:type=>String## Confirmable# field :confirmation_token, :type => String# field :confirmed_at, :type => Time# field :confirmation_sent_at, :type => Time# field :unconfirmed_email, :type => String # Only if using reconfirmable## Lockable# field :failed_attempts, :type => Integer, :default => 0 # Only if lock strategy is :failed_attempts# field :unlock_token, :type => String # Only if unlock strategy is :email or :both# field :locked_at, :type => Time## Token authenticatable# field :authentication_token, :type => Stringend
<div id="user_nav"><%ifuser_signed_in?%> Signed in as <strong><%=current_user.user_name%></strong>. Not you?<%=link_to"Sign out",destroy_user_session_path,:method=>:delete%><%else%><%=link_to"Sign up",new_user_registration_path%> or<%=link_to"Sign in",new_user_session_path%><%end%></div>
...# ==> OmniAuth# Add a new OmniAuth provider. Check the wiki for more information on setting# up on your models and hooks.# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'require"omniauth-facebook"config.omniauth:facebook,"APP_ID","APP_SECRET",:strategy_class=>OmniAuth::Strategies::Facebook...
其中”APP_ID”和”APP_SECRET”要去Facebook去申请。如果是本地测试,可以把Site URL设为http://localhost:3000/, Site Domain可设为localhost。
接下来把Sign in with Facebook的链接加到Sign up和Sign in的后面,这样点击这个链接就会把用户带到Facebook,如果用户成功登录Facebook,那Fackbook会把用户信息返回给开始设定好的Callback方法。
cat app/views/articles/index.html.erb
12345678910
<div id="user_nav"><%ifuser_signed_in?%> Signed in as <strong><%=current_user.user_name%></strong>. Not you?<%=link_to"Sign out",destroy_user_session_path,:method=>:delete%><%else%><%=link_to"Sign up",new_user_registration_path%> or<%=link_to"Sign in",new_user_session_path%><%=link_to"Sign in with Facebook",user_omniauth_authorize_path(:facebook)%><%end%></div>
classUsers::OmniauthCallbacksController<Devise::OmniauthCallbacksControllerdeffacebook# You need to implement the method below in your model@user=User.find_for_facebook_oauth(request.env["omniauth.auth"],current_user)if@user.persisted?flash[:notice]=I18n.t"devise.omniauth_callbacks.success",:kind=>"Facebook"sign_in_and_redirect@user,:event=>:authenticationelsesession["devise.facebook_data"]=request.env["omniauth.auth"]redirect_tonew_user_registration_urlendendend
defself.find_by_email(email)where(:email=>email).firstenddefself.find_for_facebook_oauth(access_token,signed_in_resource=nil)data=access_token.extra.raw_infoifuser=self.find_by_email(data.email)userelse# Create a user with a stub password.self.create!(:email=>data.email,:password=>Devise.friendly_token[0,20])endenddefself.new_with_session(params,session)super.tapdo|user|ifdata=session["devise.facebook_data"]&&session["devise.facebook_data"]["extra"]["raw_info"]user.email=data["email"]endendend
<% if can?:update,article%> <td><%= link_to 'Edit', edit_article_path(article) %></td><% end %><% if can? :destroy, article %> <td><%=link_to'Destroy',article,confirm:'Are you sure?',method::delete%></td><% end %>