- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2.3k
Manually
        Munir Ahmad edited this page Dec 18, 2020 
        ·
        9 revisions
      
    gem 'bcrypt'
and run bundler
bundle install
rails g model user name:string password_digest:string
Add has_secure_password to the model
class User < ApplicationRecord
  has_secure_password
end
Edit the config/initializers/rails_admin.rb file and add the authentication.
  config.authenticate_with do
    authenticate_or_request_with_http_basic('Login required') do |username, password|
      user = User.where(name: username).first
      user.authenticate(password) if user
    end
  end
    config.authorize_with do |controller|
      if current_user.nil?
        redirect_to main_app.new_account_session_path, flash: {error: 'Please Login to Continue..'}
      elsif !current_user.admin?
        redirect_to main_app.root_path, flash: {error: 'You are not Admin bro!'}
      end
    end
👉Saving plain text passwords could get you in trouble with GDPR, you have been warned 👈
In config/initializers/rails_admin.rb, you can add the following lines of code:
config.authenticate_with do
  authenticate_or_request_with_http_basic('Login required') do |username, password|
    user = User.where(email: username, password: password, admin: true).first
    user
  end
end
This will call your User object from the database and check if it exists, If yes, it will login else it won't.