mar*_*ion 9 twitter ruby-on-rails devise omniauth ruby-on-rails-5
我知道这已被多次询问,但答案对我来说永远不会完全接受.
因此,我正在关注Ryan Bates关于这个主题的Railscast,并将其与官方Devise Omniauth指南(基于FB)混合,但我只是没有像我期望的那样让它工作,所以我会喜欢一些帮助.
我有一个Users::OmniauthCallbacksController看起来像这样:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect root_path, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Twitter") if is_navigational_format?
else
session["devise.twitter_data"] = request.env["omniauth.auth"].except("extra")
flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages
redirect_to new_user_registration_url
end
end
alias_method :twitter, :all
def failure
redirect_to root_path
end
end
Run Code Online (Sandbox Code Playgroud)
然后我也有两种方法 User.rb
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.update(
email: auth.info.email,
password: Devise.friendly_token[0,20],
username: auth.info.nickname,
remote_avatar_url: auth.info.image,
token: auth.credentials.token,
secret: auth.credentials.secret
)
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.twitter_data"]
# user.attributes = params
user.update(
email: params[:email],
password: Devise.friendly_token[0,20],
username: data["info"]["nickname"],
remote_avatar_url: data["info"]["image"],
token: data["credentials"]["token"],
secret: data["credentials"]["secret"]
)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我遇到了各种各样的问题.最直接的是因为我设置了密码,用户在尝试登录时不知道密码(我确认时不会自动登录).
但是,如果我没有设置密码,它不会要求他们设置密码......所以这有点奇怪.
这些是我的User模型上的设计设置:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable, :omniauth_providers => [:twitter]
validates :username,
presence: true,
uniqueness: {
case_sensitive: false
}
validate :validate_username
def validate_username
if User.where(email: username).exists?
errors.add(:username, :invalid)
end
end
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,当有人通过Twitter注册时,他们是否需要输入密码?我会自动发送它们,registration/new.html.erb因为Twitter没有返回电子邮件值.但我想在优化之前先让流程先运行.
我该如何处理密码问题?
编辑1
为了更清楚起见,password_required无论OAuth提供商如何,我都必须处理此问题.
那么如何覆盖所有OAuth提供商的要求呢?
您应该在类中添加以下方法User:
def password_required?
(provider.blank? || uid.blank?) && super
end
Run Code Online (Sandbox Code Playgroud)
由于Twitter不会返回用户的电子邮件,您可能还想调整该电子邮件验证,但将用户重定向到registration/new.html.erb您已经在做的事情似乎对我来说是正确的方法.
| 归档时间: |
|
| 查看次数: |
1565 次 |
| 最近记录: |