]> BookStack Code Mirror - hacks/commitdiff
Updated username login hack after testing
authorDan Brown <redacted>
Tue, 8 Oct 2024 11:35:32 +0000 (12:35 +0100)
committerDan Brown <redacted>
Tue, 8 Oct 2024 11:35:32 +0000 (12:35 +0100)
content/username-login/auth/parts/login-form-standard.blade.php
content/username-login/functions.php
content/username-login/index.md

index 41bac680d58df3a25bccb46dea52bda6e9b9679d..42280ad6f3985592d7e387841f3174bca06c31ae 100644 (file)
@@ -3,7 +3,7 @@
 
     <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>
index 0b7593573c7c6bab178fd9cac553a4316b9226ee..2252e8efca113347122a0da17f51ddf3f791db77 100644 (file)
@@ -4,16 +4,18 @@ use BookStack\Facades\Theme;
 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
index 7c4c3c816939c7ef123d7eda44e29ae758d8b084..3df344b3cd63e7a672fd4b91d0ecb691cc4e4591 100644 (file)
@@ -2,8 +2,8 @@
 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.
@@ -13,10 +13,12 @@ Upon login attempt, this will match to a user of `<username>@<configured-domain>
 
 - 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