Victor Martin
Victor Martin

Reputation: 1

Devise_invitable : wrong flash upon successfull invitation edit

Gemfile :

gem 'devise',           '>= 2.0.0'
gem 'devise_invitable', '~> 1.3.4'

Users accessing /invitation/accept.XX?invitation_token=XXXXXXXX successfully set their password using this form:

<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => { :method => :put } do |f| %>
  <%= devise_error_messages! %>
  <%= f.hidden_field :invitation_token %>

  <p><%= f.label :password %><br />
  <%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></p>

  <p><%= f.submit t("devise.invitations.edit.submit_button"), class:'btn btn-inverse' %></p>
<% end %>

Upon succes, they get signed_in and redirected to the intended page, however they get the flash message located in :

en:  
 devise:  
  invitations:  
   invitation_token_invalid: 'The invitation token provided is not valid!'

Any idea why so?

Upvotes: 0

Views: 480

Answers (2)

Justin
Justin

Reputation: 4940

@Steve is correct, as that line is what was causing this error for me as well. In particular, its the session[:previous_url].

Here is my workaround that fixes the issue for me.

routes.rb

devise_for :users, controllers: { invitations: 'users/invitations' }

Users::InvitationsController

module Users
  class InvitationsController < Devise::InvitationsController
    def after_accept_path_for(resource)
      root_path # you can define this yourself. Just don't use session[:previous_url]
    end
  end
end

Essentially here the default behavior is to use after_sign_in_path_for as the path after a resource accepts an invitation. The above solution just overrides that default behavior. You can check the docs for more information.

Upvotes: 1

Steve
Steve

Reputation: 2666

I ran into the same problem, not sure if the cause of your issue. I had modified the devise redirect after sign in, which caused my application to try to accept the invitation a second time. This causes the error: 'The invitation token provided is not valid!' since DeviseInvitable deletes the invitation_token after the user successful accepts the invitiation the first time.

My problem code was:

def after_sign_in_path_for(resource_or_scoped)
  session[:previous_url] || root_path
end

Upvotes: 1

Related Questions