devise_invitable icon indicating copy to clipboard operation
devise_invitable copied to clipboard

Cannot accept invitation

Open TatianaTyu opened this issue 13 years ago • 14 comments

Hi,

I've recently upgraded to Rails4, and now when I invite a user and then click "Accept invitation" link in the email, I am asked to "sign in before continuing".

The accept invitation url looks like: http://lvh.me:3000/users/invitation/accept?invitation_token=dd7b6a79fbe5b08be27be68f7f7a615f11cd9e21cd933036149a8166b96fb2b8

A user with this invitation token really exists:

> User.where(invitation_token: 'dd7b6a79fbe5b08be27be68f7f7a615f11cd9e21cd933036149a8166b96fb2b8').first
  User Load (1.2ms)  SELECT "users".* FROM "users" WHERE "users"."invitation_token" = 'dd7b6a79fbe5b08be27be68f7f7a615f11cd9e21cd933036149a8166b96fb2b8' ORDER BY "users"."id" ASC LIMIT 1
John Smith {
                        :id => 42,
                     :email => "[email protected]",
        :encrypted_password => "",
      :reset_password_token => nil,
    :reset_password_sent_at => nil,
       :remember_created_at => nil,
             :sign_in_count => 0,
        :current_sign_in_at => nil,
           :last_sign_in_at => nil,
        :current_sign_in_ip => nil,
           :last_sign_in_ip => nil,
        :confirmation_token => nil,
              :confirmed_at => nil,
      :confirmation_sent_at => nil,
         :unconfirmed_email => nil,
           :failed_attempts => 0,
              :unlock_token => nil,
                 :locked_at => nil,
      :authentication_token => nil,
                :created_at => Sun, 15 Sep 2013 13:53:12 UTC +00:00,
                :updated_at => Sun, 15 Sep 2013 13:53:12 UTC +00:00,
                :superadmin => nil,
          :invitation_token => "dd7b6a79fbe5b08be27be68f7f7a615f11cd9e21cd933036149a8166b96fb2b8",
        :invitation_sent_at => Sun, 15 Sep 2013 13:53:12 UTC +00:00,
    :invitation_accepted_at => nil,
          :invitation_limit => nil,
             :invited_by_id => 13,
           :invited_by_type => "User",
                      :name => "John",
                   :surname => "Smith",
     :invitation_created_at => Sun, 15 Sep 2013 13:53:12 UTC +00:00
}

Server log says (is "Filter chain halted as :resource_from_invitation_token rendered or redirected" line relevant to the issue?):

Started GET "/users/invitation/accept?invitation_token=dd7b6a79fbe5b08be27be68f7f7a615f11cd9e21cd933036149a8166b96fb2b8" for 127.0.0.1 at 2013-09-15 17:13:23 +0300
Processing by DeviseOverridden::InvitationsController#edit as HTML
  Parameters: {"invitation_token"=>"dd7b6a79fbe5b08be27be68f7f7a615f11cd9e21cd933036149a8166b96fb2b8"}
  User Load (1.5ms)  SELECT "users".* FROM "users" WHERE "users"."invitation_token" = '4a69a7c0eedf835e0778d69e6fc98da3919e5f66be8075747134359524865e53' LIMIT 1
Redirected to http://lvh.me:3000/
Filter chain halted as :resource_from_invitation_token rendered or redirected
Completed 302 Found in 1124ms (ActiveRecord: 7.7ms)

Started GET "/" for 127.0.0.1 at 2013-09-15 17:13:24 +0300
Processing by FacilitiesController#index as HTML
Completed 401 Unauthorized in 6ms

Started GET "/users/sign_in" for 127.0.0.1 at 2013-09-15 17:13:24 +0300
...

Gem versions: rails (4.0.0) devise (3.1.0) devise_invitable (1.2.2) (in the Gemfile I have: gem 'devise_invitable', :github => 'scambra/devise_invitable' )

I've also completely updated config files and views by running: $ rails generate devise:install $ rails generate devise_invitable:install $ rails generate devise:views

InvitationsController::edit action is NOT overridden.

Any idea why that could be, or how can I better trace it?

Thank you very much.

TatianaTyu avatar Sep 15 '13 14:09 TatianaTyu

I think this is due to security changes in Devise 3.1. See issue #369 for details. You might try the workaround listed there or downgrade to Devise 3.0.2 temporarily:

Devise.setup do |config|
  config.allow_insecure_token_lookup = true
end

barrettc avatar Sep 15 '13 15:09 barrettc

Thanks Barrett, but the issue remains.

I cannot downgrade to Devise 3.0.2 as it is incompatible with Devise Invitable for Rails4 version. Tried the config.allow_insecure_token_lookup option, no luck.

Besides, my tokens that are in the DB are matching those that are in the emails, so I think issue #369 is not related to this one.

TatianaTyu avatar Sep 15 '13 15:09 TatianaTyu

Hmm well your comment made me realize I have not been following directions correctly from the devise_invitable README. For what it's worth, my Rails 4 app uses the following in the Gemfile:

gem 'devise', '3.0.2'
gem 'devise_invitable', '1.2.1'

The README of course says to use :github but so far this has been working ok for me.

barrettc avatar Sep 15 '13 16:09 barrettc

Switched to the same versions as you have, and the issue has gone. Testing the rest of functionality now. Thank you a lot Barrett! Still something needs to be fixed in the newer version.

TatianaTyu avatar Sep 15 '13 17:09 TatianaTyu

Great - if you happen to find any problems with that combination, please let me know.

And yes I agree it does seem that the latest on devise_invitable master needs some work.

barrettc avatar Sep 15 '13 17:09 barrettc

if you happen to find any problems with that combination, please let me know

Seems like it works smooth.

TatianaTyu avatar Sep 16 '13 16:09 TatianaTyu

@kira-corina Is your user valid? I opened issue #368 for this reason, perhaps you're seeing the same?

leejarvis avatar Sep 16 '13 18:09 leejarvis

@leejarvis Yes, just checked, my user is valid.

TatianaTyu avatar Sep 17 '13 06:09 TatianaTyu

@kira-corina did you overrided the mailer view? In devise 3.1 confirmation token used in URL must be @token instead of @resource.invitation_token

scambra avatar Sep 17 '13 08:09 scambra

@scambra I think it was correct as I regenerated and merged views after upgrade. I remember that @token thing. And besides, the email link included a correct token (see my original message).

I also did override InvitationsController#create method as follows below. But I don't think it's relevant as at the moment invitation is being accepted the DB data are correct and valid.

class DeviseOverridden::InvitationsController < Devise::InvitationsController

  # I basically just wrapped :invite! method to add some business logic
  def create
    authorize! :create, Member  # cancan gem

    # 1. init and invite user
    self.resource = resource_class.smart_invite!(resource_params, current_inviter, current_facility)

    # 2. render response - copied from:
    # https://github.com/scambra/devise_invitable/blob/master/app/controllers/devise/invitations_controller.rb
    if resource.errors.empty?
      set_flash_message :notice, :send_instructions, :email => self.resource.email if self.resource.invitation_sent_at
      respond_with resource, :location => after_invite_path_for(resource)
    else
      respond_with_navigational(resource) { render :new }
    end
  end
end

TatianaTyu avatar Sep 17 '13 09:09 TatianaTyu

I see resource is only searched with digested token: User Load (1.5ms) SELECT "users".* FROM "users" WHERE "users"."invitation_token" = '4a69a7c0eedf835e0778d69e6fc98da3919e5f66be8075747134359524865e53' LIMIT 1

But resource is saved with original token: :invitation_token => "dd7b6a79fbe5b08be27be68f7f7a615f11cd9e21cd933036149a8166b96fb2b8",

Is it saved before updating to devise 3.1? Or you saved by yourself wih resource_class.smart_invite! If you save with original token instead of digested one, you must enable config.allow_insecure_token_lookup = true in devise initializer, then devise_invitable will try to search by original token too

scambra avatar Sep 17 '13 11:09 scambra

Yes, exactly that's the problem. I haven't notice the difference. With the config.allow_insecure_token_lookup option set to true it works.

Is it saved before updating to devise 3.1

No

Or you saved by yourself with resource_class.smart_invite!

Yes, and inside it calls resource_class.invite!:

# at User model
def smart_invite!(resource_params, inviter, facility)
  if resource_params[:id]
    # if user id is passed, that means the user exists,
    # and we just need to send invitation email again.
    user_id = resource_params[:id]
    result = find(user_id)
    result.invite!
  else
    # ... otherwise, we need to invite a new user by email
    email = resource_params[:email]
    if result = facility.users.where(:email => email).first
      # user has already been invited to the facility.
      result.errors.add(:email, "is already invited.")
    elsif result = where(:email => email).first
      # user already exists in the system.
      result.join_facility!(facility)
      result.notify_about_new_facility!(facility) # send email
    else
      # user does not exist, invite him.
      # basically permitted_params = resource_params.permit(:name, :surname, :email, :password, :password_confirmation, :remember_me)
      permitted_params = PermittedParams.new(resource_params, inviter).user   
      # !!!
      result = invite!(permitted_params, inviter)
      result.join_facility!(facility) if result.portfolio  # i.e. if invitation is successful
    end
  end
  result
end

I will study the new device_invitable code more carefully and try to fix things on my side.

Thank you!

TatianaTyu avatar Sep 18 '13 16:09 TatianaTyu

If you invite with allow_insecure_token_lookup = true token will be saved unencrypted, the same as url token, then it must be accepted with allow_insecure_token_lookup = true too, so deviseInvitable searches with encrypted token and then unencrypted token.

If you invite with allow_insecure_token_lookup = false, then token it's saved encrypted and DeviseInvitable searches only with encrypted token

scambra avatar Sep 18 '13 22:09 scambra

Today I looked into this again and the egde version of the gem is working just fine! I believe last time I commented here I tested the app with old views. They referred to @resource.invitation_token instead of @token. Fixed that - now it works. Sorry, seems like I was tweaking git branches too much and lost the new views at some point. And thank you for your help!

TatianaTyu avatar Sep 23 '13 10:09 TatianaTyu