<div class="stretch-inputs">
<div class="form-group">
- <label for="email">{{ trans('auth.username') }}</label>
+ <label for="username">{{ trans('auth.username') }}</label>
@include('form.text', ['name' => 'username', 'autofocus' => true])
@if($errors->has('email'))
<div class="text-neg text-small">{{ $errors->first('email') }}</div>
use BookStack\Theming\ThemeEvents;
use Illuminate\Http\Request;
-const EMAIL_DOMAIN = 'admin.com';
+// The email domain to auto-append to usernames
+$emailDomain = 'example.com';
-Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, function(Request $request) {
-
- // Transform a "username" on login request to an email input with pre-determined domain
+// Listen for login requests in passing requests
+Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, function (Request $request) use ($emailDomain) {
if ($request->path() === 'login' && $request->method() === 'POST') {
+ // If there's a username in a login request, convert that
+ // to an 'email' input using our email domain.
$username = $request->input('username', '');
if ($username) {
$request->request->remove('username');
- $request->request->add(['email' => $username . '@' . EMAIL_DOMAIN]);
+ $request->request->add(['email' => $username . '@' . $emailDomain]);
}
}
});
\ No newline at end of file
title = "Username-based Login"
author = "@ssddanbrown"
date = 2022-11-25T20:00:00Z
-updated = 2022-11-25T20:00:00Z
-tested = "v22.10"
+updated = 2024-10-08T11:00:00Z
+tested = "v24.05.4"
+++
This is a hack to BookStack, using the theme system, so that login presents itself as a username.
- This assumes all users in your BookStack instance shares the same email domain.
- This overrides a large part of the login form so be extra aware this will be overriding any default changes to BookStack upon updates.
+- Login page errors may still reference email address, and username will not be auto prefilled after failed login submission.
+- Other functionality within BookStack can still require or use email, so it's generally a good idea to ensure the resulting email addresses would be valid, or email is disabled at a system level in some manner.
#### Options
-- Change the `EMAIL_DOMAIN` variable on line 7 of `functions.php` to be the common email domain used in your BookStack instance.
+- Change the `$emailDomain` variable on line 8 of `functions.php` to be the common email domain used in your BookStack instance (This will be auto-appended to usernames on login).
#### Code