Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/controllers/oauth_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class OauthController < ApplicationController
def authorize
redirect_to "https://app.hubspot.com/oauth/authorize?client_id=#{ENV['HUBSPOT_CLIENT_ID']}&redirect_uri=#{ENV['HUBSPOT_REDIRECT_URI']}&scope=#{ENV['HUBSPOT_SCOPES']}", allow_other_host: true
end

def callback
@resp = Hubspot.create_token!(params[:code])
end
end
30 changes: 30 additions & 0 deletions app/services/hubspot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Hubspot
TOKEN_BASE_URL = 'https://api.hubapi.com/oauth/v1/token'.freeze

def self.create_token!(code)
body = {
grant_type: 'authorization_code',
code: code
}.merge(auth_params)

HTTParty.post(TOKEN_BASE_URL, body: body)
end

def self.refresh_token!(user)
body = {
grant_type: 'refresh_token',
refresh_token: user.try(:hubspot_refresh_token)
}.merge(auth_params)

resp = HTTParty.post(TOKEN_BASE_URL, body: body)
user.update(hubspot_access_token: resp['access_token'])
end

def self.auth_params
{
client_id: ENV['HUBSPOT_CLIENT_ID'],
client_secret: ENV['HUBSPOT_CLIENT_SECRET'],
redirect_uri: ENV['HUBSPOT_REDIRECT_URI']
}
end
end
2 changes: 2 additions & 0 deletions app/views/dashboard/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<%= render partial: "shared/heading", locals: { title: 'Dashboard', description: '' } %>

<%= link_to 'OAuth HubSpot', '/oauth/authorize', class: 'p-5 rounded-md bg-primary-500 text-white' %>
1 change: 1 addition & 0 deletions app/views/oauth/callback.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= @resp %>
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@
# convenience helper
get 'admin', to: 'admin/dashboard#index'
end

# oauth routes
get 'oauth/authorize', to: 'oauth#authorize'
get 'oauth/callback', to: 'oauth#callback'
end