coinbase-oauth-rails-example 0

3 years after

coinbase-oauth-rails-example

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

Usage

Register an OAuth application

Put application keys in environment variables

Manually

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"]
      @user.credentials.save
    end

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

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.email = auth.info.email
    user.name = auth.info.name
    user.create_credentials! :user => user, :credentials => auth.credentials
  end
end

Accessing the user's account

In app/controllers/coinbase_controller.rb:

    coinbase_credentials = current_user.credentials
    coinbase_credentials.with_lock do
      coinbase = Coinbase::OAuthClient.new(ENV['COINBASE_CLIENT_ID'], ENV['COINBASE_CLIENT_SECRET'], coinbase_credentials.credentials)
      begin
        @balance = coinbase.balance
      ensure
        coinbase_credentials.credentials = coinbase.credentials
        coinbase_credentials.save
      end
    end

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

omniauth-coinbase

omniauth-coinbase

Coinbase OAuth 2 Strategy for Omniauth ...


Top Contributors

aianus cesardeazevedo ilyakatz