Devise with Confirmable - Redirect User to a Custom Page When Users Tries to Sign in with an Unconfirmed Email

Devise with Confirmable - Redirect user to a custom page when users tries to sign in with an unconfirmed email

Sorry at first I thought you meant after Sign Up not Sign In. So the down below works for how to direct users after Sign Up and what you need to do for Sign In is to create a custom Devise::FailureApp

See the wiki page: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

Then within your custom FailureApp overwrite redirect_url method from https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb:

  def redirect_url
if warden_message == :unconfirmed
custom_redirect_path
else
super
end
end

For custom redirect after Sign Up:

There is a controller method after_inactive_sign_up_path_for within the RegistrationsController that you can overwrite to accomplish this.

First in your Routes you will need to specify to use your custom controller:

config/routes.rb:

  devise_for :users, :controllers => { :registrations => "users/registrations" }

Second you create your custom controller that inherits from the normal controller in order to overwrite the method:

app/controllers/users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController

protected

def after_inactive_sign_up_path_for(resource)
signed_up_path
end

end

In this case for my App my Devise model is User so you may want to change that namespace if your model is named differently. I wanted my users to be redirected to the signed_up_path, but you can change that to your desired path.

Allow unconfirmed users to access certain pages which require authentication

Instead of overwriting confirmed? you could just overwrite the confirmation_required? model method (docs):

# user.rb
class User < ActiveRecord::Base
protected

def confirmation_required?
false
end
end

After that you can handle the confirmation logic yourself, by redirecting all unconfirmed users in a before_action when the controllers require confirmation, or you can pull this into your authorization logic (e.g. with pundit).

class ApplicationController < ActionController::Base
def needs_confirmation
redirect_to root_path unless current_user.confirmed?
end
end

class SomeController < ApplicationController
before_action :needs_confirmation
end

devise redirect to a custom url when not confirmed instead of flash notice

If you want to redirect users to a custom URL, you should do it in redirect_url, not in respond:

def redirect_url
if warden_message == :unconfirmed
'/confirm'
else
new_user_registration_path
end
end


Related Topics



Leave a reply



Submit