我需要将跳过验证方法添加到我的会话控制器,但我不确定将omniauth定义为和基本登录为什么。即他们都来自班级用户,但在会话控制器中,我把
def create
user = User.from_omniauth(env["omniauth.auth"])
**user.from_omniauth.skip_name_validation = true**
etc etc
然后还要为模型中的方法添加验证,以粗体显示,并将其添加到我的会话控制器,以便能够绕过每个登录的两个实例。
我的会话控制器
def create
user = User.from_omniauth(env["omniauth.auth"])
unless user.present?
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to user
# Log the user in and redirect to the user's show page.
else
# Create an error message.
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
else
log_in user
redirect_to user
end
end
我的模特.rb
class User < ApplicationRecord
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 },
**unless: :skip_name_validation**
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false },
**unless: :skip_email_validation**
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil:true,
**unless: :skip_password_validation**
**attr_accessor :skip_name_validation, :skip_email_validation, :skip_password_validation**
**attr_accessor :skip_User, :skip_self**
**validate :User, unless: :skip_User**
**validate :self, unless: :skip_self**
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
end
答案 0 :(得分:0)
class User < ApplicationRecord
attr_accessor :password_confirmation, :skip_password_validation
validates :password, :presence => true ,unless: :skip_password_validation
validates_confirmation_of :password, :if => ->{ password.present? },unless: :skip_password_validation
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
end
def create
user = User.from_omniauth(env["omniauth.auth"])
#######try this #####
user.skip_password_validation = true
####or try this ####
user.from_omniauth.skip_name_validation = true
unless user.present?
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to user
# Log the user in and redirect to the user's show page.
else
# Create an error message.
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
else
log_in user
redirect_to user
end
end