Ruby on rails 轨道4+;Omniauth+;设计:注销链接不工作

Ruby on rails 轨道4+;Omniauth+;设计:注销链接不工作,ruby-on-rails,omniauth,Ruby On Rails,Omniauth,我使用Omniauth作为登录Rails应用程序的唯一方法 问题是:当用户单击“注销”时,页面会重新加载,并且注销链接仍然存在(尽管用户是否已登录?逻辑将其包装)。这让我相信用户并没有真正注销 这是我的index.html.erb: <% if user_signed_in? %> <%= link_to "Authenticate with Google", user_omniauth_authorize_path(:google_oauth2) %> <%

我使用Omniauth作为登录Rails应用程序的唯一方法

问题是:当用户单击“注销”时,页面会重新加载,并且注销链接仍然存在(尽管用户是否已登录?逻辑将其包装)。这让我相信用户并没有真正注销

这是我的index.html.erb

<% if user_signed_in? %>
  <%= link_to "Authenticate with Google", user_omniauth_authorize_path(:google_oauth2) %>
<% else %>
  <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
<% end %>
class OmniauthCallbacksController < Devise::OmniauthCallbacksController 
  skip_before_filter :redirect_to_login_if_required
  def google_oauth2
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication
      return
    else
      session["devise.user_attributes"] = @user.attributes
      redirect_to new_user_registration_path
    end
  end
end
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
以及我的omniauth\u回调\u controller.rb:

<% if user_signed_in? %>
  <%= link_to "Authenticate with Google", user_omniauth_authorize_path(:google_oauth2) %>
<% else %>
  <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
<% end %>
class OmniauthCallbacksController < Devise::OmniauthCallbacksController 
  skip_before_filter :redirect_to_login_if_required
  def google_oauth2
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication
      return
    else
      session["devise.user_attributes"] = @user.attributes
      redirect_to new_user_registration_path
    end
  end
end
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
不幸的是,这并没有给我带来一个错误。它只是刷新索引页面,就好像什么都没发生一样

编辑:这是我单击注销时的帖子

Started DELETE "/users/sign_out" for ::1 at 2015-07-06 11:00:22 -0400
Processing by Devise::SessionsController#destroy as HTML
  Parameters: {"authenticity_token"=>"7QXScU8eVW6NVedKG5P86rPxkaP8uJdUzyJ712ZrYXtK7QjP/m33eQ2WE/ituUvFQ2GeenXLRBaiVibxEjHG6w=="}
Redirected to http://localhost:3000/
Filter chain halted as :verify_signed_out_user rendered or redirected
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
编辑2:我已将其包含在我的应用程序\u controller.rb中

def self.from_omniauth(auth)
  if user = User.find_by_email(auth.info.email)
    user.provider = auth.provider
    user.uid = auth.uid
    user
  else
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.email = auth.info.email # THIS (user.email) value i want to provide to my registration form as default value
   end
  end
end
before_action :authenticate_user!
现在我在控制台中得到的错误消息是:

Started GET "/users/auth/google_oauth2/callback?state=c92f3f9e0a8db79485e56ec2a1defd91949e8e7d99a02130&code=4/pgl_HZFw113L7VJ-rSaV9-JYngABkfgx7lqRm06Dyqg" for ::1 at 2015-07-06 16:12:14 -0400
I, [2015-07-06T16:12:14.739138 #2442]  INFO -- omniauth: (google_oauth2) Callback phase initiated.
Processing by OmniauthCallbacksController#google_oauth2 as HTML
  Parameters: {"state"=>"c92f3f9e0a8db79485e56ec2a1defd91949e8e7d99a02130", "code"=>"4/pgl_HZFw113L7VJ-rSaV9-JYngABkfgx7lqRm06Dyqg"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = ? LIMIT 1  [["email", "broy@gmail.com"]]
   (0.1ms)  begin transaction
  SQL (0.4ms)  UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = ?  [["last_sign_in_at", "2015-07-06 20:11:47.636852"], ["current_sign_in_at", "2015-07-06 20:12:15.365770"], ["sign_in_count", 42], ["updated_at", "2015-07-06 20:12:15.366734"], ["id", 4]]
   (1.4ms)  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 15ms (ActiveRecord: 2.1ms)


Started GET "/" for ::1 at 2015-07-06 16:12:15 -0400
Processing by ProductlinesController#index as HTML
Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)

我建议将if切换为除非,这样在用户登录时会显示注销链接

<% unless user_signed_in? %>
  <%= link_to "Authenticate with Google", user_omniauth_authorize_path(:google_oauth2) %>
<% else %>
  <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
<% end %>

:删除)%%>
使用Desive时,显示校正链接的另一种方式是:

<%unless current_user.blank? -%>
  <%= link_to "Authenticate with Google", user_omniauth_authorize_path(:google_oauth2) %>
<%else -%>
  <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
<%end-%>

:删除)%%>
从您发布的日志中,您已经

筛选器链暂停为:验证\u已签名\u已退出\u已呈现或重定向用户

这就是你问题的原因

在过滤器之前添加
skip\u:verify\u signed\u out\u user
到控制器应该可以解决问题

更新:

您需要将index.html.erb中的代码更改为以下内容

<% if user_signed_in? %>
  <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
<% else %>
  <%= link_to "Authenticate with Google", user_omniauth_authorize_path(:google_oauth2) %>
<% end %>

:删除)%%>

来源:

请使用
/config/initializers/develope.rb更新您的帖子
jquery ujs是否正确加载并工作?否则,链接的
:method=>:delete
部分(必需)将无法工作。尝试检查web检查器/服务器日志,查看是否确实发送了删除调用。您是否在/config/initializers/designe.rb中注释了这一行
config.sign\u out\u via=:DELETE
?如果是这样,请取消对它的注释并检查.Nope-config.sign\u out\u via=:delete在其中。您可以发布在单击注销时生成的日志吗?即使在我进行了身份验证之后,这仍然会一直为我提供“使用谷歌进行身份验证”。您100%确定登录正常吗?您介意发布编辑帖子以包含日志吗?在2015-07-06 11:00:22-0400时开始删除“/users/sign_out”for::1,由designe::sessioncontroller处理#销毁为HTML参数:{“authenticity_token”=>“7QXScU8eVW6NVedKG5P86rPxkaP8uJdUzyJ712ZrYXtK7QjP/m33eQ2WE/ituwfq2geenxlivibxejh6w=”}重定向到筛选器链暂停为:验证\u注销\u用户呈现或重定向完成302在1ms内找到(ActiveRecord:0.0ms)@beaconhill-如果您指的是“通过谷歌验证”始终显示上述代码,用户似乎没有登录,因此注销功能无法工作。但当我在Admin中查看用户记录时,“当前登录时间”时间戳是我单击login的时间和“登录计数”增加…我将其添加到omniauth\u callbacks\u controller.rb中,但我仍在暂停筛选器链,原因是:验证\u已签名\u用户已呈现或重定向。这是当前的情况:在\u筛选器之前跳过\u:如果需要,将\u重定向到\u登录\u,:验证\u已注销_user@beaconhill我想你应该把它添加到
users\u controller.rb
我没有users\u controller.rb。我通过Desive生成了用户模型。@beaconhill很抱歉,您应该将其添加到
会话\u controller.rb