Skip to content

Commit b42e9c7

Browse files
authored
Merge pull request #183 from datacite/fetch-token-when-authenticating-from-orcid
Fetch token and redirect to Commons when authenticating from ORCID Search & Link
2 parents ff0a945 + b3097b6 commit b42e9c7

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

app/controllers/users/omniauth_callbacks_controller.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,11 @@ def globus
107107

108108
def orcid
109109
auth = request.env["omniauth.auth"]
110+
omni_params = request.env["omniauth.params"]
110111
omniauth = flash[:omniauth] || {}
111112

112113
if current_user.present?
113114
@user = current_user
114-
@user.update(orcid_expires_at: User.timestamp(auth.credentials),
115-
orcid_token: auth.credentials.token)
116-
flash[:notice] = "ORCID token successfully refreshed."
117115
else
118116
@user = User.from_omniauth(auth, provider: "globus")
119117
end
@@ -129,6 +127,13 @@ def orcid
129127
if @user.persisted?
130128
sign_in @user
131129

130+
# Refresh ORCID token if the flag isn't explicitly false
131+
if omni_params["fetch_token"] != "false"
132+
@user.update(orcid_expires_at: User.timestamp(auth.credentials),
133+
orcid_token: auth.credentials.token)
134+
flash[:notice] = "ORCID token successfully refreshed."
135+
end
136+
132137
cookies[:_datacite] = encode_cookie(@user.jwt)
133138

134139
if stored_location_for(:user) == ENV["BLOG_URL"] + "/admin/"
@@ -145,7 +150,13 @@ def orcid
145150

146151
netlify_response(token: token, content: content)
147152
else
148-
redirect_to stored_location_for(:user) || setting_path("me")
153+
154+
# Redirect to Commons if the flag isn't explicitly false. Otherwise redirect to profile settings
155+
if omni_params["redirect_to_commons"] != "false"
156+
redirect_to "#{ENV['COMMONS_URL']}/orcid.org/#{current_user.orcid}"
157+
else
158+
redirect_to stored_location_for(:user) || setting_path("me")
159+
end
149160
end
150161
else
151162
flash[:alert] = @user.errors.map { |k, v| "#{k}: #{v}" }.join("<br />").html_safe || "Error signing in with #{provider}"

app/views/settings/_show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
<%= link_to "<img id=\"orcid-logo\" src=\"#{ENV["CDN_URL"]}/images/orcid.png\" alt=\"ORCID icon\"/>&nbsp;Delete ORCID Token".html_safe, setting_path("me", user: { orcid_token: nil, orcid_expires_at: Time.zone.now }), { method: :put, remote: true, class: 'btn btn-social btn-orcid btn-fill' } %>
8383
<% else %>
8484
<p>Get ORCID token to allow DataCite to update your ORCID record.</p>
85-
<%= link_to "<img id=\"orcid-logo\" src=\"#{ENV["CDN_URL"]}/images/orcid.png\" alt=\"ORCID icon\"/>&nbsp;Get ORCID Token".html_safe, user_orcid_omniauth_authorize_path, method: :post, :id => "sign-in-orcid", class: 'btn btn-social btn-orcid btn-fill' %>
85+
<%= link_to "<img id=\"orcid-logo\" src=\"#{ENV["CDN_URL"]}/images/orcid.png\" alt=\"ORCID icon\"/>&nbsp;Get ORCID Token".html_safe, user_orcid_omniauth_authorize_path(redirect_to_commons: false), method: :post, :id => "sign-in-orcid", class: 'btn btn-social btn-orcid btn-fill' %>
8686
<% end %>
8787
</dd>
8888
</dl>

app/views/users/sessions/new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<% else %>
1313
<div class="register-card-globus">
1414
<h3 class="title">Sign In</h3>
15-
<%= link_to "<img id=\"orcid-logo\" src=\"#{ENV["CDN_URL"]}/images/orcid.png\" alt=\"ORCID icon\"/>&nbsp;Sign in with ORCID".html_safe, user_orcid_omniauth_authorize_path, method: :post, :id => "sign-in-orcid", class: 'btn btn-block btn-social btn-orcid btn-fill' %>
15+
<%= link_to "<img id=\"orcid-logo\" src=\"#{ENV["CDN_URL"]}/images/orcid.png\" alt=\"ORCID icon\"/>&nbsp;Sign in with ORCID".html_safe, user_orcid_omniauth_authorize_path(fetch_token: false, redirect_to_commons: false), method: :post, :id => "sign-in-orcid", class: 'btn btn-block btn-social btn-orcid btn-fill' %>
1616
</div>
1717
<% end %>
1818
</div>

0 commit comments

Comments
 (0)