Customizing Devise authentication, to check for active Chargify subscription

This post is an extension of article. I added further customization to check if user has active chargify_subscription and then only proceed to login otherwise show custom message instead of default devise message. Here is what I did (2 step process). In our user model (specifically user_extensions) override devise method active_for_authentication as follows def active_for_authentication? super && locked? && active_subscription? end Calling super first is very important here, to have devise perform all its regular checks first and then upon success we can proceed to add our custom checks. locked? is

Customizing Devise authentication, to disable or enable user authentication.

If we need to support additional parameters for devise authentication besides normal(username/email with password). We can do this in devise by overiding an existing method, 'active_for_authentication?'. After authenticating a user and in each request, Devise checks if your model is active by calling model.active_for_authentication? Consider a feature 'Lock sub user', here we can add a boolean flag 'is_locked', based on this flag we enable or disable the authentication for the sub-user: def active_for_authentication? super && !is_locked end Instead of !is_locked, we can also add a custom method which:

Recaptcha setup and usage with devise

recaptcha is a gem that uses google's CAPTCHA service. A CAPTCHA is a program that can tell whether its user is a human or a computer. To Setup recaptcha on local machine we need to add the gem, gem "recaptcha", :require => "recaptcha/rails" then we need to obtain private and public key by registering to this url http://www.google.com/recaptcha/whyrecaptcha While registration we can choose if we need public and private keys which work on particular domain or we can choose to use it on any domain. Once we have the public and private key we can

Devise and integrating Omniauth with Devise

Devise is an authentication solution for Rails. It makes use of Warden which is Rack based authentication framework. Steps to integrate Devise: Include devise gem in your gemfile. gem "devise" Run bundle install to install it. Generally, all the login related details are kept in User model. You are free to use any model name as per your needs. Here User model is used as an example. Generate User model using devise generator command: rails generate devise User Above command will generate user model, migration for user and adds devise routes in _routes.rb_. Now you are all set to

Test Unit Macros.

While writing functional test cases, we might need a common utility to be used through our test cases. These utility is generally recognized as "Macros" and more specifically as "Controller Macros". With Rspec we can easily configure such Macros, as Rspec specifically provide a RSpec.configure block to define all our utility and support classes/modules used throughout our test suit. The RSpec.configure block looks like this. RSpec.configure do config.extend(AuthenticateMacro) end With Test::Unit which is a default testing tool provided by rials and its much more liter than Rspec, we can't

Easy filters for Devise controllers

Here's a way to add filters to your controllers without subclassing the Devise controllers. For example, I have a prepare_something method that I want to apply as a before_filter to all of the Devise controller actions. First, create a module in config/initializers/devise_filters.rb where we’ll implement a method to add all the filters to Devise that we want. We also need to make sure this module calls this method upon initialization so that our filters get added upon startup (this is important for the production environment). # Put this somewhere it will get auto-loaded, like