DeviseVerifiable adds a second step to registration process.
This is useful to register users with additional information or verify the users identity through a third-party service such as BlockScore.
Simply add DeviseVerifiable to your application's Gemfile:
gem 'devise'
gem 'devise-verifiable'
DeviseVerifiable adds a configuration variable called
fields_for_verification
. It configures which fields will be filled in the verification step:
Devise.setup do |config|
config.fields_for_verification = [:full_name, :address]
end
To include DeviseVerifiable in a model, add the verifiable
module to Devise modules:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :verifiable
end
If you have multiple models configured with devise, define the fields for verification individually using the verify_fields
method:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :verifiable
verify_fields :full_name, :address
end
To restrict unverified users from access certain pages, use the helper
:authenticate_verified_user!
:
class HomeController < ApplicationController
# Only verified users will have access to home controller actions
before_action :authenticate_verified_user!
# ...
end
DeviseVerifiable will add some methods to your model:
#verified? -> this method will check if all fields used for verification are filled.
user = User.new
user.verified? # => false
user.full_name = 'Rodrigo Ra'
user.address = '555, Atomic Avenue'
user.verified? # => true
#valid_for_verification? -> this method will validate all fields used for
verification and add errors messages for invalid fields. By default, it verifies
if the fields are blank?
user = User.new
user.valid_for_verification? # => false
user.errors # => # <ActiveModel::Errors:0x007fe372bcc200 @base=...,
# @messages={:full_name=>["can't be blank"],
# :address=>["can't be blank"]}>
user.full_name = 'Rodrigo Ra'
user.address = '555, Atomic Avenue'
user.valid_for_verification? # => true
user.errors.empty? # => true
#validate_for_verification -> You can customize the validation for fields.
Let's say that you need validate the zip_code
:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :verifiable
verify_fields :full_name, :address, :zip_code
protected
def validate_for_verification(field)
if field == :zip_code
# custom validation for zip_code
else
super
end
end
end
To use a third-party service to verify the user identity.
To customize the verification view, following the devise conventions, create a file in
app/views/devise/verification
called new.html.erb
.
First, configure which controller you want to use in config/routes.rb
:
devise_for :users, controllers: { verification: :custom_verification }
And create it (in this example, CustomVerificationController):
class CustomVerificationController << Devise::VerificationController
def new
end
def create
end
end
This app exemplifies the usage of this gem.
Questions or problems? Please post them on the issue tracker.
You can contribute by doing the following:
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
To test the application run bundle install
and then rake test
.
MIT License.