ruby-on-rails – Rails 4 Omniauth设计:注销链接不起作用

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails 4 Omniauth设计:注销链接不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用Omniauth作为登录我的Rails应用程序的唯一方法. @H_403_2@问题是:当用户单击“注销”时,页面重新加载并且logout链接仍然存在(尽管user_signed_in?逻辑包装它).这让我相信用户实际上并未注销

@H_403_2@这是我的index.html.erb:

  1. <% if user_signed_in? %>
  2. <%= link_to "Authenticate with Google",user_omniauth_authorize_path(:google_oauth2) %>
  3. <% else %>
  4. <%= link_to('logout',destroy_user_session_path,:method => :delete) %>
  5. <% end %>
@H_403_2@还有我的user.rb

  1. def self.from_omniauth(auth)
  2. if user = User.find_by_email(auth.info.email)
  3. user.provider = auth.provider
  4. user.uid = auth.uid
  5. user
  6. else
  7. where(provider: auth.provider,uid: auth.uid).first_or_create do |user|
  8. user.provider = auth.provider
  9. user.uid = auth.uid
  10. user.email = auth.info.email # THIS (user.email) value i want to provide to my registration form as default value
  11. end
  12. end
  13. end
@H_403_2@我的omniauth_callbacks_controller.rb:

  1. class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  2. skip_before_filter :redirect_to_login_if_required
  3. def google_oauth2
  4. @user = User.from_omniauth(request.env["omniauth.auth"])
  5. if @user.persisted?
  6. sign_in_and_redirect @user,:event => :authentication
  7. return
  8. else
  9. session["devise.user_attributes"] = @user.attributes
  10. redirect_to new_user_registration_path
  11. end
  12. end
  13. end
@H_403_2@和我的routes.rb:

  1. devise_for :users,:controllers => { :omniauth_callbacks => "omniauth_callbacks" }
@H_403_2@不幸的是,它没有给我带来错误.它只是刷新索引页面,好像什么也没发生.

@H_403_2@编辑:这是我点击退出时的POST

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

  1. before_action :authenticate_user!
@H_403_2@现在,我在控制台中收到的错误消息是:

  1. 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
  2. I,[2015-07-06T16:12:14.739138 #2442] INFO -- omniauth: (google_oauth2) Callback phase initiated.
  3. Processing by OmniauthCallbacksController#google_oauth2 as HTML
  4. Parameters: {"state"=>"c92f3f9e0a8db79485e56ec2a1defd91949e8e7d99a02130","code"=>"4/pgl_HZFw113L7VJ-rSaV9-JYngABkfgx7lqRm06Dyqg"}
  5. User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."email" = ? LIMIT 1 [["email","broy@gmail.com"]]
  6. (0.1ms) begin transaction
  7. 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]]
  8. (1.4ms) commit transaction
  9. Redirected to http://localhost:3000/
  10. Completed 302 Found in 15ms (ActiveRecord: 2.1ms)
  11.  
  12.  
  13. Started GET "/" for ::1 at 2015-07-06 16:12:15 -0400
  14. Processing by ProductlinesController#index as HTML
  15. Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)

解决方法

我建议将if切换为除非,因此它会在用户登录显示注销链接.
  1. <% unless user_signed_in? %>
  2. <%= link_to "Authenticate with Google",:method => :delete) %>
  3. <% end %>
@H_403_2@使用设计时,显示更正链接的另一种方法是:

  1. <%unless current_user.blank? -%>
  2. <%= link_to "Authenticate with Google",user_omniauth_authorize_path(:google_oauth2) %>
  3. <%else -%>
  4. <%= link_to('logout',:method => :delete) %>
  5. <%end-%>

猜你在找的Ruby相关文章