Devise Layouts for SignedIn & Signed Out resources - ruby-on-rails

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?

Related

Devise multiple model session in same browser

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

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 ;)

Don't allow sign in after sign up in devise

I am using devise for authentication, devise automatically sign in after signing up,
i need just sign up but not sign in.
There is similar question link but it didn't help me
Disclaimer: The following code is not verified in my practice. Just in theory they are likely to work.
At first you need to use your custom RegistrationsController. You can check how to do that in Devise wiki.
After setting up, things are fairly easy. Do the following in your custom controller
class Users::RegistrationsController < Devise::RegistrationsController
def create
super #Nothing special here.
end
protected
def sign_up(resource_name, resource)
true
end
end
How does it work? In Devise's code, #create will call a protected method #sign_up after saving successfully. This method does nothing but sign in the user. What we need to do is to overwrite this method to stop that action. Of course you can even add more of your logic here if necessary.

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

Rails: Multiple layouts with Devise

How can I have a completely different layout depending on wether a user is logged in or not?
Follow the instructions at
https://github.com/plataformatec/devise/wiki/How-To%3a-Create-custom-layouts
and make the check be if the user is logged in, which for devise means checking
user_signed_in?, which is a devise helper.
Specifically:
class ApplicationController < ActionController::Base
layout :layout_by_resource
protected
def layout_by_resource
if user_signed_in?
"special_layout_name_for_logged_in"
else
"application"
end
end
end
and put the special_layout_for_logged_in.html.erb view file in the layouts directory.

Resources