Explore the Magic of Sorcery with Comprehensive API Examples

Introduction to Sorcery

Sorcery is a powerful library that streamlines authentication and authorization processes in Ruby applications. With its intuitive and flexible API, Sorcery enables developers to implement robust user management features effortlessly.

Getting Started with Sorcery

To begin with Sorcery, add it to your Gemfile:

gem 'sorcery'

Then run the bundle command to install it:

bundle install

Next, generate the Sorcery config file:

rails generate sorcery:install

Useful API Examples

User Authentication

Sorcery makes it easy to handle user authentication:


  class User < ApplicationRecord
    # Authenticating users
    authenticates_with_sorcery!
  end

To configure session timeout and cookie storage:


  Rails.application.config.sorcery.configure do |config|
    config.user_config do |user|
      user.session_timeout = 604800 # 1 week in seconds
      user.remember_me_for = 604800
    end
  end

User Login

Define login actions in the respective controller:


  class SessionsController < ApplicationController
    def create
      if @user = login(params[:email], params[:password])
        redirect_back_or_to(root_path, notice: 'Login successful')
      else
        flash[:alert] = 'Login failed'
        render action: 'new'
      end
    end

    def destroy
      logout
      redirect_to(root_path, notice: 'Logged out')
    end
  end

User Registration

Manage user registrations with ease:


  class UsersController < ApplicationController
    def create
      @user = User.new(user_params)
      if @user.save
        auto_login(@user)
        redirect_to(root_path, notice: 'Account created')
      else
        render :new
      end
    end

    private

    def user_params
      params.require(:user).permit(:email, :password, :password_confirmation)
    end
  end

Complete Application Example

Here is a basic example of how you can integrate Sorcery into a Rails application:


  # Gemfile
  gem 'sorcery'

  # Run bundle install
  bundle install

  # Install Sorcery
  rails generate sorcery:install

  # Migration file for adding Sorcery fields
  rails generate sorcery:install migration
  rake db:migrate

  # User Model (user.rb)
  class User < ApplicationRecord
    authenticates_with_sorcery!
  end

  # Sessions Controller (sessions_controller.rb)
  class SessionsController < ApplicationController
    def create
      if @user = login(params[:email], params[:password])
        redirect_back_or_to(root_path, notice: 'Login successful')
      else
        flash[:alert] = 'Login failed'
        render action: 'new'
      end
    end

    def destroy
      logout
      redirect_to(root_path, notice: 'Logged out')
    end
  end

  # Users Controller (users_controller.rb)
  class UsersController < ApplicationController
    def create
      @user = User.new(user_params)
      if @user.save
        auto_login(@user)
        redirect_to(root_path, notice: 'Account created')
      else
        render :new
      end
    end

    private

    def user_params
      params.require(:user).permit(:email, :password, :password_confirmation)
    end
  end

  # Routes (routes.rb)
  Rails.application.routes.draw do
    root to: 'home#index'
    resources :sessions, only: [:new, :create, :destroy]
    resources :users, only: [:new, :create]
  end

With these steps, you can build an application with robust user authentication and management features using Sorcery.


Hash: 021ad7e84521450daf25e279551937dc686f2b31600e8d20c3199715e828cbef

Leave a Reply

Your email address will not be published. Required fields are marked *