Skip to content
Permalink
Browse files

Add refresh tokens and the koala gem

  • Loading branch information...
excid3 committed Aug 8, 2017
1 parent 24ce015 commit 9ff7b5eb815be00c5148a2a2acaf46768ca062ff
Showing with 54 additions and 2 deletions.
  1. +2 −0 Gemfile
  2. +6 −0 Gemfile.lock
  3. +5 −0 app/controllers/home_controller.rb
  4. +26 −0 app/models/service.rb
  5. +7 −0 app/views/home/index.html.erb
  6. +6 −0 config/initializers/koala.rb
  7. +2 −2 config/secrets.yml
@@ -64,3 +64,5 @@ gem 'sidekiq', '~> 5.0'
gem 'foreman', '~> 0.84.0'

gem 'omniauth-facebook'
gem 'koala'

@@ -113,6 +113,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (2.1.0)
jwt (1.5.6)
kaminari (1.0.1)
activesupport (>= 4.1.0)
@@ -126,6 +127,10 @@ GEM
activerecord
kaminari-core (= 1.0.1)
kaminari-core (1.0.1)
koala (3.0.0)
addressable
faraday
json (>= 1.8)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
@@ -278,6 +283,7 @@ DEPENDENCIES
foreman (~> 0.84.0)
jbuilder (~> 2.5)
jquery-rails (~> 4.3.1)
koala
listen (>= 3.0.5, < 3.2)
omniauth-facebook
pg (~> 0.18)
@@ -1,5 +1,10 @@
class HomeController < ApplicationController
before_action :authenticate_user!, only: :index

def index
@graph = current_user.services.facebook.last.client
page_params = params.permit!.to_h[:page]
@results = params[:page] ? @graph.get_page(page_params) : @graph.get_connections("me", "feed")
end

def terms
@@ -1,3 +1,29 @@
class Service < ApplicationRecord
belongs_to :user

%w{ facebook twitter }.each do |provider|
scope provider, ->{ where(provider: provider) }
end

def client
send("#{provider}_client")
end

def expired?
expires_at? && expires_at <= Time.zone.now
end

def access_token
send("#{provider}_refresh_token!", super) if expired?
super
end

def facebook_client
Koala::Facebook::API.new(access_token)
end

def facebook_refresh_token!(token)
new_token_info = Koala::Facebook::OAuth.new.exchange_access_token_info(token)
update(access_token: new_token_info["access_token"], expires_at: Time.zone.now + new_token_info["expires_in"])
end
end
@@ -1 +1,8 @@
<h1>Welcome to Facebookr!</h1>

<% @results.each do |result| %>
<p><%= result['message'] %></p>
<% end %>

<%= link_to 'Previous', url_for(:page => @results.previous_page_params) %>
<%= link_to 'Next', url_for(:page => @results.next_page_params) %>
@@ -0,0 +1,6 @@
Koala.configure do |config|
config.app_id = Rails.application.secrets.facebook_app_id
config.app_secret = Rails.application.secrets.facebook_app_secret
# See Koala::Configuration for more options, including details on how to send requests through
# your own proxy servers.
end
@@ -19,8 +19,8 @@

development:
secret_key_base: 92aa2c2250b21c26d15f07ea1e6c0019409544192d8856fce39a05be89c8e4a0b2130bbf37c4fecea70c593d13a215540f58b628bb2617b2da2c4014f3112983
facebook_app_id: 105092146834964
facebook_app_secret: b74f613b23c9f9b13440650b037516a2
facebook_app_id: <%= ENV["FACEBOOK_APP_ID"] %>
facebook_app_secret: <%= ENV["FACEBOOK_APP_SECRET"] %>

test:
secret_key_base: 540b1a1a6f3163662d6c6193cc33dc654d98bd0cd59c1d6970673533466bdfa0342ad11da4547f901bd774f2910bb5a59496aedc9558664dd91358d546d6148e

0 comments on commit 9ff7b5e

Please sign in to comment.
You can’t perform that action at this time.