Home

Customize the Redirect after a successful Member Login

Requirements

SilverStripe 2.3+

The Member system that comes with SilverStripe offers substantial control over your site's authentication capabilities.  It often requires the developer to build out or extend most common functionality to websites.  This is great in that you are never "stuck" with the out-of-the-box offerings, but as any SS dev knows you end up with a little bag of Member tricks that gets used across many projects.  Presented here is one that I use often.

When a Member logs in to /Security/login without a ?BackURL= specified, they are always redirected back to the form with a success message and form without any fields.  While this works great to keep the code/form tight, it is a less than ideal use experience.  Modifying this behavior is a little tricky as much of the controller and rendering actions are fixed in the Security class.  This is further complicated by the use of Director::redirect calls that set response headers when the login form is processed.  I find by overriding the form and replacing it with the excellent useCustomClass call works well.

The Problem: Base /Security/login form redirects to itself on success, always tagging ?BackURL=/home looks sort of messy across your site.

The Solution: Make the base MemberLoginForm redirect somewhere useful on success.

We begin by extending the MemberLoginForm and the method that does all the dirty work; dologin().  Create the file GoHomeLoginForm.php and add the following code:

Now we need to override the MemberLoginForm with our GoHomeLoginForm in our _config.php:

Now do a /dev/build/flush=all and you're gold.

Testing for a redirect being set and a current member set in the session ensures that only users who have successfully logged in will get our custom destination.  You can override with any destination you please, such as a '/myaccount/' section or even plain old '/'.

This has been tested on SS 2.3 and 2.4 using the default Member Authenticator.

About the Author

Name: dalesaurus

Comments (1)

  • Great wee snippet! Very useful.

    Posted by Jenny, 10/08/2010 10:54am (28 days ago)

RSS feed for comments on this page RSS feed for all comments

Post your comment