Monban is designed to be a very simple and extensible user authentication library for rails. Its goal is to give all the power to the developer instead of forcing them to make Monban work with their system.
Monban makes authentication simple:
- Easy to use in tests with dependency injection
- Provides convenient controller helpers
- Very customizable
Monban doesn't do the following:
- Doesn't automatically add routes to your application
- Doesn't force you to use engine based controllers or views
- Doesn't require you to make changes to your user model
Monban was designed to work with Rails > 4.0. Add this line to your Gemfile:
gem 'monban'
Then inside of your ApplicationController add the following:
include Monban::ControllerHelpers
And you're ready to start designing your authentication system.
If you don't want to add all the components yourself, you may instead generate a scaffold to start with by running:
rails g monban:scaffold
This will generate a bare bones starting point. If you don't want the full stack you can just generate some controllers with:
rails g monban:controllers
Monban does currently have some out of the box expectations, but you can configure and change any of these:
- By default the model should be called
User - Monban expects your user model to respond to create
- You should have an
emailandpassword_digestcolumn on yourUser - Passwords will be handled with BCrypt
Monban provides the following controller methods:
sign_in(user)sign_outsign_up(user)authenticate(user, password)authenticate_session(session_params)reset_password(user, password)
These helpers:
current_usersigned_in?
And this filter:
require_login
Monban provides the following:
Monban.test_mode!Which will change password encryption to provide plaintext responses instead of using BCrypt. This will allow you to write factories using the password_digest field:
FactoryGirl.define do
factory :user do
username 'wombat'
password_digest 'password'
end
endA couple of convenience methods are available in your tests.
Monban.test_mode!
RSpec.configure do |config|
config.include Monban::Test::Helpers, type: :feature
config.after :each do
Monban.test_reset!
end
endfeature "A feature spec" do
scenario "that requires login" do
user = create(:user)
sign_in(user)
# do something
sign_out
# do something else
end
endIf you are going to write controller tests, helpers are provided for those as well:
Monban.test_mode!
RSpec.configure do |config|
config.include Monban::Test::ControllerHelpers, type: :controller
config.after :each do
Monban.test_reset!
end
endrequire 'spec_helper'
describe ProtectedController do
describe "GET 'index'" do
it "returns http success when signed in" do
user = create(:user)
sign_in(user)
get 'index'
response.should be_success
end
it "redirects when not signed in" do
get 'index'
response.should be_redirect
end
end
endYou may perform a look up on a user using multiple fields by doing something like the following:
class SessionsController < ApplicationController
def create
user = authenticate_session(session_params, email_or_username: [:email, :username])
sign_in(user) do
redirect_to(root_path) and return
end
render :new
end
private
def session_params
params.require(:session).permit(:email_or_username, :password)
end
endThis will allow the user to enter either their username or email to login
Here are a few of the current limitations of monban:
- Monban assumes you only have one user model.
- 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

