我在https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview上关注了这个指南点
第一轮它对我进行了身份验证,让我登录,并将我保存到 db。
我清除了浏览器历史记录并测试了另一个用户。
它需要新用户到 facebook 页面登录,登录后自动将他们重定向到
http://localhost:3000/users/sign_up#_=_
并且不会将它们登录或保存到数据库。有任何想法吗?
路线
get "static/home"
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
resources :users
root 'static#home'
用户.rb
class User < ActiveRecord::Base
attr_accessible :name, :provider, :uid
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :timeoutable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable,
:omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
#user.image = auth.info.image # assuming the user model has an image
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
protected
def password_required?
true
end
end
设计.rb
config.omniauth :facebook, "#######", "################"
omniauth_callbacks
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"].except('extra')
redirect_to new_user_registration_url
end
end
end
主页.html.erb
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>