我用devise來允許用戶在站點內註冊和登錄。我也使用omniauth-facebook允許用戶使用他們的Facebook帳戶登錄。但是,首先使用Facebook電子郵件註冊同一電子郵件的人不能使用他們的Facebook帳戶登錄。我想同時使用Facebook登錄和網站註冊。如何允許用戶使用註冊賬戶和Facebook賬戶登錄?
這是我user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.where(:provider => auth.provider, :uid => auth.uid).first
unless user
user = User.create(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
password:Devise.friendly_token[0,20]
)
end
user
end
end
而且omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #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"]
redirect_to new_user_registration_url
end
end
end
你想同時使用facebook登錄以及設計(電子郵件/通行證)還是隻有其中一個?我可以基於此回答你的問題 - 謝謝 –
@matrixtheone我想同時使用Facebook登錄和設計註冊。謝謝! – achilleo