Log-in, Log-out, Signup and Password Reminder

Summary: Today I it's time to build a login form for the front-end part of our application. It will restrict access to paged such as "Videos" and "Account" until user is logged-in.


Let's quickly create a basic index page layout which would contain a log-in form on the side.

<div class="g-row">
	<div class="g-8">
        <p>Welcome to the DVD rental program. Lorem ipsum dolor sit amet</p>
    <div class="g-2">
        <h4>Customer Login</h3>

        <div style="float: left"><?$LoginButton?></div>
        <div style="float: right"><?$RegisterLink?><br/><?$ForgotLink?></div>

Next, open page/index.php and instead of what there is right now, put this code in:

class page_index extends Page {

$form $this->add('Form',null,'LoginForm');


// Short-cuts

// Manually encrypt password
$enc_p $auth->encryptPassword($p,$l);

// Manually verify login

// Manually log-in
$form->getElement('password')->displayFieldError('Incorrect login');
        return array(

Let's go over the contens of that index page. First we create a simple form with 2 fields. setFormClass() makes field labels appear over the fields. When form is submitted, we are encrypting password with the standard Authentication class, verify credentials and if they match, manually login the user and redirect him to the "video" page. If login wasn't successful, display message about incorrect login instead. We've also switched to use our new template, which defines "LoginForm" tag.

Next we should configure system-wide Auth class for our frontend. Edit the lib/Frontend.php file to add this:


$menu $this->add('Menu',null,'Menu');

Remove previous declaration of the menu, as we have new one now. The last and important thing you need to do is to configure "auth" secret. You have already placed it into the admin/config.php. The secret should match and it's used in password hash generation. Changing the secret will also make it impossible to check hashes anymore, so be sure to keep it safe.

Edit config.php to add
$config['auth']['key']='secret';  // should match the one used by admin

Create the page/video.php containing list of available videos and make it protected from non-authenticated users.

class page_video extends Page {


You can now test your interface by trying to log-in with the user you have created on the back-end. Let's also add a simple user-management page

class page_account extends Page {


$model $this->add('Model_Customer');

We don't want users to be able to change password as a regular fields, so we simply mark that field as a system before using the model on the FormAndSave. The FormAndSave widget is a simple extension of MVCForm which saves data into database once it's submitted. Nothing fancy.

Signup Form

The sign-up form is now really easy to create.

class page_signup extends Page {



$form $this->add('MVCForm');

$form->get('password') != $form->get('confirm_password'))
$form->exception('Passwords do not match')->setField('confirm_password');



$form->js()->hide('slow')->univ()->successMessage('Registered successfully')->execute();

Finally you would need a "Sign-up" button on your login-form which would send user to the signup page: