Skip to content

Instantly share code, notes, and snippets.

@johnny13
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save johnny13/9043480 to your computer and use it in GitHub Desktop.

Select an option

Save johnny13/9043480 to your computer and use it in GitHub Desktop.
Lithium Li3 Form Auth Authentication
<?php
$this->title($title);
?>
<?php if ($noauth): ?>
<h4>Login failed</h4>
<?php endif; ?>
<?=$this->form->create(); ?>
<?=$this->form->field('email');?>
<?=$this->form->field('password', array('type'=>'password'));?>
<?=$this->form->submit('Login');?>
<?=$this->form->end();?>
use lithium\storage\Session;
use lithium\action\Dispatcher;
use lithium\action\Response;
use lithium\security\Auth;
$name = basename(LITHIUM_APP_PATH);
Session::config(array(
'default' => array('adapter' => 'Php', 'session.name' => $name)
));
Auth::config(array(
'default' => array(
'adapter' => 'Form',
'model' => 'app\models\Users',
'fields' => array('email', 'password'),
'filters' => array('password' => array('lithium\util\String', 'hash')),
'validators' => array('password' => false)
)
));
<?php
namespace app\models;
class Users extends \lithium\data\Model {
//Basic validation
public $validates = array(
'email' => array(
array('notEmpty', 'message'=>'You must include your email.')
),
'password' => array(
array('notEmpty', 'message'=>'You must include a password.')
),
);
}
?>
<?php
namespace app\controllers;
use app\models\Users;
use lithium\security\Auth;
use lithium\storage\Session;
class UsersController extends \lithium\action\Controller {
public function login() {
$title = "User Accounts";
//assume there's no problem with authentication
$noauth = false;
//perform the authentication check and redirect on success
if (Auth::check('default', $this->request)){
//Redirect on successful login
return $this->redirect('/');
}
//if theres still post data, and we weren't redirected above, then login failed
if ($this->request->data){
//Login failed, trigger the error message
$noauth = true;
}
//Return noauth status
return compact('noauth','title');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment