Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Hide the login page register link when ALLOW_REGISTRATION is off (#479)

Murtaza Nasirf8a8fe892398
diff --git a/docs/getting-started/installation.md b/docs/getting-started/installation.md
index 22b07050..7097a0ad 100644
--- a/docs/getting-started/installation.md
+++ b/docs/getting-started/installation.md
@@ -273,7 +273,7 @@ REGISTRATION_ALLOWED_DOMAINS=
LOG_LEVEL="INFO"
```
-Setting `ALLOW_REGISTRATION=false` means only the admin can create new user accounts, which is recommended for private installations where you want to control access. If you're running Speakr for a group or family, this prevents random people from creating accounts. If you enable registration and want to restrict it to specific email domains (e.g., for corporate use), set `REGISTRATION_ALLOWED_DOMAINS` to a comma-separated list of domains like `company.com,subsidiary.org`. Leave it empty to allow all domains. The log level controls how much information Speakr writes to its logs. Use `INFO` during initial setup and testing to see what's happening, then switch to `ERROR` for production to reduce log volume and improve performance.
+Setting `ALLOW_REGISTRATION=false` means only the admin can create new user accounts, which is recommended for private installations where you want to control access. If you're running Speakr for a group or family, this prevents random people from creating accounts. With registration disabled, the login page also omits its "Register here" link. Only the value `true` (in any letter case) enables registration; an unset variable defaults to `true`, and any other value disables it. If you enable registration and want to restrict it to specific email domains (e.g., for corporate use), set `REGISTRATION_ALLOWED_DOMAINS` to a comma-separated list of domains like `company.com,subsidiary.org`. Leave it empty to allow all domains. The log level controls how much information Speakr writes to its logs. Use `INFO` during initial setup and testing to see what's happening, then switch to `ERROR` for production to reduce log volume and improve performance.
### Step 5: Configure Advanced Features
diff --git a/src/api/auth.py b/src/api/auth.py
index 89ee7fe4..d5f92914 100644
--- a/src/api/auth.py
+++ b/src/api/auth.py
@@ -124,6 +124,16 @@ class LoginForm(FlaskForm):
# --- Helper Functions ---
+def registration_enabled() -> bool:
+ """Whether self-service registration is open (ALLOW_REGISTRATION).
+
+ The /register route and the login page's register link both ask this, so
+ the page never advertises a route that refuses (#479). Read per request:
+ the environment can change under the test suite.
+ """
+ return os.environ.get('ALLOW_REGISTRATION', 'true').lower() == 'true'
+
+
def is_registration_domain_allowed(email: str) -> bool:
"""Check if email domain is allowed for registration.
@@ -182,10 +192,7 @@ def record_timezone():
@auth_bp.route('/register', methods=['GET', 'POST'])
@rate_limit("10 per minute")
def register():
- # Check if registration is allowed
- allow_registration = os.environ.get('ALLOW_REGISTRATION', 'true').lower() == 'true'
-
- if not allow_registration:
+ if not registration_enabled():
flash(translate('server.flash.registrationDisabled'), 'danger')
return redirect(url_for('auth.login'))
diff --git a/src/app.py b/src/app.py
index 81fc190b..93d8205c 100644
--- a/src/app.py
+++ b/src/app.py
@@ -826,6 +826,12 @@ def inject_help_center_link():
'help_center_label': os.environ.get('HELP_CENTER_LABEL', '').strip(),
}
+def inject_registration_flag():
+ """The login page offers the register link only when /register accepts (#479)."""
+ from src.api.auth import registration_enabled
+ return {'registration_enabled': registration_enabled()}
+
@app.context_processor
def inject_group_admin_status():
"""Inject is_group_admin flag and the admin-group list into all templates.
diff --git a/templates/login.html b/templates/login.html
index b9253bca..f3a1e023 100644
--- a/templates/login.html
+++ b/templates/login.html
@@ -160,10 +160,12 @@
<div class="flex flex-col space-y-4">
{{ form.submit(class="w-full py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-[var(--text-button)] bg-[var(--bg-button)] hover:bg-[var(--bg-button-hover)] focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[var(--border-focus)]") }}
+ {% if registration_enabled %}
<div class="text-center text-sm text-[var(--text-muted)]">
<span>Don't have an account?</span>
<a href="{{ url_for('auth.register') }}" class="font-medium text-[var(--text-accent)] hover:underline">Register here</a>
</div>
+ {% endif %}
</div>
</form>
{% endif %}
diff --git a/tests/test_cov_auth.py b/tests/test_cov_auth.py
index e2ad10d0..af09432d 100644
--- a/tests/test_cov_auth.py
+++ b/tests/test_cov_auth.py
@@ -483,6 +483,36 @@ def test_register_blocked_by_disallowed_domain(cleanup_users):
assert User.query.filter_by(email=email).first() is None
+# The login page and the /register route must agree on every value (#479):
+# only 'true' in any case opens registration, unset defaults to open, and any
+# other value ('false', 'yes', '1', '') closes it.
+_REGISTRATION_VALUES = [
+ ('true', True), ('True', True), ('TRUE', True), (None, True),
+ ('false', False), ('False', False), ('FALSE', False),
+ ('yes', False), ('1', False), ('', False),
+]
+
+
[email protected]('value,enabled', _REGISTRATION_VALUES)
+def test_registration_enabled_helper(value, enabled):
+ with _envset(ALLOW_REGISTRATION=value):
+ assert auth_mod.registration_enabled() is enabled
+
+
[email protected]('value,enabled', _REGISTRATION_VALUES)
+def test_login_register_link_matches_register_route(value, enabled):
+ with _envset(ALLOW_REGISTRATION=value):
+ client = app.test_client()
+ page = client.get('/login')
+ assert page.status_code == 200
+ link_shown = b'Register here' in page.data
+ assert link_shown is enabled
+ assert (b'href="/register"' in page.data) is enabled
+ # The link is shown exactly when the route it points to accepts.
+ route = client.get('/register')
+ assert (route.status_code == 200) is enabled
+
+
def test_is_registration_domain_allowed_helper(app_ctx):
with _envset(REGISTRATION_ALLOWED_DOMAINS='example.com'):
assert auth_mod.is_registration_domain_allowed('[email protected]') is True