Tôi có một số xác nhận mật khẩu cho mô hình User
của tôi, và một phương pháp create_with_omniauth
để có được thông tin từ tài khoản Facebook của người dùng:Làm cách nào để bỏ qua xác thực mật khẩu trong phương thức đăng nhập OmniAuth sau đây?
user.rb:
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
user.email = auth["info"]["email"]
end
end
end
Bây giờ khi tôi bấm link_to "Sign in with Facebook, "auth/facebook"
tôi nhận được lỗi này:
Validation failed: Password can't be blank, Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
Vì hai dòng này trong số User
mô hình:
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
Làm cách nào để bỏ qua xác thực đó khi người dùng đang cố gắng đăng nhập bằng phương thức đăng nhập OmniAuth?
Xin cảm ơn các đề xuất! Xin lỗi, tôi là người mới bắt đầu Rails. Bạn có thể cho tôi một ví dụ về mã để tạo mật khẩu ngẫu nhiên khi người dùng nhấp vào đăng nhập Facebook không? – alexchenco
self.password = self.password_confirmation = SecureRandom.urlsafe_base64 (n = 6) –
Cảm ơn! Vì vậy, nên đi trong phương pháp 'self.create_with_omniauth'? Chẳng phải nó sẽ là 'user.password'? – alexchenco