我正在构建一个使用Devise gem进行身份验证的Web应用程序(还有一个API),并且还使用Doorkeeper gem进行API部分的身份验证.
问题是现在,当我转到接收Oauth2代码(和登录)的URL时,我被重定向到Web应用程序而不是客户端回调URL.
我需要做的是在正常登录时重定向到Web应用程序,并在使用Oauth时重定向到回调URL.
我该怎么做?我覆盖了Devise会话控制器,但我不知道该怎么做.
这是我的代码:
- def new
- session[:return_to] = params[:return_to] if params[:return_to]
- resource = build_resource
- clean_up_passwords(resource)
- end
- def create
- resource = warden.authenticate!(auth_options)
- sign_in(resource_name,resource)
- if session[:return_to]
- redirect_to session[:return_to]
- session[:return_to] = nil
- else
- respond_with resource,:location => after_sign_in_path_for(resource)
- end
- end
问题是,Devise似乎忽略了我的重定向逻辑.
请进一步建议.
解决方法
假设你的资源是用户添加会话[:user_return_to] = request.fullpath到resource_owner_authenticator块
例:
- resource_owner_authenticator do
- #raise "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}"
- # Put your resource owner authentication logic here.
- # Example implementation:
- session[:user_return_to] = request.fullpath
- current_user || redirect_to(login_url)
- end