Skip to content

Commit

Permalink
orcid: show errors in form when registering
Browse files Browse the repository at this point in the history
  • Loading branch information
ntarocco committed Oct 20, 2023
1 parent 7c768f2 commit 9509dc2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
49 changes: 28 additions & 21 deletions invenio_oauthclient/handlers/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from functools import partial, wraps

from flask import (
Markup,
current_app,
flash,
redirect,
Expand Down Expand Up @@ -150,8 +151,10 @@ def authorized_signup_handler(resp, remote, *args, **kwargs):
return redirect(url_for("invenio_oauthclient_settings.index"))
except OAuthClientUserRequiresConfirmation as exc:
do_flash(
_(
f"A confirmation email has already been sent to {exc.user.email}. Please confirm your email to be able to log in."
Markup(
_(
f"A confirmation email has already been sent to {exc.user.email}. Didn't receive it? Click <strong><a href=\"{url_for('security.send_confirmation')}\">here</a></strong> to resend it."
)
),
category="success",
)
Expand Down Expand Up @@ -185,33 +188,23 @@ def signup_handler(remote, *args, **kwargs):
:param remote: The remote application.
:returns: Redirect response or the template rendered.
"""
session_prefix = token_session_key(remote.name)
account_info = session.get(session_prefix + "_account_info")

form = create_registrationform(request.form, oauth_remote_app=remote)
if not form.is_submitted():
# Show the form when the user is redirected here after `authorized`
# (GET request), to fill in the missing information (e.g. e-mail)
session_prefix = token_session_key(remote.name)
account_info = session.get(session_prefix + "_account_info")
# Pre-fill form
fill_form(form, account_info["user"])
return render_template(
current_app.config["OAUTHCLIENT_SIGNUP_TEMPLATE"],
form=form,
remote=remote,
app_title=current_app.config["OAUTHCLIENT_REMOTE_APPS"][remote.name].get(
"title", ""
),
app_description=current_app.config["OAUTHCLIENT_REMOTE_APPS"][
remote.name
].get("description", ""),
app_icon=current_app.config["OAUTHCLIENT_REMOTE_APPS"][remote.name].get(
"icon", None
),
)
elif form.is_submitted() and not form.errors:
return _render_signup_form(remote, form, account_info)
elif form.is_submitted():
# Form is submitted (POST request): validate the user input and register
# the user
try:
next_url = extra_signup_handler(remote, form, *args, **kwargs)
# after validation, if errors occurred, render the form to display
# the error messages.
if form.errors:
return _render_signup_form(remote, form, account_info)
except OAuthClientUnAuthorized:
# Redirect the user to login page
return redirect(url_for("security.login"))
Expand All @@ -227,6 +220,20 @@ def signup_handler(remote, *args, **kwargs):
return redirect("/")


def _render_signup_form(remote, form, account_info):
"""Return the rendered signup form."""
fill_form(form, account_info["user"])
remote_app = current_app.config["OAUTHCLIENT_REMOTE_APPS"][remote.name]
return render_template(
current_app.config["OAUTHCLIENT_SIGNUP_TEMPLATE"],
form=form,
remote=remote,
app_title=remote_app.get("title", ""),
app_description=remote_app.get("description", ""),
app_icon=remote_app.get("icon", None),
)


def oauth2_handle_error(remote, resp, error_code, error_uri, error_description):
"""Handle errors during exchange of one-time code for an access tokens."""
flash(_("Authorization with remote service failed."))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@

{%- block recoverable %}
{%- if security.recoverable or security.confirmable %}
<div class="ui basic segment padded">
{%- if security.recoverable %}
<a class="ui inverted header tiny"
href="{{ url_for_security('forgot_password') }}">{{ _('Forgot password?') }}</a>
{%- endif %}
{%- if security.confirmable %}
<a class="ui inverted header tiny"
href="{{url_for('security.send_confirmation')}}" class="text-muted">{{_('Resend confirmation email?')}}</a>
{%- endif %}
<div class="ui one column centered grid">
{%- if security.recoverable %}
<div class="row">
<a class="ui inverted header tiny"
href="{{ url_for_security('forgot_password') }}">{{ _('Forgot password?') }}</a>
</div>
{%- endif %}
{%- if security.confirmable %}
<div class="row">
<a class="ui inverted header tiny"
href="{{url_for('security.send_confirmation')}}" class="text-muted">{{_('Resend confirmation email')}}</a>
</div>
{%- endif %}
</div>
{%- endif %}
{%- endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

{%- extends config.OAUTHCLIENT_SETTINGS_TEMPLATE %}

{% from "invenio_oauthclient/_macros.html" import form_errors, render_field %}
{% from "invenio_oauthclient/_macros.html" import render_field %}

{% block page_body %}
<div class="ui centered grid rel-mb-2">
Expand Down

0 comments on commit 9509dc2

Please sign in to comment.