Reputation: 1271
I would like to change the user sign-in behaviour of devise in a way to redirect a user to a different page when that user is not confirmed.
I have the following code in my app/controllers/sessions_controller.rb
:
# POST /user/sign_in
def create
@user = warden.authenticate!(:scope => :user)
sign_in @user
respond_with @user, :location => after_sign_in_path_for(@user)
end
When the user has put correct username/password and is not confirmed Warden will raise an error that will redirect the user to the sign in page (code 302). At the same time this will put flash[:alert] to "unconfirmed".
Is there a way to redirect an unconfirmed user to a specific page?
My idea to work around this was reading the flash[:alert] value and redirecting to the appropriate page if the value is "unconfirmed" but warden is not sending additional data about the user.
Upvotes: 4
Views: 1523
Reputation: 549
You need a custom FailureApp to do this (and you need to define /unconfirmed in your routes, etc., like any other Rails action):
lib/custom_failure.rb
class CustomFailure < Devise::FailureApp
def redirect_url
if warden_message == :unconfirmed
'/unconfirmed'
else
super
end
end
# You need to override respond to eliminate recall
def respond
if http_auth?
http_auth
else
redirect
end
end
end
config/initializers/devise.rb:
config.warden do |manager|
manager.failure_app = CustomFailure
end
Upvotes: 2
Reputation: 6415
I think that to get this working you need to combine Brian's answer with an addition to your routes.rb file.
I'm going to test it out right now so I'll give you an update if/when I get it working. (EDIT: I've got this working locally).
Here's what you need to do:
Create an new file in your_rails_app/app/controllers
called sessions_controller.rb
which should look like this:
class SessionsController < Devise::SessionsController
def create
@user = User.where(:email => params[:user][:email])[0] # you get the user now
if @user.confirmed?
super
else
redirect_to YOUR_DESIRED_path # or something else you want to do
end
end
end
Add the following line to your routes.rb file (or modify the line in your routes.rb file that contains devise_for :users
...or whatever your model is called):
devise_for :users, controllers: { sessions: "sessions" }
You need to add this to your User
model:
protected
def confirmation_required?
false
end
Upvotes: 0
Reputation: 2197
Please put this line into devise.rb, if you do not want to allow unconfirmed user to sign in after successful registration.
config.allow_unconfirmed_access_for = nil
Upvotes: -1
Reputation: 31302
class SessionsController < Devise::SessionsController
def create
@user = User.where(:email => params[:user][:email])[0] # you get the user now
if @user.confirmed?
super
else
redirect_to YOUR_DESIRED_path # or something else you want to do
end
end
end
Upvotes: 2