Sinatra integration
The Clerk Ruby SDK provides a seamless integration with Sinatra through a dedicated extension that gives you access to authentication, user management, and organization management features.
The Clerk Ruby SDK provides a range of backend utilities to simplify user authentication and management in your application.
- Add the following code to your application's
Gemfile
. - Run the following command to install the SDK:
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 variable. 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:
For more information, see Faraday's documentation.
Add Clerk's Sinatra extension
The clerk
object provides access to the Ruby SDK's available methods. To get access to the clerk
object, you must register
the Sinatra::Clerk
extension.
The following example demonstrates how to register the Sinatra::Clerk
extension and access the user's User
object.
Example: Protect routes
The auth
filter can be added to any route to protect it from unauthenticated users. If a user is not authenticated, by default, auth
will redirect them to the sign-in page.
Override the default behavior of the auth
filter
By default, the auth
filter will redirect to the sign-in page if the user is not authenticated. You can override this behavior by using set(:auth)
.
In the following example, the auth
filter is overridden to redirect to the homepage if the user is not authenticated.
Example: Reverification
For actions requiring additional security, Clerk provides a reverify
filter that prompts users to re-authenticate. This filter accepts an optional preset parameter to customize the reverification requirements.
In the following example, the /super-secret-admin
or /chill-admin
routes will be protected from unauthenticated users. If the user is authenticated, they will be required to reverify their session, depending on when they last verified their session.
Feedback
Last updated on