Check if user is active before allowing user to sign in with devise (rails)
Add these two methods to your user model, devise should pick them up automatically - you should NOT need to extend Devise::SessionsController
def active_for_authentication?
super && self.your_method_for_checking_active # i.e. super && self.is_active
end
def inactive_message
"Sorry, this account has been deactivated."
end
Devise log in with one more condition to check
You should look at adding :confirmable
to your User
model, it may take care of most of what you are looking to do.
Otherwise, if you want to modify whether someone can login, look at the wiki on how to customize account validation.
From the wiki:
def active_for_authentication?
# Uncomment the below debug statement to view the properties of the returned
# self model values.
# logger.debug self.to_yaml
super && account_active?
end
Rails Devise check if User has not Logged in
You can use
<% if !user_signed_in? %>
or
<% if not user_signed_in? %>
or
<% unless user_signed_in? %>
!
and not
negate the preceding boolean expression, while unless
is the exact opposite of if
.
Ruby on Rails: Edit Sessions controller create action to check if user is active
The most simple way to do this is really just add a custom valid_for_authentication? method in the resource devise is using, so in Users.rb:
def valid_for_authentication?
super && !deactivation_date?
end
Show a page only if user is signed in? Ruby/Devise
You could do something on the page like this:
<% if user_signed_in? %>
<div> show content here </div>
<%else %>
<p>Please Sign Up or Sign In to view the content.</p>
<h2><%= link_to "Sign up", new_user_registration_path%>
<% end %>
Hope it helps!
Or setup a before_action
inside the controller:
before_action :authenticate_user!
The official documentation has details
Devise before_authenticate?
I've found that you can stack before_filter
s in the controller, so if you wanted to check for an authenticate_user!
, you could also use a before_filter
(after authenticate_user!
) to check for a locked user. If the user model has a boolean attribute locked
, you can simply write a private method in your controller (or helper) like this:
#top of controller
before_filter authenticate_user!
before_filter user_active!
#bottom of controller
private
def user_active!
unless current_user.locked?
return true
end
redirect_to root_url, :notice => "Your account is locked."
return false
end
This will give you the page you want if you're an unlocked user, and redirect you to the root page with an error message if the user is locked.
Related Topics
How to Get the Parent's Class Name in Ruby
Can You Use Semicolons in Ruby
Check If User Is Active Before Allowing User to Sign in with Devise (Rails)
Ruby Accessing Outer Variables in Nested Function
Seeding File Uploads with Carrierwave, Rails 3
Why Does Ruby "Script/Generate" Return "No Such File or Directory"
How to Run Selenium (Used Through Capybara) at a Lower Speed
How to Make Nokogiri Not to Convert &Nbsp; to Space
Using Ruby Libraries and Gems with a Shoes App
How to Ban/Block Users with Devise for Rails
How to Install Ruby on Rails 3.0 on Ubuntu 10.10
Stream Console Output Through Http (With Ruby)
Rspec 3 How to Test Flash Messages
Undefined Method 'Devise' for User
How to Get Url of Active Storage Image
Failed to Build Gem Native Extension When Install Redcloth-4.2.9 Install Linux