From 60c74ddff325dce22c776bfc01f2dfd0f3a476a7 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 4 Jan 2021 21:52:36 +1300 Subject: [PATCH] Reconnecting stale flickr auth tokens --- app/controllers/photos_controller.rb | 10 +++++++++- app/models/concerns/member_flickr.rb | 18 ++++++++++++++++++ app/views/photos/new.html.haml | 15 +++++++++++++-- config/initializers/omniauth.rb | 2 +- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 739878909..5ce70a1ae 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -92,8 +92,16 @@ class PhotosController < ApplicationController def retrieve_from_flickr @flickr_auth = current_member.auth('flickr') + return if @flickr_auth.nil? + + + if ! current_member.flickr_auth_valid? + current_member.remove_stale_flickr_auth + @please_reconnect_flickr = true + return + end + @current_set = params[:set] - return unless @flickr_auth page = params[:page] || 1 diff --git a/app/models/concerns/member_flickr.rb b/app/models/concerns/member_flickr.rb index fe13b7d6f..e2901df68 100644 --- a/app/models/concerns/member_flickr.rb +++ b/app/models/concerns/member_flickr.rb @@ -19,6 +19,24 @@ module MemberFlickr @flickr end + def flickr_auth_valid? + # no flickr token saved for this member + return false if flickr.nil? + + # test the token/secret we have + flickr.test.login # This throws exception if fails + + # success! + true + rescue FlickRaw::FailedResponse, FlickRaw::OAuthClient::FailedResponse + # token for this user doesn't work + false + end + + def remove_stale_flickr_auth + authentications.find_by(provider: "flickr")&.delete + end + # Fetches a collection of photos from Flickr # Returns a [[page of photos], total] pair. # Total is needed for pagination. diff --git a/app/views/photos/new.html.haml b/app/views/photos/new.html.haml index 7a8201a32..850ff71d2 100644 --- a/app/views/photos/new.html.haml +++ b/app/views/photos/new.html.haml @@ -1,9 +1,20 @@ - content_for :title, "New Photo" %h1 New Photo -%h2 Choose photo for #{link_to @item, @item} -- if @flickr_auth + +- if @please_reconnect_flickr + %h2 Please reconnect your flickr account + + %p the token has expired or been revoked. + + = link_to '/members/auth/flickr', class: "btn btn-lg" do + = icon 'fab', 'flickr' + Reconnect Flickr + + +- elsif @flickr_auth + %h2 Choose photo for #{link_to @item, @item} %p Connected to Flickr as = succeed "." do diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index 940d84a48..985ed96c6 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -2,5 +2,5 @@ Rails.application.config.middleware.use OmniAuth::Builder do provider :twitter, ENV['GROWSTUFF_TWITTER_KEY'], ENV['GROWSTUFF_TWITTER_SECRET'] - provider :flickr, ENV['GROWSTUFF_FLICKR_KEY'], ENV['GROWSTUFF_FLICKR_SECRET'] + provider :flickr, ENV['GROWSTUFF_FLICKR_KEY'], ENV['GROWSTUFF_FLICKR_SECRET'], scope: 'read' end