Creating Multiple User Sessions Using Devise in Rails

Total Blog Views: 190

Blog Status: publish

Created By: swaz_ahmed Created at: 06-18-2024

Tags: ror devise

In modern web applications, it's common to have different types of users, such as admins and customers, each with their own session. In this blog post, we'll walk through the steps to configure a Rails application to support multiple user sessions using the Devise gem.

Step 1: Setting Up Your Rails Application

First, ensure you have a Rails application with Devise installed. If you don’t have Devise installed yet, add it to your Gemfile and run the necessary installation commands:

Gemfile:

 

gem 'devise'

 

Install the gem by running: 

 

bundle install

 

Next, install Devise:

 

rails generate devise:install

 

Step 2: Generate User Models

Generate multiple user models using Devise. For example, let’s create Admin and Customer models:

 

rails generate devise Admin
rails generate devise Customer

This command will create the models and Devise configuration files for each user type.

Step 3: Configure Routes

Update your config/routes.rb file to define routes for each user type:

config/routes.rb:

 

Rails.application.routes.draw do
  devise_for :admins
  devise_for :customers
  root to: 'home#index' # Adjust as necessary
end

 

Step 4: Set Up Controllers

If you need custom controllers for each user type, generate Devise controllers for them:

 

rails generate devise:controllers admins
rails generate devise:controllers customers

Update the routes to use these controllers:

config/routes.rb:

 

Rails.application.routes.draw do
  devise_for :admins, controllers: {
    sessions: 'admins/sessions',
    registrations: 'admins/registrations'
  }
  devise_for :customers, controllers: {
    sessions: 'customers/sessions',
    registrations: 'customers/registrations'
  }
  root to: 'home#index' # Adjust as necessary
end 

 

Step 5: Update Views

To customize the views for each user type, generate Devise views:

 

rails generate devise:views admins
rails generate devise:views customers

This will create separate view directories for each user type, which you can customize independently.

Step 6: Configure Initializers

Ensure that your Devise initializer (config/initializers/devise.rb) is properly configured. You may need to adjust the configuration if you have any custom settings.

Step 7: Set Up Multiple User Sessions

To handle multiple user sessions, you may need to tweak the session storage mechanism. Devise uses warden for authentication, which supports multiple scopes.
Ensure that config/initializers/devise.rb includes the following configuration:

config/initializers/devise.rb:

 

Devise.setup do |config|
  # ==> Warden configuration
  config.warden do |manager|
    manager.default_strategies(scope: :admin).unshift :some_admin_strategy
    manager.default_strategies(scope: :customer).unshift :some_customer_strategy
  end
end

 

Step 8: Customizing Sessions

If you need to add custom logic to sessions, you can override the Devise controllers and add your logic:

app/controllers/admins/sessions_controller.rb:

 

class Admins::SessionsController < Devise::SessionsController
  # Add custom logic here
end

 

app/controllers/customers/sessions_controller.rb:

 

class Customers::SessionsController < Devise::SessionsController
  # Add custom logic here
end 

 

Step 9: Test Your Setup

Finally, test your setup to ensure that each user type can sign up, log in, and maintain separate sessions. Create some views and links for logging in and out as different user types:

app/views/home/index.html.erb:

 

<%= link_to 'Admin Sign Up', new_admin_registration_path %>
<%= link_to 'Admin Log In', new_admin_session_path %>
<%= link_to 'Customer Sign Up', new_customer_registration_path %>
<%= link_to 'Customer Log In', new_customer_session_path %>

 

Conclusion:
By following these steps, you can set up a Rails application with multiple user sessions using Devise. Each user type will have its own sessions, views, and controllers, allowing for a clean separation of logic and user experience. This setup is ideal for applications that need to manage different types of users with distinct roles and permissions.

 


swaz_ahmed

I am swaz_ahmed blogger on shadbox. I am influencer,content writer,author and publisher. Feel free to ask me any question and suggestions.



Comments



Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads