Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't accept invitation on an invalid resource #800

Open
jasonperrone opened this issue May 31, 2019 · 3 comments
Open

Can't accept invitation on an invalid resource #800

jasonperrone opened this issue May 31, 2019 · 3 comments

Comments

@jasonperrone
Copy link

In my app, I invite people solely by their email addresses. I don't have other information on them, like First Name and Last Name, which are required on my resource. Now, when they accept the invitation the normal way, with setting up a password, no problem. I merely added first name and last name to the invitation edit form and added those parameters to the devise sanitized parameters. Problem solved.

Except when it comes to Social Media registrations. See, I also allow you to accept the invite by logging in from Facebook or Google. In the omniauth callbacks I simply look up the user record that invite! created by the invitation token and do a user.accept_invitation! on it. Normally works great. EXCEPT, Facebook doesn't send me first name and last name. I need the user to finish filling out their profile AFTER they accept the invitation and login. But alas, Devise does not have a way to accept an invitation on an invalid resource. It's right in models.rb in the accept_invitation! method. There's a save with no option to say validate: false.

I'll work around it for now by putting some garbage in the first name and last name fields before accepting if they're blank, and then resetting them afterwards. Or in the profile setup I'll check if the user is still "invited" and then complete the invitation at that point. But I really do think there is a use case for accepting invitations on invalid resources. Just my opinion, I could be, and usually am, wrong.

@scambra
Copy link
Owner

scambra commented Jun 1, 2019

You can add conditions to your validations based on attr_accessor, so you can enable this accessor to disable your validations on controller, when invitation is accepted from facebook, before calling accept_invitation!

attr_accessor :register_from_facebook
validates :first_name, :last_name, presence: true, unless: :register_from_facebook

@richardonrails
Copy link

richardonrails commented Dec 21, 2022

My invites need an email and a valid role to be created/sent OR when an admin is modifying the invitation (editing its role).
Then when the invitee is signing up I want to require a full name to be added. I generally don't want invitations created unless my model is valid so I have been doing this:

devise :invitable, :registerable, :recoverable, :rememberable, :trackable, :validatable, validate_on_invite: true

# We use is_outstanding_invitation? so that we allow editing
# outstanding Invitations (e.g. send an invite and then change its role)
validates :full_name, presence: true, unless: :is_outstanding_invitation?

enum role: { ... }

# Does this user record represent an Invitation that has not yet been accepted?
def is_outstanding_invitation?
  # We can't use "!accepted_or_not_invited?" or "invitation_token" here because
  # "invitation_token" doesn't get set early enough in the invitation flow
  # We also can't used "created_by_invite?" because "invitation_created_at" is also
  # created too late, unlike "invited_by".
  # https://github.com/scambra/devise_invitable/blob/v1.7.5/lib/devise_invitable/models.rb
  return invited_by && !invitation_accepted?
end

However this approach has a problem that full_name isn't required/validated when accepting an invite.

Suggestions?

@scambra
Copy link
Owner

scambra commented Dec 22, 2022

@richardonrails your issue is different than this issue, it would be better to open new issue.

Anyway, you can validate with accepting_invitation? so it's required only while accepting an invitation.

If you want to validate that field while is accepting, and when user is edited after accepted, you can use accepting_invitation? || invitation_accepted?

If your users can register without invitation too, then you can use accepting_invitation? || accepted_or_not_invited?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants