0.12
No release in over 3 years
Low commit activity in last 3 years
A gem that provides authentication Rails helpers when using Warden for authentication
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 1.2.0
 Project Readme

Rails Warden

Build Status

This application adds some nice helpers on-top of the base Warden Rack layer. It aims to make Warden easier to use in Rails-based environments without something as heavy-weight as devise.

Installation

Add this line to your application's Gemfile:

gem 'rails_warden'

And then execute

$ bundle

Or install it yourself as:

$ gem install rails_warden

Usage

Create a new Rails initializer to inject RailsWarden into the Rails middleware stack:

# config/initializers/warden.rb
Rails.configuration.middleware.use RailsWarden::Manager do |manager|
  manager.failure_app = Proc.new { |_env|
    ['401', {'Content-Type' => 'application/json'}, { error: 'Unauthorized', code: 401 }]
  }
  manager.default_strategies :password # needs to be defined
  # Optional Settings (see Warden wiki)
  # manager.scope_defaults :admin, strategies: [:password]
  # manager.default_scope = :admin # optional default scope
  # manager.intercept_401 = false # Warden will intercept 401 responses, which can cause conflicts
end

If you want to customize the Session serializer (optional), add the following to the intializer:

class Warden::SessionSerializer
  def serialize(record)
    [record.class.name, record.id]
  end

  def deserialize(keys)
    klass, id = keys
    klass.find_by(id: id)
  end
end

The next step is to configure warden with some authentication strategies. Check out the warden wiki for that.

Application Mixin

RailsWarden ships with a helpful set of methods and helpers for use inside of ActionController.

To use them, just include them inside your base controller.

class ApplicationController < ActionController::Base
  include RailsWarden::Authentication
end