Handling requests with Rack and Rails
The Clerk Ruby SDK comes with Rack middleware to lazily load the Clerk session and user. If added as a gem to Rails application, the Clerk::Authenticatable
concern can be added to your controller.
require "clerk/authenticatable"
class ApplicationController < ActionController::Base
include Clerk::Authenticatable
end
This gives your controller and views access to the Clerk helper method:
clerk.session
clerk.user
clerk.user_signed_in?
clerk.sdk
If you want to protect a subset of your controllers (for example, if you have an admin section), you can add a before_filter
like this:
class AdminController < ApplicationController
before_action :require_clerk_session
private
def require_clerk_session
redirect_to clerk.sign_in_url unless clerk_session
end
end
Feedback
Last updated on