Last active
August 29, 2015 13:56
-
-
Save johnny13/9043480 to your computer and use it in GitHub Desktop.
Lithium Li3 Form Auth Authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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();?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| ) | |
| )); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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.') | |
| ), | |
| ); | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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