How to create path for users and admin after signed in rails 5 - devise

I have a created a rails 5 app using devise gem that has both a user and admin sign in. They are separate modals and have found in my lessons and created the logic for the admin to be directed to the dashboard after signing in however what is the best method to direct the user to the home page after signing in. Below is my set up for the admin and it works for the admin however when I sign in as the user they are directed to the dashboard as well. Outcome I need to see is the user goes to home page after signing in and admin goes to dashboard.
routes:
root 'pages#home'
devise_for :users,
path: '',
path_names: {sign_in: 'user_login', sign_out:
'user_logout', edit: 'user_profile', sign_up: 'user_registration'},
controllers: { omniauth_callbacks:
'users/omniauth_callbacks', registrations: 'registrations' }
devise_for :admins,
path:'',
path_names: {sign_in: 'login', sign_out:
'logout', edit: 'profile', sign_up: 'registration'},
controllers: { omniauth_callbacks:
'admins/omniauth_callbacks', registrations: 'registrations' }
resources :admins, only: [:show]
resources :photos
resources :locations
resources :deals
get 'dashboard' => 'admin_home#dashboard'
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if:
:devise_controller?
protected
def after_sign_in_path_for(_resource_or_scope)
dashboard_path
end

You can use devise methods for this purpose where you can define paths based on some custom checks i.e., user role.
class ApplicationController < ActionController::Base
protect_from_forgery
protected
def after_sign_in_path_for(resource)
sign_in_url = new_user_session_url
if request.referer == sign_in_url
super
else
stored_location_for(resource) || request.referer || root_path
end
end
end
Reference: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in

Related

redirect after logging in rails to desired page

below my the app controller and routes, normally when i open localhost:3000 its show me the positions/new page, now i get redirected to the login page which is good, but after logging in it does nothing .... while it should be routed to 'positions#new'
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
routes:
Rails.application.routes.draw do
devise_for :users
root to: 'positions#new'
resources :positions
end
If u are using Devise you need to overwrite after_sign_in_path method in your SessionsController < Devise::SessionsController
def after_sign_in_path
"/positions/new"
end
Your SessionController should look like this:
class SessionsController < Devise::SessionsController
private
def after_sign_in_path_for(resource)
"/positions/new"
end
end
i added the sessions controller but it doesn't change anything. I also tested it in
pry: ➜ positionupdate git:(master) ✗ rails c
[1] pry(main)> User.count
(0.7ms) SELECT COUNT(*) FROM "users"
=> 0
but i gives me 0 users, while i signed in/up and i have a test user in my seeds, and i runned rails db:drop db:create db migrate db:seed
below my seeds
User.destroy_all
puts 'Creating user...'
users_attributes = [
{
email: 'barcelona#vt.nl',
password: '******',
barge_name: 'Barcelona',
},
]
User.create!(users_attributes)
puts 'Finished!

Get current user in invitations controller

I am using devise_invitable gem in my application.
If a user is already signed in the application and he clicks on the Accept Invitation link then he is redirected to devise after_sign_in_path_for.
I want to change that and give the user some flash that he is signed in the application and needs to sign out before continuing.
After clicking on Accept Invitation link my control goes to Users::InvitationsController#edit. But I am not able to get current_user in Invitations Controller.
My routes and action is as:
devise_for :users, controllers: { registrations: "registrations", :invitations => 'users/invitations', sessions: "sessions", confirmations: "confirmations", passwords: "passwords" }
def edit
Rails.logger.debug current_user.inspect
if current_user.present?
Rails.logger.debug "---current user---"
else
render :edit
end
end
Can some one please suggest how I can get the current user in invitations controller.
This is the line in InvitationsController that prevents current_user from being assigned:
prepend_before_filter :require_no_authentication, :only => [:edit, :update, :destroy]
Add this to your controller and current_user will be assigned:
skip_filter :require_no_authentication, :only => :edit

Devise custom user root is not working

After a user signs in, I want them to be redirected to /users/2 or users/44, like a normal resource. (That way an admin can easily view any user's profile page just by knowing their id.)
I have no idea what is going on in my routes file. When a user logs in, I can see in the logs that it authenticates properly, and that my application_controller's after_sign_in_path_for is called.
But this is the error that I am getting when I try to redirect_to user_path(current_user).
No route matches {:action=>"show", :controller=>"users", :user_id=>11}
My config/routes.rb
devise_for :users, :path_names => {:sign_in => 'signin', :sign_out => 'signout', :sign_up => 'signup' }
devise_scope :user do
get "signin", :to => "devise/sessions#new"
get "signout", :to => "devise/sessions#destroy", via: :delete
get "signup", :to => "devise/registrations#new"
end
resources :users do
member do
get 'settings', to: 'users#edit'
end
end
My application_controller.rb
class ApplicationController < ActionController::Base
...
def after_sign_in_path_for(resource)
redirect_to user_path(:user_id=>current_user.id)
end
end
Shouldn't the resource :users block take care of creating a user show path? I do have a show method in my users_controller.rb. Please help, I've been trying to get this working for 2 days now.
My problem was that I was redirecting in the after_sign_in_path(resource) method instead of just returning the path:
def after_sign_in_path_for(resource)
return user_path(:user_id=>current_user.id)
end
is the correct way to do this.

How to use both Omniauth_callbacks controller and custom devise registrations controller

I have a custom registrations controller for devise set up, which is this:
devise_for :users, controllers: {registrations: "registrations"}
and in the controller:
class RegistrationsController < Devise::RegistrationsController
protected
def after_update_path_for(resource)
user_path(resource)
end
end
It works great.
However I also have omniauth authentication, which again works great...by itself:
devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
flash.notice = "Signed in!"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to sign_up_path
end
end
alias_method :linkedin, :all
alias_method :twitter, :all
end
However as you can probably already see my problem - I'm not sure how to get them to work together, as they both start with 'devise_for :users' and so whichever way round I place them in the routes file, one won't work.
How can I get them both working at the same time, so that the registrations controller only overrides the 'edit' and 'update' actions, while the omniauth_callbacks controller handles authentication?
Thanks
In routes.rb, you can put comma seperated paths for devise_for like this -
devise_for :users, controllers: {registrations: "registrations", omniauth_callbacks: "omniauth_callbacks"}
This will work.

Devise redirect after signin with username in url

I'm struggling with getting devise to redirect to a user's profile page after signin. My routes file looks like this:
get "profiles/index"
get "users/index"
get "users/show"
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
resources :users
scope ":username", :as => "user" do
match '/', :to => 'profiles#index'
end
I would like it to redirect to /myusername which the user's profile page. Thanks for your guys' help.
If you want to change the sign in redirect you can override the after_sign_in_path_for method by adding a new SessionsController as so:
class SessionsController < Devise::SessionsController
#after_sign_in_path_for is called by devise
def after_sign_in_path_for(user)
"/users/#{user.username}" #adjust the returned path as needed
end
end
As Ashikata mentioned you need to change the devise routing to the following if you're changing the session controller.
devise_for :users, :controllers => { :sessions => 'sessions' }
Alternatively, adding that modified after_sign_in_path_for method to your application controller should do the trick.