Skip to content
Snippets Groups Projects
Commit fbb5cc6d authored by Arie Peterson's avatar Arie Peterson
Browse files

Add a demo mode with public sign-up form

parent 179473ef
No related branches found
No related tags found
1 merge request!91Resolve "Demo instance features"
Pipeline #37535 passed with stages
in 4 minutes and 1 second
from flask import jsonify, request
from flask_jwt_extended import get_jwt, jwt_required
from flask_cors import cross_origin
from flask_expects_json import expects_json
from flask_jwt_extended import get_jwt, jwt_required
from areas import api_v1
from helpers import KratosApi
......
......@@ -21,3 +21,5 @@ SQLALCHEMY_TRACK_MODIFICATIONS = False
# running in a Kubernetes pod. Set it to "false" to load the config from the
# `KUBECONFIG` environment variable.
LOAD_INCLUSTER_CONFIG = os.environ.get("LOAD_INCLUSTER_CONFIG").lower() == "true"
DEMO_INSTANCE = os.environ.get("DASHBOARD_DEMO_INSTANCE", "False").lower() in ('true', '1')
......@@ -16,7 +16,7 @@ from ory_hydra_client.models import AcceptConsentRequest, AcceptLoginRequest, Co
import ory_hydra_client.exceptions as hydra_exceptions
import ory_kratos_client
from ory_kratos_client.api import frontend_api, identity_api
from flask import abort, redirect, render_template, request, current_app
from flask import abort, current_app, jsonify, redirect, render_template, request
from database import db
from helpers import KratosUser
......@@ -24,6 +24,8 @@ from config import *
from web import web
from areas.apps import AppRole, App, OAuthClientApp
from areas.roles import RoleService
from areas.roles.models import Role
from areas.users.user_service import UserService
# This is a circular import and should be solved differently
......@@ -159,7 +161,7 @@ def login():
# or `not identity`
# User is not logged in yet.
# In either case, we present the login screen now.
return render_template("login.html", api_url=KRATOS_PUBLIC_URL, dashboard_url=DASHBOARD_URL, refresh=refresh)
return render_template("login.html", api_url=KRATOS_PUBLIC_URL, dashboard_url=DASHBOARD_URL, refresh=refresh, demo=DEMO_INSTANCE)
@web.route("/auth", methods=["GET", "POST"])
......@@ -520,3 +522,14 @@ def logout():
ex)
return redirect(kratos_api_response.logout_url)
if DEMO_INSTANCE:
@web.route("/demo-user", methods=["POST"])
def demo_user():
data = request.get_json()
defaults = {
"name": "",
"app_roles": [{"name": "dashboard", "role_id": Role.ADMIN_ROLE_ID}],
}
UserService.post_user({**defaults, **data})
return jsonify("User created successfully. You should receive an email to confirm your address and set a password.")
......@@ -24,5 +24,43 @@
<div id="contentHelp">
<a href='recovery'>Set new password</a> | <a href='https://stackspin.net'>About stackspin</a>
</div>
{% if demo %}
<br>
<script>
function submitSignup() {
let result = document.querySelector('#signup-result');
let email = document.querySelector('#signup-email');
let xhr = new XMLHttpRequest();
let url = "/web/demo-user";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
result.innerHTML = this.responseText;
}
};
// Converting JSON data to string
var data = JSON.stringify({"email": email.value });
// Sending data with the request
xhr.send(data);
}
</script>
<h2>Sign up for this demo instance</h2>
Enter your email address here to create an account on this Stackspin
instance.
<div class="alert alert-warning">Warning: this is a demo instance! That means that:
<ul>
<li>Anyone can create an account on this same instance, like yourself,
and will share the same set of users and data. So any data you create
or upload, including the email address you enter here, becomes
essentially public information.</li>
<li>Every night (Europe/Amsterdam time), this instance gets automatically
reset to an empty state, so any data you create or upload will be
destroyed.</li>
</ul>
</div>
<div class="form-group"><label for="signup-email">Email address</label><input type="email" class="form-control" id="signup-email" name="signup-email" placeholder="Your email address to sign up with."></div>
<div class="form-group"><button class="btn btn-primary" onclick="submitSignup()">Sign up</button><p id="signup-result"></p></div>
{% endif %}
{% endblock %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment