Devise multiple model session in same browser - ruby-on-rails

I have two devise model User and Admin. Now does devise support multiple model session in same browser in different tabs. If yes than how can i assign two different layout with two session in progress.
I tried this
layout :layout_by_resource
def layout_by_resource
if devise_controller? and admin_signed_in?
'admin'
elsif devise_controller? and user_signed_in?
'user'
else
'application'
end
end
but this won't work because admin and user both already signed in.
If devise does not support two different model session in same browser then what is the purpose of using devise and how can i accomplish my requirement.

If you follow this https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-Role
You can simply check current_user.admin?
def layout_by_resource
if current_user.admin?
'admin'
elsif current_user
'user'
else
'application'
end
end

Related

rails_admin with devise and single admin user

I'm using Rails 5, rails_admin and devise with the standard devise user model setup:
config.authenticate_with do
warden.authenticate! scope: :user
end
config.current_user_method(&:current_user)
I would like a single user to be able to access the /admin area. Without having to create a new "isAdmin" attribute in the User model, is it possible to simply tell rails_admin that user.id=1 is the only user that can access the /admin area?
This worked for me. In rails_admin.rb, I removed the devise configuration and simply checked for current_user.id to equal 1.
config.authorize_with do
if user_signed_in?
redirect_to main_app.root_path unless current_user.id == 1
else
redirect_to main_app.root_path
end
end
This is simple and does not give away there is an /admin area unless you are the current signed in user and your id == 1

Rails Devise - Different redirect paths on signup

I'm using devise for users but I have two types of users (customers and suppliers), and need different redirect routes based on which path they follow. EG: If a customer signs (/signup) up it will redirect them to their dashboard. If a supplier signs up(/suppliers/registrations/user), it needs to direct them to the next form where they start describing their business(/suppliers/registrations/business). How do you manage this?
UPDATE
I've update my devise registrations controller to include the following (I've excluded all of the commented out stuff)
users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
if resource.supplier == true
redirect_to supplier_business_path
elsif resource.supplier == false
redirect_to user_projects_path(current_user)
end
end
end
But it keeps taking me to the root regardless.
after_sign_in_path_for should just return the path but not perform a redirect, try changing your method like this:
protected
def after_sign_up_path_for(resource)
if resource.supplier == true
# also add specific url = '/suppliers/registrations/user'
supplier_business_path
elsif resource.supplier == false
user_projects_path(current_user)
end
end
also refer this link:
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)
Use devise after_sign_in_path_for method in aplication controller. For eg
def after_sign_in_path_for(resource)
if resource.role == "customer"
redirect_to customer_dashboard_path
elsif resource.role == "supplier"
redirect_to supplier_dashboard_path
end
end

How do I check if my user is an admin in rails even after new http requests?

My user model has the attributes password, password_confirmation, username, email, and admin.
I'm wondering how exactly do I check whether the currently logged in user is an admin. How would I go about the methods? I've tried if user.admin? on my views, but it seems that doesn't work.
I'm new to rails so any suggestions would be helpful!
There is a "session" hash which persists through the whole session.
Once a user has logged in, you would store the current user's id in the session hash, like so
session[:user_id] = user.id
Now, if you want the current user to be accessible from your controllers and in your views, you can go to apps/controllers/application_controller and make some useful methods...
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user, :signed_in?, :is_admin?
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def signed_in?
!!current_user
end
def is_admin?
signed_in? ? current_user.admin : false
end
end
Now in your views you can use "is_admin?" to check if the current user is an admin, use
"signed_in?" to check if a user is signed in, and "current_user" to access the user object if it exists (if no user is logged in, "current_user" will be nil)
Cheers
I suggest you to consult with this Devise guide. It shows how to create a basic user model with help of Devise and how to perform admin checks. And yes, by giving you this link, I strongly encourage you to use Devise gem for users and all that stuff ;)

Login in to main app from active admin

I have app, and connected active admin. I'm trying to let admin user login as any user via active admin without password using devises's sign_in #user method. Is it possible to achieve this out of box?
I can make redirect with username in params/session, but that isnt secure, as if i wouldnt like to pass anything outside active admin.
Any ideas?
I'm not sure exactly what you mean by "not secure"; the devise internals can make sure that you are an admin user, etc, and all you need to pass around is the username of the new user. That said, the code below is entirely within ActiveAdmin and will achieve what you want.
N.B. I can't think of an easy way to sign the Admin user back in (I use the same Devise for all users and use role-based auth).
member_action :sign_in_as, :method => :put do
user = User.find(params[:id])
sign_in user, bypass: true
redirect_to root_path
end
action_item :only => :show do
link_to('Sign in As', sign_in_as_admin_user_path(user), method: 'put')
end
you can do this by using additional method to authenticate admin first like this,
def authenticate_any!
if authenticate_admin_user!
true
else
authenticate_user!
end
end
or overriding devise
module Devise
module Controllers
module Helpers
def authenticate_user!
if authenticate_admin_user!
return true
end
super
end
end
end
end

Devise Layouts for SignedIn & Signed Out resources

My App has two UI states:
- Signed IN
- Signed Out
I've been using the following to determine which app/view/layout to use based on if the user is signed in or out with Devise:
# Devise, way of using a different Layout for all the devise/non-signed in Views
layout :layout_by_resource
def layout_by_resource
if devise_controller?
"application_unauthorized"
else
"application"
end
end
The problem is once your signed in it uses the wrong layout? ideas?
I only want to use "application_unauthorized" if it's devise & the user is not signed in.
Actually you should use the user_signed_in? method to check if the user is signed in. I noticed that current_user? might return true even if the user is currently not signed in.
So your code would look something like this:
layout :layout_by_resource
def layout_by_resource
if user_signed_in?
"application"
else
"application_unauthorized"
end
end
Personally I would check using if current_user? rather than devise_controller?

Resources