The configuration object provides a flexible way to configure the SDK. When a configuration value is not explicitly provided, it will fall back to checking the corresponding environment variableRuby Icon. You must provide your Clerk Secret Key, which can be retrieved from the API keys page in the Clerk Dashboard.
The following example shows how to set up your configuration object:
Clerk.configuredo|c| c.secret_key=`YOUR_SECRET_KEY`# if omitted: ENV["CLERK_SECRET_KEY"] - API calls will fail if unset c.logger=Logger.new(STDOUT) # if omitted, no loggingend
Once you've added the middleware, you can access the clerk object in your actions and views. The clerk object provides access to the Ruby SDK's available methodsRuby Icon.
The following example demonstrates a simple Rack application that protects all routes. If the user is authenticated, it returns the user's first name and ID. If the user is not authenticated, it returns a 401 status code.
app.rb
require"erb"require"clerk"classAppdefcall(env) clerk = env["clerk"]# Check if the user is authenticated user = clerk.user user ? [200, {"Content-Type"=>"text/plain"}, ["Authenticated User: #{user.first_name} (#{user.id})"]]: [401, {"Content-Type"=>"text/plain"}, ["Not Authenticated"]]endend
To handle reverification in your Rack middleware, use the Clerk::Rack::Reverification middleware. It accepts an optional presetRuby Icon parameter to customize the reverification requirements and an optional routes parameter to specify which routes should be protected.
In the following example, the reverification preset is set to LAX and reverification is required for all routes that match the /* pattern.
config.ru
require"rack"require"clerk/rack"require_relative"app"use Clerk::Rack::Middleware# Reverification preset is set to `LAX`use Clerk::Rack::Reverification,preset:Clerk::StepUp::Preset::LAX,routes: ["/*"]run App.new
Use the clerk.user_needs_reverification? method to check if the user needs to reverify their session, which accepts an optional presetRuby Icon parameter to customize the reverification requirements.
Use the clerk.user_reverification_rack_response method to get the response.
The following example demonstrates a simple Rack application that requires authentication and reverification for all routes.
app.rb
require"erb"require"clerk"STEP_UP_PRESET=Clerk::StepUp::Preset::LAXclassAppdefcall(env) clerk = env["clerk"]# Check if the user needs to reverify their sessionif clerk.user_needs_reverification?(STEP_UP_PRESET)# Get the responsereturn clerk.user_reverification_rack_response(STEP_UP_PRESET)end# Check if the user is authenticated user = clerk.user user ? [200, {"Content-Type"=>"text/plain"}, ["Authenticated User: #{user.first_name} (#{user.id})"]]: [401, {"Content-Type"=>"text/plain"}, ["Not Authenticated"]]endend