AdminAuth
This is a small gem for rails that extracts simple admin database authentication functionality.
It is written in a way that will work with both ActiveRecord
and Mongoid
and any other ORM that has the methods all
, new
, create
, where
, update_attributes
, destroy
(And they work in the same fashion).
It requires adding a few things to your code, but handles all the things related to authentication on this basic level, including controllers, views, routing, password encryption (with BCrypt).
Installation
Add this line to your application's Gemfile:
gem 'admin_auth'
And then execute:
$ bundle
Or install it yourself as:
$ gem install admin_auth
Usage
To use this gem you need to do few things:
- Create an
Admin
model that has two fields:- email (string)
- encrypted_password (string)
- Add the line
admin_auth_routes
to yourroutes.rb
file. - Now if you add
before_action :authenticate_admin!
to any controller it will send the user to the admin login page. - To create an admin you will need to do it in the rails console:
Admin.create(email: 'email@email.com', password: 'password', password_confirmation: 'password')
- Optionally, add an
after_login_path(locale = nil)
method to yourApplicationController
that returns the path you want to redirect to when logged in. - Optionally, add an
after_logout_path(locale = nil)
method to yourApplicationController
that returns the path you want to redirect to after logout. - Optionally, you can copy the views in the gem to
app/views/admin
and modify then how you see fit.
Contributing
- Fork it ( https://github.com/pboksz/admin_auth/fork )
- 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 a new Pull Request