Omniauth Facebook登录无法工作

Omniauth Facebook logging in not working

本文关键字:工作 登录 Facebook Omniauth      更新时间:2024-04-29

我一直在尝试为我的最新项目创建一个用户身份验证系统。我决定不存储任何密码和东西,而是使用社交网络允许人们在我的项目中使用特定功能(发布文章等)。

我刚刚完成了Sergio Tapia Gutierrez关于https://coderwall.com/p/bsfitw.正如评论中所显示的那样,教程步骤对大多数用户来说都是正确的。但对我来说不是。

我做的每件事都和他在帖子中提到的一样,但我现在面临着很多错误。

首先,它是识别脚本。我的rails应用程序似乎无法识别用户是否登录,尽管我已经在我的应用程序中放入了教程中提到的每一行代码。奇怪的是,当我点击多次时,它开始识别并登录我。

接下来让我头疼的是javascript。我在Heroku上托管我的应用程序——有时JS似乎对鼠标点击没有反应(实际上是jquery)。我还为Facebook对话框放了一个弹出脚本——它也不会弹出。

很抱歉,我不能正确地解释它——我是Rails的新手,现在我很困惑——我不知道真正的问题在哪里,这些只是猜测。

Facebook.js.coffee

    jQuery ->
      $('body').prepend('<div id="fb-root"></div>')
      $.ajax
        url: "#{window.location.protocol}//connect.facebook.net/en_US/all.js"
        dataType: 'script'
        cache: true

    window.fbAsyncInit = ->
      FB.init(appId: 'MyAppId(i've put it here, can't show ya :)', cookie: true)
      $('#sign_in').click (e) ->
        e.preventDefault()
        FB.login (response) ->
          window.location = '/auth/facebook/callback' if response.authResponse
      $('#sign_out').click (e) ->
        FB.getLoginStatus (response) ->
          FB.logout() if response.authResponse
        true

application.html.erb 中的一块代码

<div id="user-widget">
  <% if current_user %> 
    Welcome <strong><%= current_user.name %></strong>!
    <%= link_to "Sign out", "#", id: "sign_out" %>
  <% else %>
    <%= link_to "Sign in with Facebook", "#", id: "sign_in" %>
  <% end %>
</div>

以及来自应用程序控制器的用户状态检查器

class ApplicationController < ActionController::Base

  private
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  helper_method :current_user
end

最后,会话控制器代码

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id
    redirect_to articles_path, :notice => "Logged in"
  end
  def destroy
    session[:user_id] = nil
    redirect_to articles_path, :notice => "Logged out"
  end
end

更新我已经检查了JS控制台。它不断地吐出这句话——"当用户已经连接时调用FB.login()。"这是什么意思?用户似乎没有连接,但它一直在弹出。

要使其在生产中发挥作用,您必须在facebook应用程序设置中进行一些更改

  1. 您的应用程序域应该是herokuapp.com

  2. 网站网址http://yourappname.herokuapp.com/

  3. 并且不应处于沙箱模式