简体   繁体   English

Devise:登录后如何重定向到上一页,但前提是前一页需要身份验证?

[英]Devise: How to redirect to the previous page after login, but only if previous page requires authentication?

Using the Devise gem, how can I redirect to the previous page after login, but only if the previous page requires authentication (otherwise redirect to a specific page I define)?使用Devise gem,我如何在登录后重定向到上一页,但前提是前一页需要身份验证(否则重定向到我定义的特定页面)?

Required scenarios:所需场景:

  • Try to access /my-private-account > Devise detects authentication required and redirects to login > Successfully login > redirect to /my-private-account尝试访问/my-private-account > Devise 检测到需要身份验证并重定向到登录 > 成功登录 > 重定向到/my-private-account
  • Visit / (doesn't require authentication) > visit /login > Successfully login > redirect to specific page eg.访问/ (不需要身份验证)> 访问/login > 成功登录 > 重定向到特定页面,例如。 /dashboard
  • Visit /about (doesn't require authentication) > visit /login > Successfully login > redirect to /dashboard访问/about (不需要认证)>访问/login >成功登录>重定向到/dashboard

This is almost covered by the Devise Wiki , but their answer redirects back for ALL pages. Devise Wiki几乎涵盖了这一点,但他们的答案重定向回所有页面。 Eg.例如。 from / (doesn't require authentication) > visit /login > Successfully login > redirects back to // (不需要认证)>访问/login >成功登录>重定向回/

My authentication is defined in routes:我的身份验证在路由中定义:

# routes.rb
authenticate :user do
  resources :events
end
# application_controller.rb
class ApplicationController < ActionController::Base
  before_action :store_user_location!, if: :storable_location?

  private
    # Redirect back to current page after sign in
    # ref: https://github.com/heartcombo/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update
    def storable_location?
      request.get? && is_navigational_format? && !devise_controller? && !request.xhr? 
    end

    def store_user_location!
      # :user is the scope we are authenticating
      store_location_for(:user, request.fullpath)
    end

    def after_sign_in_path_for(resource)
      stored_location_for(resource) || dashboard_path
    end

end

Is there a way I can check whether a route requires authentication?有没有办法检查路由是否需要身份验证? Then I could override after_sign_in_path_for eg.然后我可以覆盖after_sign_in_path_for例如。

def after_sign_in_path_for(resource)
  if stored_location_for(resource).requires_authentication? # something like this...?
    stored_location_for(resource)
  else
    events_path
  end
end

Or am I tackling this in the wrong direction and is there a better way?还是我在错误的方向上解决这个问题,有没有更好的方法?

Lastly, is there anything else I need to be aware of to make this work for omniauth too?最后,我还需要注意什么才能使这项工作也适用于omniauth?

I think what you actually need to override is the storable_location?我认为您实际需要覆盖的是storable_location? method so you don't store locations you don't want to go back.方法,这样你就不会存储你不想 go 回来的位置。

For example, in the controller that handles the / or /about routes, you could override that storable_location?例如,在处理//about路由的 controller 中,您可以覆盖该storable_location? method to always return false so those locations are not stored, then stored_location_for won't be set.方法总是返回 false 以便不存储这些位置,然后不会设置stored_location_for

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM