How to set rails devise sign_in path as a root url - ruby-on-rails

devise_for :admins, path: 'admins'
devise_scope :admin do
root to: "devise/sessions#new"
end
http://localhost:3000/
I want to redirect admins/sign_in path when I just enter above url, login page is opening sometimes but after click on login button every time I get this error and not sign in. How to solve this problem?
error:
Filter chain halted as :require_no_authentication rendered or
redirected

look like you're trying to log in the same user again without a sign out
devise_for :admins, path: 'admins'
devise_scope :admin do
authenticated :admin do
root 'home#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end

You can achieve the same with this
routes.rb
root "home#index"
devise_for :admins, path: 'admins'
home_controller.rb
class HomeController < ApplicationController
def index
if not admin_signed_in?
redirect_to admin_session_path
end
end

A logged user can't sign in again...
You can try this, in your session_controller.rb add
class Users::SessionsController < Devise::SessionsController
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
end

Related

show errors in custom page rather than in default page while login/signup with devise

I have put login and signup in one page and every thing works fine except when I encounter errors. Then the page redirects to their default pages and show errors there. In my case the login redirects me to the default domain.com/users/sign_in , but signup redirects me to domain.com/users.
routes.rb
Rails.application.routes.draw do
root 'visitor#index'
namespace :admin do
# get "/stats" => "stats#stats"
devise_scope :admin_user do
get '/stats/:scope' => 'stats#stats', as: :admin_stats
end
end
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
namespace :client do
get 'dashboard' => 'dashboard#index', as: 'dashboard'
# resources :verification, only: [:create, :index, :destroy]
get 'verification' => 'verification#index', as: 'verification'
match 'verification' => 'verification#upload', as: 'verification_upload', via: [:post, :patch]
end
devise_for :users, class_name: 'FormUser', controllers: { omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations' }
# devise_scope :user do
# root to: 'devise/registrations#new'
# end
end
you can use a CustomFailure class to control where the redirect goes if Devise fails to authenticate.
It's explained at this wiki page...
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

Redirect to appropriate pages after logging in

I have installed devise and activeadmin gem. When I try to login via client then I get redirected to the right page but when I try to login via admin I get redirected to the client login page.
routes.rb
namespace :admin do
# get "/stats" => "stats#stats"
devise_scope :admin_user do
get '/stats/:scope' => "stats#stats", as: :admin_stats
end
end
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
namespace :client do
get 'dashboard' => 'dashboard#index', as: 'dashboard'
end
devise_for :users, class_name: 'FormUser', controllers: { omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations' }
devise_scope :user do
root to: 'devise/registrations#new'
end
application_controller.rb
def after_sign_in_path_for(resource_or_scope)
client_dashboard_path
end
def after_sign_out_path_for(resource_or_scope)
root_path
end
How can I fix this?
Here is a link to the test app enter link description here
Admin login enter link description here
def after_sign_in_path_for(resource_or_scope)
case resource_or_scope
when AdminUser
admin_dashboard_path
when User
client_dashboard_path
end
end

Devise root - disable must sign-in

Whenever I access my home page or root, I'm redirected to Devise's /users/sign_in/. I've played with the routes.rb file a fair amount and I'm unable to figure out why Devise kicks in for the pages controller. The Pages controller is effectively empty.
routes.rb
Rails.application.routes.draw do
resources :pages
root 'pages#index'
resources :events do
member do
get :create_attendee
delete :destroy_attendee
end
end
devise_for :users
devise_scope :user do
authenticated :user do
root 'events#index', as: :authenticated_root
end
unauthenticated do
root 'pages#index', as: :unauthenticated_root
end
end
pages_controller.rb
class PagesController < ApplicationController
def index
end
end
Add before_filter :authenticate_user!, except: [:index] to your pages_controller.rb and it'll skip the auth.
The authenticate_user! action is triggered for all controller method calls except index, i.e, for create, update... That way, you don't to go through log in page.

Redirect user to Sign In page after Sign Up (registration)

I am using Devise 3.1.1 and am trying to redirect user to the Sign In page after he signs up.
As instructed in Devise's wiki I overridden RegistrationsController with the following:
class RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(resource)
'/users/sign_in'
end
end
As instructed, I also added the following line to the routes.rb:
devise_for :users, controllers: { registrations: 'registrations'}
After which I get the following error when I go to sign in page:
Invalid route name, already in use: 'new_user_session' You may have defined two routes with the same name using the:asoption, or you may be overriding a route already defined by a resource with the same naming.
In my routes I already have this defined:
devise_for :users, skip: :registrations
devise_scope :user do
resource :registration,
# disabled :edit & :destroy
only: [:new, :create, :update],
path: 'users',
path_names: { new: 'sign_up' },
controller: 'devise/registrations',
as: :user_registration do
get :cancel
end
end
You can only define the devise_for block once, and as you're already messing with the default registrations controller you should be able to just do something like the following to have devise use your controller:
devise_for :users, skip: :registrations
devise_scope :user do
resource :registration,
# disabled :edit & :destroy
only: [:new, :create, :update],
path: 'users',
path_names: { new: 'sign_up' },
controller: 'registrations',
as: :user_registration do
get :cancel
end
end

Devise with rails 4 authenticated root route not working

What I'm trying to do
I want to send a user to the registrations#new page if they aren't logged in.
After you enter login info and click submit, I want you to be sent to the registrations#show page.
What is happening
It sends you to the registrations#new page when you're not logged in (correct so far). But when you submit the login form, it sends errors out with a redirect loop. The server's output is just this block repeated over and over:
Started GET "/" for 127.0.0.1 at 2013-09-25 02:31:59 -0400
Processing by RegistrationsController#new as HTML
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 8 ORDER BY "users"."id" ASC LIMIT 1
Redirected to http://lvh.me:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.7ms)
I can't seem to figure it out. It does log you in, as I can see that by manually navigating to a different page, but the authenticated root path is not working correctly. I've tried a few different combinations in my routes file and can't seem to get it. The code I'm using is based off of this thread
My code
In my application controller I have before_filter :authenticate_user!
My routes file:
devise_for :users, :controllers => {
:registrations => "registrations"
}
devise_scope :user do
root to: "registrations#new"
end
authenticated :user do
root to: "registrations#show", :as => "profile"
end
unauthenticated do
root to: "registrations#new", :as => "unauthenticated"
end
First, you should customize Devise::RegistrationsController (you can add file app/controllers/registrations_controller.rb)
And see prepend_before_filter on devise registrations_controller.rb
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
Add show action to prepend_before_filter :authenticate_scope!
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy, :show]
# GET /resource/sign_up
def new
super
end
# POST /resource
def create
super
end
# GET /resource/edit
def edit
super
end
def update
super
end
# DELETE /resource
def destroy
super
end
def show
end
protected
def after_sign_up_path_for(resource)
after_sign_in_path_for(resource)
end
end
Also copy the view of devise registration (edit and new template) to /app/views/registrations/ and You can make a file show.html.erb in /app/views/registrations/ folder for a profile page.
For routes of devise looks like :
devise_for :users, :skip => [:registrations]
devise_for :users, :controllers => {
:registrations => "registrations"
}
authenticated :user do
devise_scope :user do
root to: "registrations#show", :as => "profile"
end
end
unauthenticated do
devise_scope :user do
root to: "registrations#new", :as => "unauthenticated"
end
end
Last, you to set a "path" in after_sign_in_path_for(resource) and after_sign_out_path_for(resource_or_scope) at file application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
private
def after_sign_in_path_for(resource)
# After you enter login info and click submit, I want you to be sent to the registrations#show page
profile_path
end
def after_sign_out_path_for(resource_or_scope)
new_user_session_path
end
end
Note: I have tried this (to make sample apps), and it works. See log when sign in here, and log sign out here
The redirect to registration#new is default so all you need to do is this (in your route file):
devise_for :users, :controllers => {
:registrations => "registrations"
}
devise_scope :user do
root to: "registrations#show" # This is the root path of the user when you are logged in
end
unauthenticated do
root to: "registrations#new", :as => "unauthenticated"
end
# I dont't think this part is neccesary... Try if it works without
authenticated :user do
root to: "registrations#show", :as => "profile"
end
Do not use routes to do the jobs belonging to controller.
To redirect an user to certain page after signing up, use Devise built-in after_sign_up_path_for and override it.
class ApplicationController
def after_sign_up_path_for(resource)
faked_user_profile_path(resource)
end
end
For routes, I'm not very aware of all of them except devise_for part. But for the redirecting feature, this overriding should be enough.

Resources