我终于“使用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
会话控制器:
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 %>
不太确定我一直在做错或者为什么我很难找到答案,所以我觉得放置代码可能更容易。我对此很陌生,所以感谢任何帮助。谢谢!
答案 0 :(得分:2)
如果仍然显示“使用Facebook登录”,并且没有显示退出,我假设“已登录”也未显示。
这很可能是因为没有设置此current_user,所以此行失败
<% if current_user %>
所以此代码被触发
<li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>
所以我假设current_user实际上没有设置,因此你没有真正登录。你可以添加设计控制器过滤器authenticate_user!
以查看你是否登录。像
class ApplicationController
before_filter :authenticate_user!
end
如果您未登录,会重定向您。
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 %>
使用Facebook工作流程进行正常登录,终端中的Rails服务器将“停止”在`&lt;%binding.pry%&gt;并允许您运行命令,包括检查变量。
在rails控制台运行的终端中,您应该看到类似
的内容current_user ? "There is a current user set" : "No current user is set"
您也可以直接运行
current_user
但前者的输出更详细。其他一些有用的调试命令包括
help
whereami
exit
!!!