加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 3 – 使用OmniAuth设计 – 登录后重定向

发布时间:2020-12-16 19:25:41 所属栏目:百科 来源:网络整理
导读:我使用devize与omniauth签署用户与Facebook.我希望他们在登录后登录的页面被重定向. 我用过了 def after_sign_in_path_for(resource_or_scope) store_location = session[:return_to] clear_stored_location (store_location.nil?) ? "/" : store_location.t
我使用devize与omniauth签署用户与Facebook.我希望他们在登录后登录的页面被重定向.

我用过了

def after_sign_in_path_for(resource_or_scope)
    store_location = session[:return_to]
    clear_stored_location
    (store_location.nil?) ? "/" : store_location.to_s
  end

在我的应用程序控制器中,并使用此代码创建了一个sessions_helper

def deny_access
    store_location
    redirect_to new_user_session_path
  end

  def anyone_signed_in?
    !current_user.nil?
  end

  private

    def store_location
      session[:return_to] = request.fullpath
    end

    def clear_stored_location
      session[:return_to] = nil
    end

为了解决这个问题,被重定向到“服务/”,我有使用Facebook和其他平台的身份验证的逻辑,我已经使用

skip_before_filter :store_location

在服务和其他控制器中,我不会作为位置存储.

Q1我现在遇到的问题是当我使用ajax并在模态窗口中渲染登录表单是当用户成功登录时,它被重定向到/ users / sign_in /.我没有一个用户控制器,并尝试使一个sessions_controller.rb并添加了skip_before …在那里,但它不起作用.

这是我的sign_in的路线

new_user_session       GET   /users/sign_in(.:format)    {:action=>"new",:controller=>"devise/sessions"}
    user_session      POST   /users/sign_in(.:format)    {:action=>"create",:controller=>"devise/sessions"}
destroy_user_session   GET   /users/sign_out(.:format)   {:action=>"destroy",:controller=>"devise/sessions"}

Q2当用户登出时,我尝试使用重定向

def after_sign_out_path_for(resource_or_scope)
      (session[:return_to].nil?) ? "/" : session[:return_to].to_s
  end

但是,这只会将我重定向到根页面.

我非常感谢任何帮助,

解决方法

我在这里回答

Devise doesn’t redirect properly to stored location when using omniauth provider like facebook

在application_controller.rb中

def after_sign_in_path_for(resource_or_scope)
   if request.env['omniauth.origin']
      request.env['omniauth.origin']
    end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读