开发者

Rails 3 Devise redirect within custom strategy

开发者 https://www.devze.com 2023-04-06 07:22 出处:网络
My setup: Rails 3.0.9, Ruby 1.9.2, Devise 1.3.4 I implemented a custom Devise / Warden strategy to authenticate against a 3rd party API and if successful, a new user is created the first time. I shou

My setup: Rails 3.0.9, Ruby 1.9.2, Devise 1.3.4

I implemented a custom Devise / Warden strategy to authenticate against a 3rd party API and if successful, a new user is created the first time. I should clarify that the user creation is done within the custom strategy code in devise.rb

devise.rb
config.warden do |manager|
  manager.strategies.add(:mls_strategy) do
    def authenticate!
      ... authenticate against 3rd party API...
      if res.body =~ /success/
        u = User.find_or_in开发者_Python百科itialize_by_email(params[:user][:email])
        if u.new_record?
          u.save
        end
      success!(u)
    end
  end
end

That all works except that that upon creation, the user sees the login page still with an alert saying Signed in successfully. The desired behavior is that the user gets redirected to the application's root which I tried to do by adding redirect_to "/" after creating user but it couldn't find redirect_to method and I'm not even sure that's the best way to do it.

I have also tried adding this to routes.rb without success

namespace :user do
  root :to => "blah#index"
end

Suggestions?


Argh, as it turns out, all I need to do is to use save! instead of just save. Apparently save! persists it to the database whereas save delays it causing Devise to not recognize the user as authenticated.


You need to make a new controller "Registrations" and customize the appropriate method:

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    '/an/example/path'
  end
end

you can find more information here: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-%28registration%29

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号