Add Authentication to your Rails App using Clerk.dev

Clerk. dev is designed to make developers' lives easier by handling the complex parts of user management, authentication and security so you can build your apps faster.

After using clerk in my node applications I was curious to see how different it is from using devise gem and the built-in rails authentication methods. My observation is that Clerk reduces boilerplate code, provides more out-of-the-box functionality and handles security aspects on your behalf. This comes at a cost, however, since Devise and Rails auth offer more customization if you want to implement everything yourself.

Some of the key differences between Clerk and Devise are;

  • Clerk is a fully hosted service, while Devise and Rails auth require you to implement everything yourself. With Clerk, user data, passwords, sessions etc are managed on their infrastructure.

  • Clerk has a dashboard, user management UI, and other tools optimized for auth. Devise and Rails auth are just libraries that give you lower-level building blocks.

  • Clerk handles more auth functionality out of the box like social login, MFA, magic links, etc. These require more customization with Devise/Rails.

  • Clerk has client-side JS for UI components like login/register forms. Devise and Rails auth are backend only.

  • Clerk has SDKs and improves developer experience. Devise and Rails auth require more boilerplate code from developers.

  • Clerk follows security best practices by default. More responsibility falls on developers to secure Devise/Rails auth.

Getting Started

To get started with Clerk you need to install the gem by adding it to your Gemfile:

gem 'clerk-sdk-ruby', require: "clerk"

and then execute

bundle install

After the gem is installed we need to get the API KEYS. Specifically the CLERK_SECRET_KEY.

Other than the key you also need the CLERK_SIGN_IN_URL and the CLERK_SIGN_UP_URL

These three will go to your .env file which now looks like

CLERK_SECRET_KEY=
CLERK_SIGN_IN_URL=
CLERK_SIGN_UP_URL=

Next, you need to configure our initializer, do this by creating a file called clerk.rb in config/initializers/clerk.rb and have the following code.

Clerk.configure do |config|
  config.api_key = ENV['CLERK_SECRET_KEY']
  config.logger = Logger.new(STDOUT)
  config.middleware_cache_store = Rails.cache
  config.base_url="https://api.clerk.dev/v1/"
end

Clerk offers easy access to the clerk session and user, through adding the Clerk::Authenticatable concern to your application_controller.rb

require "clerk/authenticatable"
class ApplicationController < ActionController::Base
  include Clerk::Authenticatable
end

Lastly, you need to add it to your controller:

class HomeController < ApplicationController
  before_action :require_clerk_session
  def index
    @user = clerk_user
  end


  private
  def require_clerk_session
    redirect_to clerk_sign_in_url, allow_other_host: true unless clerk_session
  end
end

That's it, you now have Clerk added to your app!