我终于“使用 Facebook 登录”来使用 devise/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
session Controller :
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
Omniauth_callbacks_controller:
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
最后,应用程序布局
<% 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 为 nil,因此您实际上没有登录。
如果“使用 Facebook 登录”仍然显示,但没有显示“注销”,我假设“登录为”也没有显示。
这很可能是由于未设置此 current_user,所以此行失败
<% if current_user %>
因此这段代码被触发
<li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>
所以我假设 current_user 实际上没有设置,因此你没有真正登录。你可以添加设备 Controller 过滤器 authenticate_user!
来查看你是否登录。有点像
class ApplicationController
before_filter :authenticate_user!
end
如果您未登录,它将重定向您。
使用 Pry 找出答案
Pry是调试这类东西的好工具
要使用 pry 进行调试,您可以将其添加到您的 Gemfile
group :development do
gem "pry", "~> 0.9.12.4"
end
并运行bundle install
然后您可以将其添加到您的代码中
<%= 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 %>
通过正常的 Login with Facebook 工作流程,您终端中的 Rails 服务器将“停止”在 `<% binding.pry %> 并允许您运行命令,包括检查变量。
在运行 Rails 控制台的终端中,您应该会看到类似的内容
current_user ? "There is a current user set" : "No current user is set"
你也可以直接跑
current_user
但前者有更详细的输出。其他一些有用的调试命令包括
帮助
哪里有
退出
!!!
关于facebook - 设计+ omniauth注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926176/