Skip to content

Commit 55adfcc

Browse files
committed
Implement improvements suggested in #94
1 parent d1f348f commit 55adfcc

5 files changed

Lines changed: 17 additions & 6 deletions

File tree

lib/rpi_auth/controllers/auto_refreshing_token.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ module AutoRefreshingToken
1818
private
1919

2020
def refresh_credentials_if_needed
21-
return unless current_user
21+
return if current_user.blank? || current_user.expires_at.blank?
2222

2323
return if Time.now.to_i + REFRESH_WINDOW_IN_SECONDS <= current_user.expires_at
2424

25+
# This raises an OAuth2::Error on failure, which is rescued by the
26+
# handle_oauth2_error method.
2527
current_user.refresh_credentials!
2628
self.current_user = current_user
27-
rescue OAuth2::Error, ArgumentError
29+
rescue OAuth2::Error
30+
# Catching here allows the controller to continue on from where it left
31+
# off.
2832
reset_session
2933
end
3034
end

lib/rpi_auth/controllers/current_user.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ module CurrentUser
99
helper_method :current_user if respond_to?(:helper_method)
1010
end
1111

12+
# Make sure our memoized user is cleared out on reset
13+
def reset_session
14+
@current_user = nil
15+
super
16+
end
17+
1218
def current_user
1319
return nil unless session[:current_user]
1420
return @current_user if @current_user

spec/dummy/app/controllers/home_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ def show
33
end
44

55
def reset_user
6-
current_user
76
reset_session
87
render :show
98
end

spec/dummy/app/views/home/show.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
<p>
44
<% if current_user.present? %>
55
Logged in as <%= current_user.user_id %>
6+
<br />
67
<%= link_to 'Log out', rpi_auth_logout_path, class: 'button' %>
78
<% else %>
9+
Logged out
10+
<br />
811
<%= button_to 'Log in', rpi_auth_login_path, method: :post %>
912
<br />
1013
<%= link_to 'Log in GET', rpi_auth_login_path %> (should 404)

spec/dummy/spec/requests/refresh_credentials_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
subject(:request) { get root_path }
1111

12-
let(:logged_in_text) { 'Log out' }
1312
let(:stub_oauth_client) { instance_double(RpiAuth::OauthClient) }
1413

1514
before do
@@ -40,14 +39,14 @@
4039
shared_examples 'the user is logged in' do
4140
it do
4241
request
43-
expect(response.body).to include(logged_in_text)
42+
expect(response.body).to include('Logged in as')
4443
end
4544
end
4645

4746
shared_examples 'the user is logged out' do
4847
it do
4948
request
50-
expect(response.body).not_to include(logged_in_text)
49+
expect(response.body).to include('Logged out')
5150
end
5251
end
5352

0 commit comments

Comments
 (0)