diff --git a/app/controllers/accept_account_invitations_controller.rb b/app/controllers/accept_account_invitations_controller.rb index 5c5a717b..6ba0d020 100644 --- a/app/controllers/accept_account_invitations_controller.rb +++ b/app/controllers/accept_account_invitations_controller.rb @@ -5,7 +5,12 @@ def show return if user_signed_in? session[:after_sign_in_url] = request.fullpath - redirect_to new_user_session_url(email: AccountInvitation.find_by!(token: ps.fetch(:token)).email) + + invitee_email = AccountInvitation.find_by!(token: ps.fetch(:token)).email + user = User.find_by(email: invitee_email) + redirect_url = user ? new_user_session_url(email: invitee_email) : new_user_registration_url(email: invitee_email) + + redirect_to redirect_url end def update diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 43ad96a7..78f3e84d 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -1,4 +1,12 @@ class Users::RegistrationsController < Devise::RegistrationsController + def new + if params.key?(:email) + self.resource = User.new(email: params[:email]) + else + super + end + end + def create super do |user| user.create_account(name: user.email, email: user.email) if user.persisted? @@ -10,4 +18,13 @@ def create def devise_mapping @devise_mapping ||= Devise.mappings[:user] end + + private + + def after_sign_up_path_for(resource) + after_sign_in_url = session.delete(:after_sign_in_url) + return after_sign_in_url if after_sign_in_url.present? + + super + end end \ No newline at end of file diff --git a/spec/controllers/accept_account_invitations_spec.rb b/spec/controllers/accept_account_invitations_spec.rb index f1777968..5201dbcb 100644 --- a/spec/controllers/accept_account_invitations_spec.rb +++ b/spec/controllers/accept_account_invitations_spec.rb @@ -6,7 +6,7 @@ describe '#show' do let(:account) { create(:account, :parent) } let(:user) { create(:user, account: account) } - let(:second_user) { create(:user, account: account) } + let(:second_user) { create(:user) } let!(:account_invitation) { create(:account_invitation, user: user, account: account, email: second_user.email) } @@ -21,6 +21,15 @@ end end + context 'when the user not signed up' do + let(:account_invitation) { create(:account_invitation, user: user, account: account) } + + it 'redirects to registration url with email' do + subject + expect(response).to redirect_to(new_user_registration_url(email: account_invitation.email)) + end + end + context 'when the user signed in' do before { sign_in second_user }