Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Update browserID code

Roland Geider31abdfaa1b35
diff --git a/manager/static/js/browserid.js b/manager/static/js/browserid.js
new file mode 100644
index 000000000..48f87cca3
--- /dev/null
+++ b/manager/static/js/browserid.js
@@ -0,0 +1,56 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+(function($) {
+ 'use strict';
+
+ $(function() {
+ var requestOptions = [
+ 'siteName',
+ 'siteLogo',
+ 'oncancel',
+ 'privacyPolicy',
+ 'returnTo',
+ 'termsOfService'
+ ];
+
+ $(document).delegate('.browserid-login, #browserid', 'click', function(e) {
+ e.preventDefault();
+
+ // Arguments to navigator.id.request can be specified by data-attributes
+ // on the BrowserID link: <a href="#" data-site-name="Site Name">
+ var options = {};
+ var $link = $(e.target);
+ for (var k = 0; k < requestOptions.length; k++) {
+ var name = requestOptions[k];
+ var value = $link.data(name);
+ if (value !== undefined) {
+ options[name] = value;
+ }
+ }
+
+ navigator.id.request(options); // Triggers BrowserID login dialog.
+ });
+
+ $('.browserid-logout').bind('click', function(e) {
+ e.preventDefault();
+ navigator.id.logout(); // Clears User Agent BrowserID state.
+ });
+
+ navigator.id.watch({
+ onlogin: function(assertion) {
+ if (assertion) {
+ var $e = $('#id_assertion');
+ $e.val(assertion.toString());
+ $e.parent().submit();
+ }
+ },
+
+ onlogout: function() {
+ // TODO: Detect if logout button was a link and follow its href
+ // if possible.
+ }
+ });
+ });
+})(jQuery);
diff --git a/manager/templates/base.html b/manager/templates/base.html
index bb45dec5f..e8b02c93e 100644
--- a/manager/templates/base.html
+++ b/manager/templates/base.html
@@ -34,53 +34,11 @@
<script src="{% static 'js/jquery.js' %}"></script>
<script src="{% static 'js/jquery-ui.js' %}"></script>
<script src="{% static 'js/workout-manager.js' %}"></script>
+ <script src="{% static 'js/browserid.js' %}"></script>
+
{% block header %}{% endblock %}
- <script>
- navigator.id.watch({
- loggedInUser: {% if user.is_authenticated %}"{{user.email}}"{% else %}null{% endif %},
- onlogin: function(assertion) {
-
- var $e = $('#id_assertion');
- $e.val(assertion.toString());
-
- $.ajax({
- type: 'POST',
- url: $('#browserid-form').attr('action'),
- data: $('#browserid-form').serialize(),
- success: function(res, status, xhr) { window.location = '/'; },
- error: function(xhr, status, err) { alert("login failure: " + err); }
- });
- },
- onlogout: function() {
- $.ajax({
- type: 'POST',
- url: '/logout',
- success: function(res, status, xhr) { window.location.reload(); },
- error: function(xhr, status, err) { alert("logout failure: " + err); }
- });
- }
- });
-
- $(document).ready(function() {
-
- $('#browserid-login').bind('click', function(e) {
- navigator.id.request({siteName: 'Workout Manager'});
- });
-
- $('#logout').bind('click', function(e) {
- navigator.id.logout();
- });
-
- // Init the modal dialog for editing forms
- form_modal_dialog();
-
- // Initialise the hook to reload the main-content
- load_maincontent();
- });
- </script>
-
<title>{% block title %}{% endblock %}</title>
</head>
<body>
@@ -112,7 +70,7 @@
{% if user.username %}
<ul style="float:right;">
<li>
- <a href="{% url manager.views.logout %}" id="logout">
+ <a href="{% url manager.views.logout %}" id="logout" class="browserid-logout">
{% trans 'Logout' %}
</a>
</li>
diff --git a/manager/templates/user/login.html b/manager/templates/user/login.html
index 274649ef0..043c8165b 100644
--- a/manager/templates/user/login.html
+++ b/manager/templates/user/login.html
@@ -36,7 +36,7 @@
<h2>{% trans "or" %}</h2>
<a id="browserid-login"
href="#"
- class="persona-button dark">
+ class="persona-button dark browserid-login">
<span>{% trans "Sign in with your Email" %}</span>
</a>
<form method="POST" action="{% url browserid_verify %}" id="browserid-form">