Convert Figma logo to code with AI

NoamB logosorcery

Magical authentication for Rails 3 & 4

2,314
385
2,314
75

Top Related Projects

23,904

Flexible authentication solution for Rails with Warden.

Rails authentication with email & password.

OmniAuth is a flexible authentication system utilizing Rack middleware.

A simple ruby authentication solution.

1,448

Magical Authentication

Quick Overview

Sorcery is a Ruby authentication library designed to be flexible, modular, and easy to use. It provides a clean and straightforward way to add authentication to Ruby applications, with support for various authentication methods and customization options.

Pros

  • Highly modular and customizable
  • Supports multiple authentication methods (e.g., password, external providers)
  • Easy to integrate with different ORMs (ActiveRecord, Mongoid, DataMapper)
  • Extensive documentation and active community support

Cons

  • Learning curve for advanced customizations
  • May require additional setup for complex authentication scenarios
  • Some features might be overkill for simple applications
  • Occasional breaking changes between major versions

Code Examples

  1. Basic user authentication setup:
class User < ApplicationRecord
  authenticates_with_sorcery!
end

class UsersController < ApplicationController
  def create
    @user = User.new(user_params)
    if @user.save
      auto_login(@user)
      redirect_to root_path
    else
      render :new
    end
  end
end
  1. Implementing remember me functionality:
class User < ApplicationRecord
  authenticates_with_sorcery! do |config|
    config.authenticates_with :remember_me
  end
end

# In your login action
login(params[:email], params[:password], params[:remember_me])
  1. Adding external authentication provider (e.g., OAuth):
class User < ApplicationRecord
  authenticates_with_sorcery! do |config|
    config.authenticates_with :external
  end
  has_many :authentications, dependent: :destroy
  accepts_nested_attributes_for :authentications
end

# In your controller
def oauth
  if @user = login_from(params[:provider])
    redirect_to root_path
  else
    begin
      @user = create_from(params[:provider])
      auto_login(@user)
      redirect_to root_path
    rescue
      redirect_to root_path, alert: "Failed to login from #{params[:provider]}"
    end
  end
end

Getting Started

  1. Add Sorcery to your Gemfile:

    gem 'sorcery'
    
  2. Run bundle install:

    bundle install
    
  3. Run the Sorcery generator:

    rails generate sorcery:install
    
  4. Run migrations:

    rails db:migrate
    
  5. Add authentication to your User model:

    class User < ApplicationRecord
      authenticates_with_sorcery!
    end
    
  6. Implement login and logout actions in your controller and views as needed.

Competitor Comparisons

23,904

Flexible authentication solution for Rails with Warden.

Pros of Devise

  • More comprehensive out-of-the-box functionality, including built-in views and controllers
  • Larger community and ecosystem, with more plugins and extensions available
  • Better integration with other popular Rails gems and frameworks

Cons of Devise

  • Can be overly complex for simple authentication needs
  • Less flexible and customizable compared to Sorcery's modular approach
  • Steeper learning curve for beginners due to its extensive feature set

Code Comparison

Devise configuration:

devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :validatable

Sorcery configuration:

authenticates_with_sorcery!

Summary

Devise is a more feature-rich and widely adopted authentication solution for Rails, offering a comprehensive set of tools out of the box. It's ideal for projects that require a full-featured authentication system with minimal setup. However, this comes at the cost of flexibility and simplicity.

Sorcery, on the other hand, provides a more modular and lightweight approach to authentication. It offers greater customization options and is easier to understand for beginners. Sorcery is better suited for projects that need a simpler authentication system or require more control over the authentication process.

The choice between Devise and Sorcery ultimately depends on the specific needs of your project and your preference for either a more comprehensive or a more flexible authentication solution.

Rails authentication with email & password.

Pros of Clearance

  • Simpler and more opinionated, making it easier to set up and use out of the box
  • Maintained by thoughtbot, a well-known and respected Rails consultancy
  • Includes built-in views and controllers for common authentication actions

Cons of Clearance

  • Less flexible and customizable compared to Sorcery
  • Fewer authentication options and strategies available
  • May require more work to integrate with existing user models or complex authentication scenarios

Code Comparison

Clearance user model:

class User < ApplicationRecord
  include Clearance::User
end

Sorcery user model:

class User < ApplicationRecord
  authenticates_with_sorcery!
end

Both gems aim to simplify authentication in Rails applications, but they take different approaches. Clearance provides a more opinionated and streamlined solution, while Sorcery offers greater flexibility and customization options.

Clearance is ideal for projects that want a quick and straightforward authentication setup, especially those following Rails conventions. Sorcery, on the other hand, is better suited for applications requiring more complex authentication scenarios or those needing to integrate with existing user models.

Ultimately, the choice between Clearance and Sorcery depends on the specific requirements of your project and your preferred level of control over the authentication process.

OmniAuth is a flexible authentication system utilizing Rack middleware.

Pros of OmniAuth

  • Supports a wide range of authentication providers out-of-the-box
  • Highly modular and extensible architecture
  • Large community and extensive ecosystem of strategies

Cons of OmniAuth

  • Requires additional gems for basic authentication features
  • Can be more complex to set up for simple authentication scenarios
  • Focuses primarily on third-party authentication, less on traditional username/password

Code Comparison

OmniAuth:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']
  provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET']
end

Sorcery:

Rails.application.config.sorcery.submodules = [:external]
Rails.application.config.sorcery.configure do |config|
  config.external_providers = [:github, :google]
  config.github.key = ENV['GITHUB_KEY']
  config.github.secret = ENV['GITHUB_SECRET']
end

Both libraries offer flexible authentication solutions, but OmniAuth specializes in third-party authentication with a wide range of providers, while Sorcery provides a more comprehensive authentication framework with built-in support for traditional authentication methods alongside external providers.

A simple ruby authentication solution.

Pros of Authlogic

  • More mature and established project with a longer history
  • Extensive documentation and community support
  • Flexible configuration options for various authentication scenarios

Cons of Authlogic

  • Heavier and more complex compared to Sorcery
  • Less actively maintained in recent years
  • Steeper learning curve for beginners

Code Comparison

Authlogic example:

class User < ApplicationRecord
  acts_as_authentic do |c|
    c.crypto_provider = Authlogic::CryptoProviders::BCrypt
  end
end

Sorcery example:

class User < ApplicationRecord
  authenticates_with_sorcery!
end

Authlogic requires more configuration in the model, while Sorcery provides a simpler setup. Authlogic offers more granular control over authentication settings, but Sorcery's approach is more straightforward for basic use cases.

Both libraries provide similar core functionality, but Sorcery aims for simplicity and modularity. Authlogic offers more built-in features and customization options out of the box, which can be beneficial for complex authentication requirements but may be overkill for simpler projects.

Ultimately, the choice between Authlogic and Sorcery depends on the specific needs of your project, your familiarity with the libraries, and your preference for configuration vs. convention.

1,448

Magical Authentication

Pros of Sorcery

  • More actively maintained with recent updates and contributions
  • Larger community support and user base
  • Better documentation and examples for implementation

Cons of Sorcery

  • Potentially more complex setup due to additional features
  • May have a steeper learning curve for beginners
  • Slightly larger footprint in terms of codebase size

Code Comparison

Sorcery:

class User < ApplicationRecord
  authenticates_with_sorcery!
  
  validates :password, length: { minimum: 3 }, if: -> { new_record? || changes[:crypted_password] }
  validates :password, confirmation: true, if: -> { new_record? || changes[:crypted_password] }
  validates :password_confirmation, presence: true, if: -> { new_record? || changes[:crypted_password] }
end

NoamB/sorcery:

class User < ActiveRecord::Base
  authenticates_with_sorcery!
  
  validates_length_of :password, minimum: 3, message: "password must be at least 3 characters long", if: :password
  validates_confirmation_of :password, message: "should match confirmation", if: :password
end

Both repositories provide similar functionality for user authentication in Ruby on Rails applications. The main differences lie in the syntax and validation approach. Sorcery offers more flexibility and modern Ruby syntax, while NoamB/sorcery uses a slightly older style of ActiveRecord validations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Sorcery has moved!

You can find sorcery at it's new home: Sorcery/sorcery