Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

User Login in Frontend


Go to End


10 Posts   6983 Views

Avatar
mhdesign

Community Member, 216 Posts

10 August 2016 at 3:26pm

Found the answer on SS Bits...

http://www.ssbits.com/tutorials/2009/custom-login-form-with-group-based-redirection/

Make sure you read all the comments to get it working in SS 3.x...

Avatar
Mo

Community Member, 541 Posts

11 August 2016 at 7:58am

Edited: 11/08/2016 7:58am

Hi All,

If you want to embed a generic login form on a page in your CMS, you can hijack the default form used by Security/login.

I do this quite a lot, to gain access to the basic form you will need to add the following functions to your page controller:


class Page_Controller extends ContentController
{
...

    private static $allowed_actions = array(
        "LoginForm",
        "login",
    );

    /**
     * Use this function to direct a user where you want them to go after login
     * (currently the root of the site)
     */
    public function login()
    {
        return $this->redirect(Director::absoluteBaseURL());
    }

    /**
     * Get the login form to process according to the submitted data
     *
     * @return Form
     */
    public function LoginForm()
    {
        $authenticator = Authenticator::get_default_authenticator();
        if($authenticator) {
            $form = $authenticator::get_login_form($this);
            // Use this if you want to use a custom form template
            $form->setTemplate('HomeLoginForm');
            return $form;
        }
        throw new Exception('Passed invalid authentication method');
    }

    ...

}

You can add this form to your template by using the $LoginForm variable.

It should work off the bat and load the default login form into your template, if there are any issues give us a shout.

Go to Top