Devise Oauth可以工作,但不包含任何数据

时间:2019-12-09 02:07:56

标签: ruby-on-rails devise omniauth

我让Google,Facbook和Github作为Devise和Omniauth的登录方法。

用户登录后,会将他们带到提供者确认页面,然后按预期的方向进行重定向。问题是,如果他们仅编辑电子邮件地址就去编辑其个人资料。他们甚至无法添加自己的名字或其他任何东西,因为devise需要输入密码才能进行更改,并且由于他们已与提供者注册,因此没有密码可用于更新个人资料。

我的omniauth控制器

class OmniauthController < ApplicationController


  def facebook
  @user = User.create_from_provider_data(request.env['omniauth.auth'])
  if @user.persisted?
    sign_in_and_redirect @user
  else
    flash[:error] = 'There was a problem signing you in through Facebook. Please register or try signing in later.'
    redirect_to new_user_registration_url
  end
end

def github
  @user = User.create_from_provider_data(request.env['omniauth.auth'])
  if @user.persisted?
    sign_in_and_redirect @user
  else
    flash[:error] = 'There was a problem signing you in through Github. Please register or try signing in later.'
    redirect_to new_user_registration_url
  end
end

def google_oauth2
  @user = User.create_from_provider_data(request.env['omniauth.auth'])
  if @user.persisted?
    sign_in_and_redirect @user
  else
    flash[:error] = 'There was a problem signing you in through Google. Please register or try signing in later.'
    redirect_to new_user_registration_url
  end
end

def failure
  flash[:error] = 'There was a problem signing you in. Please register or try signing in later.'
  redirect_to new_user_registration_url
end


end

用户模型

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :omniauthable, omniauth_providers: [:facebook, :github, :google_oauth2]


has_one_attached :avatar
has_many :newsletters
has_many :articles, through: :newsletters
has_many :events, through: :articles

def self.create_from_provider_data(provider_data)
    where(provider: provider_data.provider, uid: provider_data.uid).first_or_create do | user |
      user.first_name = provider_data.info.first_name
      user.last_name = provider_data.info.last_name
      user.email = provider_data.info.email
      user.password = Devise.friendly_token[0, 20]
    end
  end

  def full_name
    @full_name = "#{self.first_name} #{self.last_name}"
  end

  def initials
     @initials = "#{self.first_name[0]}.#{self.last_name[0]}."
  end

  def print_name
    @print_name = "#{self.first_name[0]}. #{self.last_name}"
  end
end

和在我的devise.rb

config.omniauth :facebook, Rails.application.credentials.dig(:facebook, :facebook_client_id),
  Rails.application.credentials.dig(:facebook, :facebook_client_secret), scope: 'public_profile,email', info_fields: 'email,first_name,last_name,picture'
  config.omniauth :github, Rails.application.credentials.dig(:github, :github_client_id),
  Rails.application.credentials.dig(:github, :github_client_secret), scope: 'user,public_repo'
  config.omniauth :google_oauth2, Rails.application.credentials.dig(:google, :google_client_id),
  Rails.application.credentials.dig(:google, :google_client_secret), scope: 'userinfo.email,userinfo.profile'

所以我想我需要知道为什么我没有得到用户名之类的东西(我确实在他们的计划中),或者如何使之如此,以便他们不需要密码来更新其个人资料。

0 个答案:

没有答案