设计如何添加登录检查自定义会话控制器? [英] Devise how to add signed in check in custom session controller?

查看:164
本文介绍了设计如何添加登录检查自定义会话控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Devise的控制器帮助程序signed_in?检查用户是否已经sign_in,如果是,那么我想重定向到一个特定的页面。



我已经尝试过这个,但是signed_in?方法总是返回true,我该如何使这项工作?

  class SessionsController< Devise :: SessionsController 
def new
redirect_to root_url
end

def create
如果signed_in?(resource_name)
redirect_to其中_-_ want_to_url
else
resource = warden.authenticate!(:scope => resource_name,:recall =>#{controller_path} #failure)

return sign_in_and_redirect(resource_name,resource )
end
end

def sign_in_and_redirect(resource_or_scope,resource = nil)
scope = Devise :: Mapping.find_scope!(resource_or_scope)
resource | | = resource_or_scope
sign_in(scope,resource)除非warden.user(scope)== resource
return render:json => {:success => true,:redirect => stored_location_for(scope)|| after_sign_in_path_for(resource)}
end

def failure
return render:json => {:success => false}
end

end


解决方案

您是否尝试过如果user_signed_in?



现在,我真的不知道你是什么尝试完成,但我不会在控制器中定义我的路由。我会这样做:

 #config / routes.rb 
认证:user do
root:to => where_I_want_to_url
end
root:to => 'welcome#index'


I am trying to use Devise's controller helper method signed_in? to check if a user already signed_in, if yes then I want to redirect to a specific page.

I have tried this, but the signed_in? method always return true, how can I make this work?

class SessionsController < Devise::SessionsController
  def new
    redirect_to root_url 
  end

  def create
    if signed_in?(resource_name)
      redirect_to where_-_want_to_url
    else  
      resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")

      return sign_in_and_redirect(resource_name, resource)
    end
  end

  def sign_in_and_redirect(resource_or_scope, resource=nil)
    scope = Devise::Mapping.find_scope!(resource_or_scope)
    resource ||= resource_or_scope
    sign_in(scope, resource) unless warden.user(scope) == resource
    return render :json => {:success => true, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource)}
  end

  def failure      
    return render :json => { :success => false }
  end

end

解决方案

Have you tried if user_signed_in?

Now, I'm really not sure what you are trying to accomplish but I wouldn't define my routing like that in the controller. I'd do something like this:

# config/routes.rb
authenticated :user do
  root :to => where_I_want_to_url
end
root :to => 'welcome#index'

这篇关于设计如何添加登录检查自定义会话控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆