2

我在https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview上关注了这个指南点

第一轮它对我进行了身份验证,让我登录,并将我保存到 db。

我清除了浏览器历史记录并测试了另一个用户。

它需要新用户到 facebook 页面登录,登录后自动将他们重定向到

 http://localhost:3000/users/sign_up#_=_

并且不会将它们登录或保存到数据库。有任何想法吗?

路线

 get "static/home"
 devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
 resources :users

 root 'static#home'

用户.rb

class User < ActiveRecord::Base
    attr_accessible :name, :provider, :uid
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable, :timeoutable,
        :recoverable, :rememberable, :trackable, :validatable, :omniauthable,
        :omniauth_providers => [:facebook]

def self.from_omniauth(auth)
    where(auth.slice(:provider, :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
    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



 protected 
    def password_required? 
    true 
 end 




end

设计.rb

config.omniauth :facebook, "#######", "################"

omn​​iauth_callbacks

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  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 #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"].except('extra')
      redirect_to new_user_registration_url
    end
  end
end

主页.html.erb

<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
4

2 回答 2

0

下面的代码正在重定向他。原来这只是他的脸书帐户。需要知道为什么他的帐户会这样做...

 if @user.persisted?
      sign_in_and_redirect @user #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"].except('extra')
      redirect_to new_user_registration_url
 end
于 2014-07-29T14:43:46.073 回答
0
http://localhost:3000/users/sign_up#_=_

我的反应完全一样。我在本地运行服务器并查看日志,它说它无法验证客户端。

事实证明,这是因为我在本地设置了 ENV 密钥export FACEBOOK_APP_SECRET='xxxxxxx',只有在您从终端的同一选项卡运行服务器时才有效。

要尝试的事情:

  1. 如果使用exports设置 env 键 -$bin/rails s从同一选项卡运行。$printenv可以通过在终端中输入来检查密钥是否已设置。
  2. 将 env 键设置为 bash 配置文件。每天在这里清除 osx 的说明
  3. 使用dotenv 模块来管理密钥。

一切顺利。

于 2017-05-07T10:16:47.153 回答