coinbase-oauth-rails-example 0

3 years after


An example rails app that demonstrates accessing user's coinbase accounts using the devise, omniauth-coinbase, and coinbase gems.


Register an OAuth application

Put application keys in environment variables


export COINBASE_CLIENT_ID=your_coinbase_app_client_id
export COINBASE_CLIENT_SECRET=your_coinbase_app_client_secret

Using figaro

touch config/application.yml
echo "COINBASE_CLIENT_ID: your_coinbase_client_id" >> config/application.yml
echo "COINBASE_CLIENT_SECRET: your_coinbase_app_client_secret" >> config/application.yml

NOTE: Do not check this file into version control!

Run the server locally

bundle install
bundle exec rake db:reset
bundle exec rails server

Then navigate to the root path (default would be 'http://localhost:3000/')

You will be redirected to Coinbase and asked for access to your basic account information and balance. Upon approving the access, you will be redirected back to the root path where you will be shown your balance.

Code Highlights

Configuring the omniauth provider

In config/initializers/devise.rb:

config.omniauth :coinbase, ENV["COINBASE_CLIENT_ID"], ENV["COINBASE_CLIENT_SECRET"], scope: 'user balance'

Here we configure the omniauth provider with the app credentials and the permissions we want to request our users to grant. The default is 'all' and it's mandatory to have at least one of 'all' and 'user'. A full list can be seen here.

Handling the omniauth callback

First we tell devise which controller to use to handle the omniauth callbacks

In config/routes.rb

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

Then we define a method with the same name as the provider ('coinbase'), look up or create a new user based on his uid, and sign him in.

In app/controllers/users/omniauth_callbacks_controller.rb:

def coinbase
    @user = User.find_for_oauth(request.env["omniauth.auth"])

    @user.credentials.with_lock do
      @user.credentials.credentials = request.env["omniauth.auth"]["credentials"]

    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication
      session["devise.omniauth_data"] = request.env["omniauth.auth"]

In app/models/user.rb

def self.find_for_oauth(auth)
  where(auth.slice(:provider, :uid)).first_or_create do |user|
    user.provider = auth.provider
    user.uid = auth.uid = =
    user.create_credentials! :user => user, :credentials => auth.credentials

Accessing the user's account

In app/controllers/coinbase_controller.rb:

    coinbase_credentials = current_user.credentials
    coinbase_credentials.with_lock do
      coinbase =['COINBASE_CLIENT_ID'], ENV['COINBASE_CLIENT_SECRET'], coinbase_credentials.credentials)
        @balance = coinbase.balance
        coinbase_credentials.credentials = coinbase.credentials

First we instantiate a Coinbsae OAuthClient object using the logged in user's OAuth credentials, then we make a call to find out his balance, and finally we save his credentials in case the call to balance required the access token to be refreshed.

Related Repositories



Coinbase OAuth 2 Strategy for Omniauth ...

Top Contributors

aianus cesardeazevedo ilyakatz