Search code examples
ruby-on-railsauthenticationdevisedevise-invitable

Log in user after accept invitation using rails with devise_invitable


I use devise_invitable with Rails and need some help. I want make user logged in after accept invitation. Here is my InvitationsController

class InvitationsController < Devise::InvitationsController
  def update
    if User.accept_invitation!(user_params)
      # log in user here
      redirect_to dashboard_show_path, notice: t('invitaion.accepted')
    else
      redirect_to root_path, error: t('invitation.not_accepted')
    end
  end

  private
  def user_params
    params.require(:user).permit(:invitation_token, :password, :password_confirmation)
  end
end

You can see comment in code

# log in user here

here I want log in the user who has accept the invitation.

Thanks.


Solution

  • The method your looking for is sign_in, try this:

    def update
      if User.accept_invitation!(user_params)
        sign_in(params[:user])
        redirect_to dashboard_show_path, notice: t('invitaion.accepted')
      else
        redirect_to root_path, error: t('invitation.not_accepted')
      end
    end
    

    However I should note that devise_invitable, by default, signs in users after they have accepted an invitation. See the default update action here, if you wish to use the default functionality simply call the super method or don't implement the update action at all.