Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Facebook 设计+;omniauth注销_Facebook_Devise_Omniauth_Logout - Fatal编程技术网

Facebook 设计+;omniauth注销

Facebook 设计+;omniauth注销,facebook,devise,omniauth,logout,Facebook,Devise,Omniauth,Logout,我终于获得了“使用Facebook登录”来使用Desive/omniauth,但当用户登录时,“使用Facebook登录”链接不会更改为“注销”,并且/或者没有可见的注销选项 这是我的route.rb文件 devise_for :users, :controllers => {:omniauth_callbacks => "users/omniauth_callbacks", :registrations => 'registrations'}, :path_names =&g

我终于获得了“使用Facebook登录”来使用Desive/omniauth,但当用户登录时,“使用Facebook登录”链接不会更改为“注销”,并且/或者没有可见的注销选项

这是我的route.rb文件

devise_for :users, :controllers => {:omniauth_callbacks => "users/omniauth_callbacks", :registrations => 'registrations'}, :path_names => { :sign_in => 'login', :sign_out => 'logout' } do
  get 'login' =>'devise/sessions#new', :as => :new_user_session
  post 'login' => 'devise/sessions#create', :as => :user_session
  get 'signup'  => 'registrations#new', :as => :new_user_registration
  get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
结束

这是用户文件

class User < ActiveRecord::Base
devise :omniauthable, :omniauth_providers => [:facebook]
def self.find_for_facebook_oauth(auth, signed_in_resource=ni)
user = User.where (:provider => auth.provider, :uid => auth.uid).first
unless user

def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.where
user = User.create(name:auth.extra.raw_info.name,
                     provider:auth.provider,
                     uid:auth.uid,
                     email:auth.info.email,
                     password:Devise.friendly_token[0,20]
                     )
end
user
end

end
class用户[:facebook]
def self.find_for_facebook_oauth(auth,signed_in_resource=ni)
user=user.where(:provider=>auth.provider,:uid=>auth.uid)
除非用户
def self.find_for_facebook_oauth(auth,signed_in_resource=nil)
user=user.where
user=user.create(名称:auth.extra.raw\u info.name,
提供者:auth.provider,
uid:auth.uid,
电子邮件:auth.info.email,
密码:design.friendly_令牌[0,20]
)
结束
用户
结束
结束
会话控制器:

class SessionsController < ApplicationController
def create
 user = User.from_omniauth(env["omniauth.auth"])
 session[:user_id] = user.id
 redirect_to root_path
 end

def destroy
 session.delete[:user_id] = nil
 redirect_to root_path
end
end
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.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

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
end
class sessioncontroller
Omniauth\u回调\u控制器:

class SessionsController < ApplicationController
def create
 user = User.from_omniauth(env["omniauth.auth"])
 session[:user_id] = user.id
 redirect_to root_path
 end

def destroy
 session.delete[:user_id] = nil
 redirect_to root_path
end
end
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.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

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
end

class Users::OmniAuthCallbackController:authentication#如果未激活@user,将引发此错误
如果是导航格式,设置flash消息(:notice,:success,:kind=>“Facebook”)?
其他的
会话[“designe.facebook_data”]=request.env[“omniauth.auth”]
将\重定向到新\用户\注册\ url
结束
结束
结束
最后是应用程序布局

<% if current_user %>
      Signed in as <strong><%= current_user.name %></strong>!
      <%= link_to "Sign out", destroy_user_session, id: "sign_out" %>
    <% else %>
  <li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>
  <% end %>
身份登录!
  • 我不太清楚我是不是一直在做错事,也不清楚为什么我很难找到答案,所以我想只需要编写代码就可以了。我是新手,非常感谢您的帮助。谢谢!

    简短回答-当前用户为零,因此您没有实际登录。 如果“使用Facebook登录”仍在显示,而“注销”未显示,则我假设“以身份登录”也未显示

    这很可能是由于当前用户未设置,因此此行失败

    <% if current_user %>
    
    如果您未登录,它将重定向您

    用撬子找出 是调试这类事情的好工具

    要使用pry进行调试,您需要将

    group :development do
      gem "pry", "~> 0.9.12.4"
    end
    
    然后运行
    bundle安装

    然后可以将其添加到代码中

    <%= binding.pry %>
    <% if current_user %>
      Signed in as <strong><%= current_user.name %></strong>!
      <%= link_to "Sign out", destroy_user_session, id: "sign_out" %>
    <% else %>
      <li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>
    <% end %>
    
    你也可以跑步

    current_user
    
    但是前者有更详细的输出

    帮助
    mi
    退出

    !!!

    你会发布你的用户/omniauth\u回调控制器吗?
    类用户::omniauthcallbackcontroller:authentication#如果@user未激活,则会抛出设置闪现消息(:注意,:success,:kind=>“Facebook”)如果是导航格式?否则会话[“designe.Facebook\u data”]=request.env[“omniauth.auth”]如果您可以编辑您的原始答案,请将\u重定向到新用户\u注册\u url end
    ,并将此代码添加到底部,这样会更好。此外,请包括您的用户模型,因为用户正在调用该模型::OmniauthCallbackControllergotcha,已更新!这是一个非常基本的问题,但我将在哪里查找d用户模型?你会发现模型位于app/models中,因此用户模型将位于app/models/user.rb感谢你的回复,@ruevaughn!那么,当我第一次登录时,我需要到facebook验证和验证我是否想要登录facebook…那么这是否意味着它工作或不工作?如果它不工作,我该如何设置用户“未设置?该屏幕表示您已正确配置了所有内容以与facebook对话,但并不意味着之后所有内容都已刷新。我认为您的User::OmniAuthCallbackController中存在语法错误,可能在此处附近:User.find_for_facebook_oauth(request.env[“omniauth.auth”],当前用户)**sign_in_和_redirect@user是您应该登录的内容,并设置当前的_用户
    current_user