您好,我不确定这个有什么问题。
我尝试使用设计在我的应用程序上实现 Omniauth-facebook。
在这里我看到了指南 https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
尝试之后。它只是重定向到 sign_up url。
这里是我的代码和设计。
学生.rb
class Student < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:facebook]
attr_accessor :login
validates :username,
:uniqueness => {
:case_sensitive => false
},
:format => { with: /\A[A-Za-z][A-Za-z0-9._-]{2,19}\z/ }
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |student|
student.provider = auth.provider
student.uid = auth.uid
student.username = auth.info.uid
student.email = auth.info.email
student.firstname = auth.info.firstname
student.lastname = auth.info.lastname
student.password = Devise.friendly_token[0,20]
end
end
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value",{ :value => login.downcase }]).first
else
where(conditions).first
end
end
end
学生/omniauth_callbacks_controller.rb
class Students::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@student = Student.from_omniauth(request.env["omniauth.auth"])
if @student.persisted?
sign_in_and_redirect @student, :event => :authentication
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_student_registration_url
end
end
end
路线.rb
devise_for :students, :controllers => { :omniauth_callbacks => "students/omniauth_callbacks" }
终端日志
Started GET "/students/auth/facebook" for 127.0.0.1 at 2014-07-13 12:49:38 +0800
I, [2014-07-13T12:49:38.289478 #18659] INFO -- omniauth: (facebook) Request phase initiated.
Started GET "/students/auth/facebook/callback?code=AQAu8HkwMJyqNZRUJ7vrD9U9Pn6MtWhNu7SYcNXiGenslAqEfzdKHKR2bfYSqRjYe3NXjWEdW4zGEIfTo_J-mpqRUfZRkN9_t2mJmn2PAqzXYPICiroyhWo2shjI2c9YzKs-0mT-ZZ7Das1WfoxQccd4_MOlwA7boi2vyxk-XLzFogDgM_1kVr58rRnA-6aSi4cdXGNdk0eM9Ic4qPa3RKpz3LFSGjLljpzsD_5RY7X8xYlbyTHIXJYg7cx0R6mAMdC4483Oyf5kAkmSyhNSRnO3rrlqGGUwG3iYMNMg4r02IUzW0MJip7Y1HJYnth7pv4M&state=4582701b7f51f315b76c3efb2354e1f1e90eaec47b5294a1" for 127.0.0.1 at 2014-07-13 12:49:38 +0800
I, [2014-07-13T12:49:38.719705 #18659] INFO -- omniauth: (facebook) Callback phase initiated.
Processing by Students::OmniauthCallbacksController#facebook as HTML
Parameters: {"code"=>"AQAu8HkwMJyqNZRUJ7vrD9U9Pn6MtWhNu7SYcNXiGenslAqEfzdKHKR2bfYSqRjYe3NXjWEdW4zGEIfTo_J-mpqRUfZRkN9_t2mJmn2PAqzXYPICiroyhWo2shjI2c9YzKs-0mT-ZZ7Das1WfoxQccd4_MOlwA7boi2vyxk-XLzFogDgM_1kVr58rRnA-6aSi4cdXGNdk0eM9Ic4qPa3RKpz3LFSGjLljpzsD_5RY7X8xYlbyTHIXJYg7cx0R6mAMdC4483Oyf5kAkmSyhNSRnO3rrlqGGUwG3iYMNMg4r02IUzW0MJip7Y1HJYnth7pv4M", "state"=>"4582701b7f51f315b76c3efb2354e1f1e90eaec47b5294a1"}
Student Load (0.7ms) SELECT "students".* FROM "students" WHERE "students"."provider" = 'facebook' AND "students"."uid" = '10204532053849746' ORDER BY "students"."id" ASC LIMIT 1
(0.2ms) BEGIN
Student Exists (0.4ms) SELECT 1 AS one FROM "students" WHERE "students"."username" IS NULL LIMIT 1
(0.2ms) ROLLBACK
Redirected to http://localhost:3000/students/sign_up
Completed 302 Found in 81ms (ActiveRecord: 1.5ms)
Started GET "/students/sign_up" for 127.0.0.1 at 2014-07-13 12:49:40 +0800
Processing by Devise::RegistrationsController#new as HTML
Rendered devise/shared/_links.erb (0.7ms)
Rendered devise/registrations/new.html.erb within layouts/application (6.0ms)
Rendered layouts/_notification.html.erb (0.1ms)
Rendered layouts/_header.html.erb (1.2ms)
Completed 200 OK in 44ms (Views: 42.6ms | ActiveRecord: 0.0ms)
知道如何解决这个问题。在 Rails 控制台上,具有不同详细信息的用户名都可以创建新的。