Official OmniAuth strategy for authenticating to use Gusto’s API with OAuth 2.
Add this line to your application's Gemfile:
gem 'omniauth-gusto'
And then execute:
$ bundle
Or install it yourself as:
$ gem install omniauth-gusto
Integrate the strategy into your middleware. Refer to Devise’s documentation if using Devise.
# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :gusto, ENV['GUSTO_CLIENT_ID'], ENV['GUSTO_CLIENT_SECRET']
end
The route /auth/gusto
will become available to initiate authentication.
Set up the callback route /auth/gusto/callback
and tie to a controller action
to handle any post-authentication actions.
# config/routes.rb
Rails.application.routes.draw do
get '/auth/gusto/callback', to: 'sample_controller#gusto'
end
For more information on how to use OmniAuth, refer to the OmniAuth documentation.
Use the access token from the Auth Hash request.env['omniauth.auth']['credentials']['token']
to
make calls to other Gusto API endpoints
such as payrolls.
To gain access to Gusto’s API, contact Gusto to establish a client id and secret.
Below is an example Auth Hash availble in request.env['omniauth.auth']
. Note info['name']
is set to the user’s email and that employee names will have to be fetched from the employees endpoint.
{
"provider" => "gusto",
"uid" => 123,
"info" => {
"email" => "[email protected]",
"name" => "[email protected]"
},
"credentials" => {
"token" => "456",
"refresh_token" => "789",
"expires_at" => 1561589955,
"expires" => true
},
"extra" => {
"raw_info" => {
"id" => 123,
"email" => "[email protected]",
"roles" => {
"payroll_admin" => {
"companies" => [
{
"id" => 101112,
"name" => "Poi's Doughnuts",
"trade_name" => null,
"locations" => [
{
"id" => 131415,
"street_1" => "1236 Mission St",
"street_2" => "",
"city" => "San Francisco",
"state" => "CA",
"zip" => "94103",
"country" => "USA",
"active" => true
}
]
}
]
}
}
}
}
}
The gem is available as open source under the terms of the MIT License.