Rails Devise Confirmable - redirect - ruby-on-rails

The Rails docs for devise say that the instructions for how to redirect are incorrect:
https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users#redirecting-user
Does anyone know how to do this?

you will need to override devise confirmation controller.
in your routes.rb add this line
devise_for :users, controllers: { confirmations: 'confirmations' }
create file in and paste this code
class ConfirmationsController < Devise::ConfirmationsController
private
def after_confirmation_path_for(resource_name, resource)
your_new_after_confirmation_path #your path where you want to land
end
end
you may also need to restart the server.
Source : https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users#redirecting-user

Related

Rails Authentification with Devise: Redirect to Sign-In form after Sign-Up

I'm building an authentication using Devise.
Creating users works, but after the creation the user is automatically logged-in and becomes redirected to the root-page.
I have created an own RegistrationController and overwritten the after_sign_up_path method:
class RegistrationsController < Devise::RegistrationsController
def after_sign_up_path_for(resource)
"/users/sign_in"
end
end
But it doesn't work.
What have I done wrong, respectively what have I missed.
I have read various other StackOverflow question already. Until now nothing has worked.
Here's my routes.rb too:
Rails.application.routes.draw do
resources :comments
resources :posts
devise_for :users, controllers: { registrations: "registrations" }
root "posts#index"
end
It seems like after you sign up successfully devise automatically log in and you cannot get login page if you still log in, correct me if I'm wrong
This is the default sign up method that ships with the RegistrationsController
# Signs in a user on sign up. You can overwrite this method in your own
# RegistrationsController.
def sign_up(resource_name, resource)
sign_in(resource_name, resource)
end
You can override it in your code.
Thanks to Nick M's answer I could solve the problem the following way:
First make sure your "routes.rb"-file has a line like here ...
Rails.application.routes.draw do
devise_for :users, :controllers => {:registrations => "registrations"}
...
end
Then create a "registrations_controller.rb"-file and add there this:
class RegistrationsController < Devise::RegistrationsController
protected
def sign_up(resource_name, resource)
sign_out :user
end
end
Doing it that way results in being redirected to the Login-form automatically, after creating a user. Exactly the behaviour I liked to accomplish.
"#sign_out(resource_or_scope = nil) ⇒ Object
Sign out a given user or scope. This helper is useful for signing out a user after deleting accounts. Returns true if there was a logout and false if there is no user logged in on the referred scope
sign_out :user # sign_out(scope)
sign_out #user # sign_out(resource)"
Source

Devise: Redirect to different path on failed user authentication

I am struggling to find a way to customize authenticate_user! so that instead of redirecting to the login page, it will redirect to a beta signup page if a user is not logged in.
Is there a way I can change the redirect path to a beta signup page conditionally by adding something like the below to the authenticate_user! method?
ENV['BETA_MODE'] ? redirect_to beta_signup_path : redirect_to login_path
You need to add custom devise controller to your app. You can do it as follow;
In config/routes.rb file;
devise_for :users, controllers: { registrations: "registrations" }
Add following controller content to app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
ENV['BETA_MODE'] ? beta_signup_path : login_path
end
end
More: https://github.com/plataformatec/devise/wiki/how-to:-redirect-to-a-specific-page-on-successful-sign-up-(registration)
I found a fix for this first mentioned here https://groups.google.com/forum/#!topic/plataformatec-devise/qymDM9u9n6Y.
I defined a custom authentication failure class in config/initializers/devise.rb like so:
config.warden do |manager|
manager.failure_app = CustomAuthenticationFailure
end
After doing that I created a file called custom_authentication_failure.rb in /vendor. The contents of that file looked like this:
class CustomAuthenticationFailure < Devise::FailureApp
protected
def redirect_url
ENV['BETA_MODE'] ? beta_signup_path : new_user_session_path
end
end
I also needed to add this in config/application.rb, because it wasn't already there:
config.autoload_paths += %W(#{config.root}/vendor)

How can I override "after_sign_up_path_for" in ActiveAdmin?

I am building a Rails application (using ActiveAdmin and Devise) and I am trying to override the after_sign_up_path_for to change the redirection after signing up.
I followed this tutorial from devise but my RegistrationsController is never called. I guess it might work a little bit differently with ActiveAdmin.
I also tried other solution I found on stack overflow without any luck.
Here is my routes:
Rails.application.routes.draw do
devise_config = ActiveAdmin::Devise.config
devise_config[:controllers][:omniauth_callbacks] = 'users/omniauth_callbacks'
devise_config[:controllers][:registrations] = 'registrations'
devise_for :users, devise_config
ActiveAdmin.routes(self)
# other routes
end
And my RegistrationsController: (which is never called)
class RegistrationsController < ActiveAdmin::Devise::RegistrationsController
protected
def sign_up(_resource_name, _resource)
true
end
def after_sign_up_path_for(_resource)
root_url
end
end
Thanks for your help !
My project:
Rails 4.2.6
ActiveAdmin 1.0.0.pre2
Devise 3.5.9
ActiveAdmin don't use your RegistrationsController and can't use them. You can define that method on your ApplicationController or you can do it this way:
# conig/initializer/active_admin.rb
ActiveAdmin::Devise::RegistrationsController.class_eval do
def after_sign_up_path_for(_resource)
root_url
end
end

How to make Devise redirect after confirmation

How can I create an after-confirmation redirect in Devise?
Before I added the confirmation module the custom after_sign_up_path worked fine for the first time login/signup but now when I click the confirmation link in the email it redirects to the path I set for the after-login path (user profile).
My goal is to create a form wizard and "getting started" page to collect additional information. The obvious caveat being that this redirect will only happen one time, upon confirmation.
I tried some other solutions that have been posted on Stack Overflow but none of them seem to work any longer.
A less intrusive way of achieving this might be just overriding the after_confirmation_path_for method of Devise::ConfirmationsController.
Create a new confirmations_controller.rb in app/controllers directory:
class ConfirmationsController < Devise::ConfirmationsController
private
def after_confirmation_path_for(resource_name, resource)
your_new_after_confirmation_path
end
end
In config/routes.rb, add this line so that Devise will use your custom ConfirmationsController. This assumes Devise operates on users table (you may edit to match yours).
devise_for :users, controllers: { confirmations: 'confirmations' }
Restart the web server, and you should have it.
Essentially, you want to change around line 25 of Devise's ConfirmationsController.
This means you need to override the show action modifying the "happy path" of that if statement in the show action to your heart's content:
class ConfirmationsController < Devise::ConfirmationsController
def new
super
end
def create
super
end
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_navigational_format?
sign_in(resource_name, resource)
respond_with_navigational(resource){ redirect_to confirmation_getting_started_path }
else
respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render_with_scope :new }
end
end
end
And a scoped route for it (I put the view and action in the registrations controller but you can change it to whatever):
devise_for :users, controllers: { confirmations: 'confirmations' }
devise_scope :user do
get '/confirmation-getting-started' => 'registrations#getting_started', as: 'confirmation_getting_started'
end
The default show action is referring to the protected after_confirmation_path_for method, so as another option, you could just modify what that method returns.
Have you checked the Devise wiki? It explains how to do this, with the after_signup_path_for being the path to define in your case.
From the wiki:
Make a new controller "registrations_controller.rb" and customize the appropriate method:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
'/an/example/path'
end
end
Then add a route to use it:
Modify config/routes.rb to use the new controller
devise_for :users, :controllers => { :registrations => "registrations" }
The solution given by #Lee Smith is working perfectly but I wish to add a little addition: We don't need to add the new and create actions while overriding the Devise confirmations controller for this case:
class ConfirmationsController < Devise::ConfirmationsController
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_navigational_format?
sign_in(resource_name, resource)
respond_with_navigational(resource){ redirect_to your_desired_redirect_path }
else
respond_with_navigational(resource.errors, status: :unprocessable_entity){ render_with_scope :new }
end
end
end
Then in the route file, just add routing for the confirmations controller.
devise_for :users, controllers: { confirmations: "confirmations" }
I just went through all of this and none of the other answers worked (2015-04-09 with devise 3.4.1).
After signup, I wanted the user to be redirected to the login page with a message about a confirmation email. To get that working, here's what I had to do:
class RegistrationsController < Devise::RegistrationsController
protected
# This is the method that is needed to control the after_sign_up_path
# when using confirmable.
def after_inactive_sign_up_path_for(resource)
new_user_session_path
end
end
I just found this comment which would have sent me exactly where I needed to be much sooner.
Here is the reference to the after_inactive_sign_up_path_for that
mentions Niels: Devise wiki – marrossa Nov 13 '12 at 3:38
The confirmation_path also must be configured while working with refinerycms integrated in a rails app

Rails 3 /devise seems to ignore custom controller

Using devise with Rails 3 app, I have read wiki/docs on how to customise the after sign up route, I am using confirmable module, so I think I need to override after_inactive_sign_up_path_for
I think I've done it all right, but it is completely ignoring my custom controller and still routing to the root_path after sign up. Driving me nuts.
My registration is using User model, I have copied the views for devise using the generate task; if i move them to views/registrations devise falls back to the default views (in the gem I guess), so it seems not to be 'noticing' my controller
I have this in my routes:
devise_for :users, :controllers => { :registrations => "registrations" }
match 'sign_up_done' => 'home#sign_up_done', :as => :after_sign_up
Here is my controller: (controllers/registrations_controller.rb)
class RegistrationsController < Devise::RegistrationsController
def after_inactive_sign_up_path_for(resource)
after_sign_up_path
end
def after_sign_up_path_for(resource)
after_sign_up_path
end
end
(Added after_sign_up_path_for just in case, using confirmable)
It just seems to completely ignore my controller, is the naming wrong?
Thanks for any input!
I think your folder structure may have problems. try this structure: ( it's the same as those in Gem folder)
app/controllers/devise/registrations_controller.rb
app/views/devise/registrations/new.html.erb
app/views/devise/registrations/edit.html.erb
and the controller file looks the same as it is declared in the gem folder:
#app/controllers/devise/registrations_controller.rb
# NOT: class RegistrationsController < Devise::RegistrationsController ,
# since you are "overwriting" it.
class Devise::RegistrationsController < DeviseController
def after_inactive_sign_up_path_for(resource)
#...
end
end

Resources