[英]Disable Devise sign_in redirection
I'm building an API on top of my rails 3.2 application. 我正在Rails 3.2应用程序之上构建API。
When I login to api/login
using the right parameters for the first time, it works great. 当我第一次使用正确的参数登录
api/login
时,效果很好。 But when I attempt to repeat the exact same operation again, I'm already logged in and it renders me the html of my app's homepage. 但是,当我尝试再次重复完全相同的操作时,我已经登录,它使我呈现了应用程序主页的html。 I guess it's because my application is set up to redirect to this page after log in. Can I modify my
api/sessions_controller.rb
such that it does not redirect to anything : 我猜这是因为我的应用程序设置为登录后重定向到此页面。我可以修改
api/sessions_controller.rb
使其不重定向到任何内容:
class SessionsController < Devise::SessionsController
prepend_before_filter :require_no_authentication, :only => [:create]
before_filter :ensure_params_exist, :only => [:create]
respond_to :json
def create
build_resource
resource = User.find_for_database_authentication(login: params[:login])
return invalid_login_attempt unless resource
if resource.valid_password?(params[:password])
sign_in("user", resource) # I would like to disable redirection here
generate_authentication_token(resource)
render :json => {success: true, ...}
return
end
invalid_login_attempt
end
Edit 编辑
def invalid_login_attempt
warden.custom_failure!
render :json=> {success: false, message: "Error with your login or password" + params.inspect}, :status=>401
end
I'm going to answer my own question because I found one way to do this : 我将回答我自己的问题,因为我找到了一种解决方法:
#sessions_controller.rb
def after_sign_in_path_for(resource)
sign_in_url = url_for(:action => 'show_info', :controller => 'sessions', :only_path => false, :protocol => 'http')
end
And then make sessions#show_info
return the exact same information the create
function will return. 然后使
sessions#show_info
返回与create
函数将返回的完全相同的信息。 Not sure if this is the best way though 不确定这是否是最好的方法
Does this help you in any way? 这对您有任何帮助吗? https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in Also, not redirecting anywhere after sign in is not a very healthy workflow.
https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in另外,登录后不重定向到任何地方都不是很健康的工作流程。 In a logged in session, you shouldn't be able to reach a sign in screen to input credentials.
在登录的会话中,您不应能够进入登录屏幕来输入凭据。 You can only reach it after sign out.
您只有退出后才能到达。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.