Skip to content

Instantly share code, notes, and snippets.

@ssddanbrown
Last active November 25, 2022 10:13
Show Gist options
  • Save ssddanbrown/90a3944e2c1ed168b1d750c0933c99de to your computer and use it in GitHub Desktop.
Save ssddanbrown/90a3944e2c1ed168b1d750c0933c99de to your computer and use it in GitHub Desktop.
bookstack-username-login-hack

This is a hack to BookStack, using the theme system, so that login presents itself as a username. Upon login attempt, this will match to a user of <username>@<configured-domain> within the database.

Setup

This uses the logical theme system.

  1. Within the BookStack install folder, you should have a themes folder.
  2. Create a themes/custom/functions.php file with the contents of the functions.php file example below.
  3. Configure the EMAIL_DOMAIN to be your desired email domain (Should be common across BookStack users).
  4. Create a themes/custom/auth/parts/login-form-standard.blade.php file with the contents of the login-form-standard.blade.php file below.
  5. Add APP_THEME=custom to your .env file.

Note

These customizations are not officially supported any may break upon, or conflict with, future updates. Quickly tested on BookStack v22.10

<?php
use BookStack\Facades\Theme;
use BookStack\Theming\ThemeEvents;
use Illuminate\Http\Request;
const EMAIL_DOMAIN = 'admin.com';
Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, function(Request $request) {
// Transform a "username" on login request to an email input with pre-determined domain
if ($request->path() === 'login' && $request->method() === 'POST') {
$username = $request->input('username', '');
if ($username) {
$request->request->remove('username');
$request->request->add(['email' => $username . '@' . EMAIL_DOMAIN]);
}
}
});
<form action="{{ url('/login') }}" method="POST" id="login-form" class="mt-l">
{!! csrf_field() !!}
<div class="stretch-inputs">
<div class="form-group">
<label for="email">{{ 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>
@endif
</div>
<div class="form-group">
<label for="password">{{ trans('auth.password') }}</label>
@include('form.password', ['name' => 'password'])
<div class="small mt-s">
<a href="{{ url('/password/email') }}">{{ trans('auth.forgot_password') }}</a>
</div>
</div>
</div>
<div class="grid half collapse-xs gap-xl v-center">
<div class="text-left ml-xxs">
@include('form.custom-checkbox', [
'name' => 'remember',
'checked' => false,
'value' => 'on',
'label' => trans('auth.remember_me'),
])
</div>
<div class="text-right">
<button class="button">{{ Str::title(trans('auth.log_in')) }}</button>
</div>
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment