The SDK comes with a Rack middleware which lazily loads the Clerk session and user. It inserts a
clerk key in the Rack environment, which is an instance of
Clerk::Proxy. To get the session or the user of the session, you call
user respectively. In case there is no session, you can retrieve the API error with the
error getter method.
If you add the gem in a Rails app, the Rack middleware will be included automatically in the middleware stack. For easier access to the Clerk session and user, include the
Clerk::Authenticatable concern in your controller:
require "clerk/authenticatable"class ApplicationController < ActionController::Baseinclude Clerk::Authenticatableend
This gives your controller and views access to the following methods:
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 AdminsController < ApplicationControllerbefore_action :require_clerk_sessionprivatedef require_clerk_sessionredirect_to clerk_sign_in_url unless clerk_sessionendend
Don't forget to set the environment variable
CLERK_SIGN_IN_URL or the method
clerk_sign_in_url will fail.