Marco
Marco

Reputation: 195

Devise/Devise_invitable: How to skip the pending invitations control

I have a Rails/Mongoid application using devise and devise_invitable gems with a special scenario where a User has_and_belongs_to_many groups. This in the application means that a User can be invited to one or more groups by other users. I'm using devise_invitable to handle those invites for both new and existing users.

The scenario where I'm having problems is when a user that has already registered and confirmed its account, and that might have already be invited to one group, is invited to another group. In this case the system sends the new invitation as usual but for the existing User instead of creating a new one to be invited.

If the user confirms the invitation everything works ok, but the problem I'm having is that unless the user confirms it, he/she won't be able to login again into his user account because it will get the alert:

You have a pending invitation, accept it to finish creating your account.

So I'm wondering how should I do or what should I override on Devise/Devise_invitable in order to skip that control when trying to login as a user that has already been confirmed but has a pending invitation.

My User model is this one:

class User
  include Mongoid::Document
  include Mongoid::Attributes::Dynamic
  include Mongoid::Timestamps
  include Mongoid::Userstamp::User

  devise :invitable, :encryptable, :database_authenticatable, :registerable, :lockable, :recoverable, :confirmable, :rememberable, :trackable, :validatable, :timeoutable

has_and_belongs_to_many :groups

Thanks!

Upvotes: 2

Views: 2384

Answers (1)

Marco
Marco

Reputation: 195

Ok I got help at the devise_invitable ticket I had opened.

The way to achieve what I was asking for is to override this method on the User model. So the solution to allow the confirmed user to sign in even when he/she has pending invitations is the following:

class User
  ...
  def block_from_invitation?
    #If the user has not been confirmed yet, we let the usual controls work
    if confirmed_at.blank?
      return invited_to_sign_up?
    else #if the user was confirmed, we let them in (will decide to accept or decline invitation from the dashboard)
      return false
    end
  end
  ...
end

Hope that helps anyone else in the future.

Upvotes: 1

Related Questions