Devise and Mongoid in Rails 4
To use MongoDB as a database in rails application we need to use mongoid.
mongoid #
Mongoid is an Object-Document-Mapper (ODM) for MongoDB written in Ruby
In a Rails application mongoid provide functionality like ActiveRecord, but not exactly same, because MongoDB is a document-orinted database, and that mappers is called Object Document Mappers (ODM) which maps between an Object Model and a Document Database
To use mongoDB, most of the configuration comes down by making sure that we are am not loading ActiveRecord. One way to avoid loading ActiveRecord at time of creating rails project using following switch
—skip-active-record
The Rails command that generate application skeleton now has an option -O , which commands rails to skip active record.
So, the final app skeleton will look like so:
rails new appName -T -O
open your Gemfile and add:
source 'https://rubygems.org'
source 'http://gems.github.com'
gem 'mongoid', github: "mongoid/mongoid"
gem 'bson_ext'
gem 'devise'
Now run:
bundle
create your application using(for this case to save time)
rails generate scaffold Movie name:string storyline:string
mongoid configuration #
we can configure Mongoid through a mongoid.yml file to specify options and database sessions.
we can also generate a config file by executing the following generator:
rails g mongoid:config
Devise #
Devise is a Rack based authentication solution for Rails based on Warden.
configure devise #
now run the generator:
rails g devise:install
Then create a model for authentication, generally it is named User (or Admin)
lets use the generator to do the same
rails generate devise User
It will invoke mongoid and generate cupple of files including a new entry in routes.rb.
invoke mongoid
create app/models/user.rb
insert app/models/user.rb
insert app/models/user.rb
route devise_for :users
Note: rails destroy devise User
command may use to rollback.
invoke mongoid
remove app/models/user.rb
route devise_for :users
If we take a look into our newly generated User model:
class User
include Mongoid::Document
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
field :email, type: String, default: ""
......
......
......
end
devise composed of 10 modules (from devise wiki):
Database Authenticatable: this module responsible for encrypting password and validating authenticity of a user while signing in.
Registerable: handles signing up users through a registration process, also allowing them to edit and destroy their account.
Recoverable: resets the user password and sends reset instructions.
Rememberable: manages generating and clearing a token for remembering the user from a saved cookie.
Trackable: tracks sign in count, timestamps and IP address.
Timeoutable: expires sessions that have no activity in a specified period of time.
Validatable: provides validations of email and password.
Lockable: locks an account after a specified number of failed sign-in attempts.
Omniauthable: adds Omniauth support.
Confirmable: sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.
All set of configuration is done now we going to use it:
we can add some links to access user ‘sign in’ or ‘sign up’ path. To access those link throughout the application I going to add it in views/layout/application.rb
<div>
<% if user_signed_in? %>
Welcome <%= current_user.email %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to "Sign in", new_user_session_path%> or <%= link_to "Sign up", new_user_registration_path %>
<% end %>
</div>
To protect controller add a filter, we are going to protect all other action other than movie listing page:
# In our controllers
before_filter :authenticate_user!, :except => [:index]
That’s it :)