Custom member login page

silverstripe-4
Tags: #<Tag:0x00007f082677c560>

#1

Silverstripe Version:4.0.3

Question: What is the cleanest way to make a custom member login page?

Just wanted to alter the login page for members so I can;

  • change the layout
  • add custom JavaScript (specifically to show a splash image before the login form)
  • add to the form fields and validate them ( specifically a checkbox agreeing to the t&c)

I have managed to achieve the first two of these points by adding a Security_login.ss file to my site modules templates/Layout folder.
Then adding the java script file in Secutiry_login.ss this way;

<% require css("kiwi/code/css/SplashLogin.css") %>

Then I tried to extend SilverStripe\Security\MemberAuthenticator\MemberLoginForm
like so;

use SilverStripe\Security\MemberAuthenticator\MemberLoginForm;
class CustomMemberLogin extends MemberLoginForm{
        private static $required_fields = [
        'Email',
        'Password',
        'AgreeTerms',
    ];
    
    function getFormFields(){
        $fields = parent::getFormFields();
        $fields->push(
                CheckboxField::create(
                    "AgreeTerms",
                    "Agree To Terms"
                )
            );
    }
}

But then when I added this to a yml config (app.yml) it went a little haywire:

SilverStripe\Core\Injector\Injector:
  SilverStripe\Security\MemberAuthenticator\MemberLoginForm:
    class: CustomMemberLogin

After that I was unable to load the login page or see any errors.

Is there a cleaner and easier method entirely and/or have I missed something crucial?


#2

As I was driving to work this morning I suddenly had a though; "did I return $fields from getFormFields()?

The answer was no.

So adding;
return $fields; to the end of the getFormFields() method
fixed this!

I’m still curious if this is the best approach so please reply if you have any insight!