LogoLogo
  • Sign-In with Ethereum
    • ⭐Quickstart Guide
      • Creating SIWE Messages
      • Implement the Frontend
      • Implement the Backend
      • Connect the Frontend
      • Implement Sessions
      • Resolve ENS Profiles
      • Resolve NFT Holdings
  • Libraries
    • ⌨️TypeScript
      • Migrating to v2
      • TypeScript Quickstart
    • 🦀Rust
    • 🍷Elixir
    • 🐍Python
    • 💎Ruby
      • 🛤️Rails
    • 💨Go
  • Integrations
    • 💬Discourse
    • 🛡️NextAuth.js
    • 🖼️Auth0
  • Additional Support
    • 🔓Security Considerations
    • 📇ENS Profile Resolution
    • 🫂Community Highlights
  • Servers
    • 🖥️OIDC Provider
      • ⭐Deployment Guide
      • 🔒Hosted OIDC Provider
  • General Information
    • 📒SIWE Overview
      • 📜EIP-4361
      • 🔍Review of Related EIPs
    • 🚨SIWE Code of Conduct
Powered by GitBook
On this page
  • Overview
  • Examples
  • Custom-Controller
  • Rails-Engine
  • OmniAuth
  1. Libraries
  2. Ruby

Rails

Examples of Integrating Sign-In with Ethereum into Rails Applications

PreviousRubyNextGo

Last updated 3 years ago

Overview

Rails is a full-stack framework built on top of Ruby to create web apps. The following are a set of gems and examples to get you started incorporating Sign-In with Ethereum to your Rails application.

Requirements

  • version 2.7.0 or above

  • - can be installed using gem install rails once Ruby is installed

  • wallet

Two gems have been created in order to make different examples of Rails integrations possible:

Which is a Rails gem that adds Sign-In with Ethereum local sign-in routes.

Which provides an strategy for Sign In With Ethereum.

Examples

Currently, there are three examples of Sign-In with Ethereum being used for authentication in Rails applications:

Which shows how to manually add endpoints to generate and verify the Sign-In with Ethereum message, and handle session-based user logins on a Rails application.

Which shows how to use siwe_rails gem to set up and configure the endpoints to generate and verify a Sign-In with Ethereum message in a Rails application.

Which shows how to use and configure the omniauth-siwe provider with OmniAuth in a Rails application.

To get started with any of the examples, clone the siwe-rails-examples repository locally:

git clone https://github.com/spruceid/siwe-rails-examples

When testing, please make sure to update the Ruby version specified in the example's Gemfile to the current version of Ruby that you are using. You can check your version of Ruby by entering ruby -v in your terminal.

To build and test each example, check out the following guides:

Custom-Controller

  • First, enter the custom-controller directory in siwe-rails-examples.

cd siwe-rails-examples/custom-controller
  • Finally, run the following commands to run the example:

bundle install
bin/rails db:migrate RAILS_ENV=development
bundle exec rails server

This executes any database migrations, installs the proper gems, and runs the Rails example. Visit the example by visiting localhost:3000 in your web browser.

You should now see the example, and be able to Sign-In with Ethereum to authenticate and establish a session.

Rails-Engine

  • Clone the siwe_rails gem in the same parent directory as siwe-rails-examples:

# from siwe-rails-examples
cd ..
git clone https://github.com/spruceid/siwe_rails
  • Next, enter the rails-engine directory in siwe-rails-examples.

cd siwe-rails-examples/rails-engine
  • Finally, run the following commands to run the example:

bundle install
bin/rails db:migrate RAILS_ENV=development
bundle exec rails server

This executes any database migrations, installs the proper gems, and runs the Rails example. Visit the example by visiting localhost:3000 in your web browser.

You should now see the example, and be able to Sign-In with Ethereum to authenticate and establish a session.

OmniAuth

  • Clone the omniauth-siwe gem in the same parent directory as siwe-rails-examples:

# from siwe-rails-examples
cd ..
git clone https://github.com/spruceid/omniauth-siwe
  • Next, enter the omniauth directory in siwe-rails-examples.

cd siwe-rails-examples/omniauth
 curl -X POST 'https://oidc.login.xyz/register' \
  -H 'Content-type: application/json' \
  --data '{"redirect_uris": ["http://localhost:3000/auth/siwe/callback"]}'

In this case, since we're running this example on localhost:3000, we need the redirect_uris to contain http://localhost:3000/auth/siwe/callback.

  • After that, update omniauth.rb under omniauth/config/initializers with both the provided identifier (client_id) and secret (client_secret):

omniauth.rb
  client_options = {
    scheme: 'https',
    host: 'oidc.login.xyz',
    port: 443,
    authorization_endpoint: '/authorize',
    token_endpoint: '/token',
    userinfo_endpoint: '/userinfo',
    jwks_uri: '/jwk',
    identifier: 'your-client-id',
    secret: 'your-client-secret'
  }

  provider :siwe, issuer: 'https://oidc.login.xyz/', client_options: client_options
  • Finally, run the following commands to run the example:

bundle install
bundle exec rails server

This installs the proper gems and runs the Rails example. Visit the example by visiting localhost:3000 in your web browser.

You should now see the example, and be able to Sign-In with Ethereum to authenticate and establish a session.

Each of these examples can be found in the repository.

To be able to use you will need to register as a client. To do that, use the following command, filling out your own redirect_uris, according to your setup:

💎
🛤️
Ruby
Rails
MetaMask
siwe_rails
omniauth-siwe
OmniAuth
custom-controller
rails-engine
omniauth-siwe
siwe-rails-examples
oidc.login.xyz