āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā š shadcn/directory/clerk/clerk-docs/reference/ruby/rack ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
The Clerk Ruby SDK provides a Rack middleware to integrate Clerk into your Rack-based application.
<Steps> ## Install `clerk-sdk-ruby` <Include src="_partials/ruby/installation" />Add the Clerk middleware to your Rack application by updating your config.ru file with the following code:
require "rack"
require "clerk/rack"
require_relative "app"
use Clerk::Rack::Middleware
run App.new
clerk objectOnce 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 methods.
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.
require "erb"
require "clerk"
class App
def call(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"]]
end
end
</Steps>
The reverification feature provides an additional layer of security by requiring users to reverify their session before accessing sensitive routes.
There are two ways to handle reverification in a Rack application:
To handle reverification in your Rack middleware, use the Clerk::Rack::Reverification middleware. It accepts an optional preset 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.
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
To handle reverification in your app logic,
clerk.user_needs_reverification? method to check if the user needs to reverify their session, which accepts an optional preset parameter to customize the reverification requirements.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.
require "erb"
require "clerk"
STEP_UP_PRESET = Clerk::StepUp::Preset::LAX
class App
def call(env)
clerk = env["clerk"]
# Check if the user needs to reverify their session
if clerk.user_needs_reverification?(STEP_UP_PRESET)
# Get the response
return 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"]]
end
end
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā