From 49f600adfae080135ec695a89c4f6d83fe57fa80 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Wed, 24 Jan 2024 11:54:51 +0100 Subject: [PATCH] wip: refactor login flow Instead of dealing with in JavaScript, let's do a more classic HTTP flow. The main flows work, but there is still at least one to deal with: when editing a map without being logged in, the server may ask for login, and in this case we should login THEN reissue the request, so we need to interrupt the first request in some way, otherwise the server will still answer with a 403, which is what happens after this commit. --- umap/decorators.py | 14 ----- umap/static/umap/content.css | 15 +++++ umap/static/umap/js/modules/request.js | 17 +++-- umap/templates/registration/login.html | 86 +++++++++++++++----------- umap/templates/umap/branding.html | 3 + umap/templates/umap/content.html | 18 ------ umap/templates/umap/navigation.html | 4 +- umap/urls.py | 3 +- 8 files changed, 81 insertions(+), 79 deletions(-) create mode 100644 umap/templates/umap/branding.html diff --git a/umap/decorators.py b/umap/decorators.py index 005e4215..3ae667c5 100644 --- a/umap/decorators.py +++ b/umap/decorators.py @@ -60,17 +60,3 @@ def can_view_map(view_func): return view_func(request, *args, **kwargs) return wrapper - - -def jsonize_view(view_func): - @wraps(view_func) - def wrapper(request, *args, **kwargs): - response = view_func(request, *args, **kwargs) - response_kwargs = {} - if hasattr(response, "rendered_content"): - response_kwargs["html"] = response.rendered_content - if response.has_header("location"): - response_kwargs["redirect"] = response["location"] - return simple_json_response(**response_kwargs) - - return wrapper diff --git a/umap/static/umap/content.css b/umap/static/umap/content.css index ccdb250f..55fb1c98 100644 --- a/umap/static/umap/content.css +++ b/umap/static/umap/content.css @@ -37,6 +37,21 @@ input:-moz-placeholder, :-moz-placeholder { /* **************** */ /* Login icons */ /* **************** */ +body.login { + width: 320px; + margin: auto; + text-align: center; +} +body.login header { + display: flex; + justify-content: center; +} +.login-grid { + display: grid; + justify-content: space-between; + grid-gap: 5px; + grid-template-columns: repeat(auto-fill,92px); +} .login-grid li, .login-grid a { display: inline-block; diff --git a/umap/static/umap/js/modules/request.js b/umap/static/umap/js/modules/request.js index df8c4033..a77ab47b 100644 --- a/umap/static/umap/js/modules/request.js +++ b/umap/static/umap/js/modules/request.js @@ -73,12 +73,12 @@ export const ServerRequest = Request.extend({ headers['X-CSRFToken'] = token } const response = await Request.prototype.post.call(this, uri, headers, data) - return this._handle_json_response(response) + return await this._handle_json_response(response) }, get: async function (uri, headers) { const response = await Request.prototype.get.call(this, uri, headers) - return this._handle_json_response(response) + return await this._handle_json_response(response) }, _handle_json_response: async function (response) { @@ -105,10 +105,15 @@ export const ServerRequest = Request.extend({ this.ui.closePanel() } else if (data.error) { this.ui.alert({ content: data.error, level: 'error' }) - } else if (data.html) { - const ui_options = { data } - let listen_options - this.ui.openPanel(ui_options) + } else if (data.login_required) { + // TODO: stop flow and run request again when user + // is logged in + const win = window.open(data.login_required) + window.umap_proceed = () => { + console.log('logged in') + this.fire('login') + win.close() + } } }, diff --git a/umap/templates/registration/login.html b/umap/templates/registration/login.html index c9edc372..d88bffe6 100644 --- a/umap/templates/registration/login.html +++ b/umap/templates/registration/login.html @@ -1,37 +1,51 @@ -{% load i18n %} -{% if ENABLE_ACCOUNT_LOGIN %} -
{% trans "Please log in with your account" %}
-
- {% if form.non_field_errors %} -
+ {% endif %} + +{% endblock content %} diff --git a/umap/templates/umap/branding.html b/umap/templates/umap/branding.html new file mode 100644 index 00000000..b6fb9931 --- /dev/null +++ b/umap/templates/umap/branding.html @@ -0,0 +1,3 @@ +

+ {{ SITE_NAME }} +

diff --git a/umap/templates/umap/content.html b/umap/templates/umap/content.html index a9daf056..2eccf4d1 100644 --- a/umap/templates/umap/content.html +++ b/umap/templates/umap/content.html @@ -40,24 +40,6 @@ !(function () { const ui = new L.U.UI(document.querySelector('header')) - const xhr = new L.U.Xhr(ui) - const login = document.querySelector('a.login') - if (login) { - L.DomEvent.on(login, 'click', function (e) { - L.DomEvent.stop(e) - xhr.login({ - login_required: this.getAttribute('href'), - redirect: '/', - }) - }) - } - const logout = document.querySelector('a.logout') - if (logout) { - L.DomEvent.on(logout, 'click', function (e) { - L.DomEvent.stop(e) - xhr.logout(this.getAttribute('href')) - }) - } const getMore = function (e) { L.DomEvent.stop(e) xhr._ajax({ diff --git a/umap/templates/umap/navigation.html b/umap/templates/umap/navigation.html index fcc41fe0..f8d2dba9 100644 --- a/umap/templates/umap/navigation.html +++ b/umap/templates/umap/navigation.html @@ -1,9 +1,7 @@ {% load i18n %}