简体   繁体   English

Rails设计omniauth Facebook

[英]Rails devise omniauth facebook

I can't get devise omniauth-facebook to work. 我无法设计omniauth-facebook来工作。 I've followed the devise guide to no avail. 我没有遵循设计指南。 I think my problem is the model is not retrieving info. 我认为我的问题是模型没有检索信息。

Model 模型

class User < ActiveRecord::Base
  has_attached_file :image, styles: {large: "1920x1080#", medium:              "800x500#", thumb: "100x100"}, :default_url =>    "/images/:style/missing.png"
  validates_attachment_content_type :image, :content_type =>    ["image/jpg", "image/jpeg", "image/png", "image/gif"]

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable,
     :omniauthable, :omniauth_providers => [:facebook]
  def admin?
    admin
  end

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do     |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      user.name = auth.info.name   # assuming the user model has a     name
      user.image = auth.info.image # assuming the user model has an image
      puts request.env["omniauth.auth"]
    end
  end

  def self.new_with_session(params, session)
    super.tap do |user|
      if data = session["devise.facebook_data"] &&     session["devise.facebook_data"]["extra"]["raw_info"]
        user.email = data["email"] if user.email.blank?
      end
    end
  end
end

Routes.rb 的routes.rb

Rails.application.routes.draw do

  mount RailsAdmin::Engine => '/admin', as: 'rails_admin'

  devise_for :users do
    delete 'logout' => 'sessions#destroy', :as => :destroy_user_session,
    :controllers => { :omniauth_callbacks => "user/omniauth_callbacks" }
  end

Controller 调节器

class User::OmniauthCallbacksController <         Devise::OmniauthCallbacksController
  # You should configure your model like this:
  # devise :omniauthable, omniauth_providers: [:twitter]

  # You should also create an action method in this controller like this:
  # def twitter
  # end
  def facebook
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.from_omniauth(request.env["omniauth.auth"])
    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

  def self.new_with_session(params, session)
    if session["devise.user_attributes"]
      new(session[devise.user_attributes], without_protection: true) do |user|
        user.attributes = params
        user.valid?
      end
    else
      super
    end

After I click "Login with facebook", it directs to a facebook url, I input a password, press enter, get redirected to the same page with a long url. 单击“使用Facebook登录”后,它会定向到Facebook网址,我输入密码,然后按Enter键,然后重定向到带有长网址的同一页面。 ( http://localhost:3000/demographics?code=AQDK1z40APoLiWykomxDDUljBUNHotenM4lzj_bZMhH8iQ74J_Nu_EUnPqBqkbNAeWQEPZwQs7YghqkB4eD7AoQLkN_RuYIlmotMtrJc4UyGRSe3CJIHcxp6kcB9BuYHA_Ldz0NMJvvGzOuvC-uDpFn6TyrzvV5v9LvivORXVduSsCy7_r6PcW8jxAkWqZzKyASXf26h8h3f_kha2d0KX6Ygft8ozN1HT9Xr-1y7ZtIKgTXEGMrqK950kASv2oTE0tQ5CYt6mfEZsVyLpykYIApOls8NLhjOaOIJewzV9EnLdSq0FbrvtedhhDmy-hg6IkRAbRVgwEkfUFsi9DXoxKyX&state=bdbf498f33f67ef57f3f54b846f870f21bb80c039c099f1a# = ) HTTP://本地主机:3000 /人口统计码= AQDK1z40APoLiWykomxDDUljBUNHotenM4lzj_bZMhH8iQ74J_Nu_EUnPqBqkbNAeWQEPZwQs7YghqkB4eD7AoQLkN_RuYIlmotMtrJc4UyGRSe3CJIHcxp6kcB9BuYHA_Ldz0NMJvvGzOuvC-uDpFn6TyrzvV5v9LvivORXVduSsCy7_r6PcW8jxAkWqZzKyASXf26h8h3f_kha2d0KX6Ygft8ozN1HT9Xr-1y7ZtIKgTXEGMrqK950kASv2oTE0tQ5CYt6mfEZsVyLpykYIApOls8NLhjOaOIJewzV9EnLdSq0FbrvtedhhDmy-hg6IkRAbRVgwEkfUFsi9DXoxKyX&状态= bdbf498f33f67ef57f3f54b846f870f21bb80c039c099f1a#=

EDIT: 编辑:

Now I am getting 现在我得到

Could not authenticate you from Facebook because "Invalid credentials".

This is what worked for me: 这对我有用:

user model 用户模型

devise :omniauthable, :omniauth_providers => [:facebook]

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.email = auth.info.email
      user.username = auth.info.name #gives full user name
      user.password = Devise.friendly_token[0,20]
      user.skip_confirmation!
      user.save
    end
end

devise.rb devise.rb

config.omniauth :facebook, ENV['facebook_key'], ENV['facebook_secret'],
 scope: 'email,public_profile', info_fields: 'email, first_name, last_name'

callbacks_controller.rb callbacks_controller.rb

class CallbacksController < ApplicationController
  def facebook
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication
      flash[:notice] = "Logged in as #{@user.username}"      
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

  def failure
    redirect_to root_path
  end
end

routes.rb 的routes.rb

devise_for :users, controllers: { omniauth_callbacks: "callbacks" }

So the problem was I didn't config the routes properly. 所以问题是我没有正确配置路由。

In fact, I had to remove all existing devise routes and add this line. 实际上,我必须删除所有现有的设计路线并添加此行。

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

The problem is Facebook is not always returning email for user 问题是Facebook并不总是向用户返回电子邮件

from facebook developers https://developers.facebook.com/bugs/298946933534016 来自Facebook开发人员https://developers.facebook.com/bugs/298946933534016

Some possible reasons: 一些可能的原因:

  • No Email address on account 帐户中没有电子邮件地址
  • No confirmed email address on account 帐户中没有确认的电子邮件地址
  • No verified email address on account 帐户中没有经过验证的电子邮件地址
  • User entered a security checkpoint which required them to reconfirm their email address and they have not yet done so 用户输入了一个安全检查点,要求他们重新确认其电子邮件地址,但尚未这样做。
  • Users's email address is unreachable 用户的电子邮件地址无法访问

Set an condition in your controller if request.env["omniauth.auth"].info.email.present? 如果request.env [“ omniauth.auth”]。info.email.present?在控制器中设置条件? see the script below. 请参阅下面的脚本。

    class User::OmniauthCallbacksController < Devise::OmniauthCallbacksController
      def facebook
        puts request.env["omniauth.auth"]   #  check if request.env["omniauth.auth"] is provided an email
        if request.env["omniauth.auth"].info.email.present?
            @user = User.from_omniauth(request.env["omniauth.auth"])
            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
        else
            redirect_to new_user_registration_url, notice: "Couldn't connect to your #{request.env["omniauth.auth"].provider} account. Try to sign up."
        end    
      end
    end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM