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_initialize_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 justsave. Apparentlysave!persists it to the database whereassavedelays it causing Devise to not recognize the user as authenticated.