我终于让"sign in with Facebook“与devise/omniauth一起工作,但当用户登录时,"sign in with facebook”链接不会更改为"sign out“,并且/或者没有可见的sign out选项。
这是我的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
endOmniauth_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 %>我不太确定我一直做错了什么,或者为什么我很难找到答案,所以我想把代码放上去可能会更容易。我是个新手,所以任何帮助我都很感激。谢谢!
发布于 2014-01-05 04:25:42
简而言之- current_user是空的,所以你实际上并没有登录。
如果"Sign in with Facebook“仍然显示,而”Sign Out“没有显示,我认为”Sign in as“也没有显示。
这很可能是因为未设置此current_user,因此此行失败
<% if current_user %>所以这段代码会被触发
<li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>因此,我假设实际上没有设置current_user,因此您并没有真正登录。您可以添加devise控制器筛选器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 %>通过使用Facebook的正常登录工作流程,您终端中的Rails服务器将在`<% binding.pry %>处“停止”,并允许您运行命令,包括检查变量。
在运行rails控制台的终端中,您应该看到类似这样的内容
current_user ? "There is a current user set" : "No current user is set"你也可以直接运行
current_user但前者的输出更为冗长。其他一些有用的调试命令包括
help whereami exit !!!
https://stackoverflow.com/questions/20926176
复制相似问题